Write a Java program to add two numbers and display the result in the output screen.
Answers
Explanation:
Program to Add Two Integers
public class AddTwoIntegers {
public static void main(String[] args) {
int first = 10;
int second = 20;
System.out.println("Enter two numbers: " + first + " " + second);
int sum = first + second;
System.out.println("The sum is: " + sum);
}
}
Output:
Enter two numbers: 10 20
The sum is: 30
In this program, two integers 10 and 20 are stored in integer variables first and second respectively.
Then, first and second are added using the + operator, and its result is stored in another variable sum.
Finally, sum is printed on the screen using println() function
Answer:
import java.util.Scanner;
public class add
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("ENTER THE FIRST NUMBER")
double a=sc.nextInt();
System.out.print("ENTER THE SECOND NUMBER")
double b=sc.nextInt();
double c=a+b;
System.out.println("THE SUM OF "+a+" and "+b+" is "+c)
}
}