#ADD TWO NUMBERS IN JAVA
ADDS TWO NUMBERS IN JAVA
Here's an
example code in Java that adds two numbers:
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
System.out.println("The sum of
" + num1 + " and " + num2 + " is " + sum);
}
}
NOTE:-
In this example, we declare two integer variables num1 and num2, assign them the values 5 and 10 respectively, add them using the + operator and store the result in the sum variable. Finally, we print out the result using System.out.println(). The output of this program would be:
TO
ADD TWO NUMBERS IN JAVA USING USER-DEFINED
To add two numbers in Java using user-defined
inputs, you can use the Scanner class to get input from the user, and then
perform the addition operation on the input numbers. Here is an example:
import
java.util.Scanner;
public
class AddTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the first
number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second
number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum of
" + num1 + " and " + num2 + " is " + sum);
}
}
Note
In this example, we first create a Scanner object to read input from the user. We then use the nextInt() method of the Scanner class to get the first and second numbers from the user.
We then add the two numbers together using the + operator and store the result in a variable called sum. Finally, we print out the sum using the println() method of the System class.
When you run this program, it will prompt the user to enter two numbers. Once the user enters the numbers, the program will add them together and display the result.
Comments
Post a Comment