Write a Java program to print the sum of two numbers.
//Method-1
public class Add{
public static void main (String[] args){
System.out.println (10+20);
}
}
Save this file as 'Add.java'.
Compile: $javac Add.java
Execute: $java Add
Output:
30
//Method-2
public class Add1{
public static void main (String[] args){
int num1 = 10;
int num2 = 20;
System.out.println ("First number : "+num1);
System.out.println("Second number : "+num2);
//compute sum
int sum = num1 + num2;
System.out.println("\nAddition of "+num1+" and "+num2+" is : "+sum);
}
}
Save this file as 'Add1.java'.
Compile: $javac Add1.java
Execute: $java Add1
Output:
First number : 10
Second number : 20
Addition of 10 and 20 is : 30
Exercise:
1. Write a Java program to print the subtraction of two numbers.
2. Write a Java program to multiply two numbers.
3. Write a Java program to print the division of two numbers.
4. Write a Java program to compute the sum and average of three integer numbers.
Java Hello World Program
How To Print Multiple Strings Using Single Print Command In Java?
Java Hello World Program
How To Print Multiple Strings Using Single Print Command In Java?
0 Comments