answersLogoWhite

0

How do you add two num in java?

User Avatar

Anonymous

15y ago
Updated: 8/18/2019

Java's arithmetic capabilities are extremely simple and are very easy to use. Remember that all programming languages store values in variables. Look at this piece of code.

int a = 5;

int b = 10;

The above code created 2 variables. Both of them are integers, and they are named 'a' and 'b' respectively. The first (a) was assigned a value of 5, and the second (b) was assigned a value of 10. Let's see how we can add them.

int c = a + b;

This simple statement creates a new variable named 'c.' It is also an integer, and the initial value is set to 'a + b'. Since a equals 5 and b equals 10, c is equal to 15.

Of course, we don't always have to use a variable in order to add. Look at the code below:

System.out.println(12345 + 67890);

This code simply prints the value of 12345+67890 (80235) to the screen. Now, for a working example:

public class AdditionTest {

public static void main(String[] args) {

int a = 5;

int b = 10;

System.out.println("The value of a + b is " + a + b);

}

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

How to create a Java scanner class to add two numbers?

http://javacodespot.blogspot.com/2011/04/java-programming-using-scanner-for-user.html import java.util.*; public class ScannerClass { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.print("1st num: "); int num1=scan.nextInt(); System.out.println(); System.out.print("2nd num: "); int num2=scan.nextInt(); int sum=num1+num2; System.out.println(); System.out.println("Sum: "+sum); } }


Write a method which displays first 50 prime numbers?

Implement an isPrime method int JAVA with this: int count = 0, num = 2; while(count < 50) { if(isPrime(num)) count++; num++; }


Composite numbers from 1-10 in c plus plus programming?

#include<iostream> bool is_composite (const size_t); bool is_prime (const size_t); int main() { for (size_t num=1; num<=10; ++num) if (is_composite (num)) std::cout << num << " is composite\n"; } bool is_composite (const size_t num) { if (num < 4U) return false; return !is_prime (num); } bool is_prime (const size_t num) { const size_t two = 2U; if (num < two) return false; if (!(num % two)) return num == two; const size_t max = static_cast<size_t>(sqrt (static_cast<double>(num))); for (size_t div = 3U; div <= max; div += two) if (!(num % div)) return false; return true; }


How do you find the complement of numbers in java script?

function complement(num, base){ var result = 0, column = 0; if(base < 2) return null; while(num > 0){ digit = num % base;comp = base - 1 - digit;result += comp * Math.pow(base, column);column++;num -= digit;num /= base;} return result; }


What is Literal in java?

Literals are the values assigned to variables. int num = 10; Here 10 is the integer literal.


How can add java code to java server page?

Scriplet.


Write a program to show the concept of recursion in java?

public class Main{ public static void main(String[] args){ System.out.println("the factorial of 5 is: " + getFactorial(5)); } public static int getFactorial(int num){ return num + getFactorial(num-1); } }


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


How do you write a program to read a value and display all prime numbers up to the value?

#include#includebool is_prime(unsigned num) {unsigned max, factor;if (num


What does the plus in java mean?

It is used for addition - to add two numbers. Also, to concatenate two Strings (texts) - that is, to make a longer text from two shorter ones.


Can you add smiley on your java notepad?

yes


What will be a simple program to explain object in java?

This is the Num objects class public class Num{ public int number; // makes the integer that will store the number public Num(int a){ //constructor number = a; //number being stored } public void setNumber(int b){ //New Method that will edit number number = b; } public int getNumber(){ //New Method that will return number return number; } } This is the class that edits and add the object public class FindNum{ public static void main(String[] args){ Num example = new Num(5); //creates an object of the class Num and sets the int number to 5 example.setNumber(7); //set the new value to 7 System.out.print(example.getNumber()); //returns the value and prints it } }