Write a java-program to print addition of two numbers
Program
public class Main
{
public static void main(String args[])
{
int x=10,y=20,z;
z=x+y;
System.out.println("Addition is "+(z));
}
}
Output:
Addition is 30
Using CommandLine Arguments
public class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("Addition is "+(z));
}
}
Output:
Addition is 30
Using Scanner Class
import java.util.*;
public class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
z=x+y;
System.out.println("Addition is "+(z));
}
}
Output:
Enter x,y values
12
12
Addition is 24
More Questions
6 . Write a java-program to print addition of two numbers
7 . Write a Java-program to print subtraction of two numbers
8 . Write a Java-program to print multiplication of two numbers
9 . Write a Java-program to print division of two numbers
10 . Write a Java-program to print modular division of two numbers
11 . Write a java-program to print swaping of two numbers with using third variable
12 . Write a Java-program to print swaping of two numbers without using third variable
13 . Write a java-program to print relation of two numbers
14 . Write a Java-program to print logical operators program using &&, || ,!
15 . Write a Java-program to print bitwise AND operation
16 . Write a Java-program to print bitwise OR operation