write a program in java to find sum , difference, product and division of two numbers
Answers
Required Program :-
public class simplification
{
public static void main ( string args [ ] )
{
int a , b , s , d , p
a = 45 ; b = 20 ;
s = a + b ;
d = a - b ;
p = a * b
System.out.println (" The sum of two numbers = " + s ) ;
System.out.println (" The difference of two numbers = " + d ) ;
System.out.println (" The product of two numbers = " + p ) ;
}
}
Explaination :-
- Line 1: Simplification name has been given because we are performing sum , difference, product and division all these.
- Line 5: a and b are the two numbers inputted for simplification. s is sum , d is difference and p is product.
- Line 6: Value of a we have entered as 45 and of b as 20
- Line 7: Both the numbers a and b are added.
- Line 8: Both the numbers a and b are subtracted i.e., difference.
- Line 9: Product is calculated of a and b.
import java.util.Scanner;
public class Main {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.println("Sum is : " + (num1 + num2));
System.out.println("Difference is : " + (num1 - num2));
System.out.println("Product is : " + (num1 * num2));
System.out.println("Quotient is : " + (num1 / num2));
}
}
- It imports the Scanner class for getting input.
- The name of the class is "Main".
- The scanner class is activated within the main method.
- Now we take input in integers called "num1" and "num2".
- We print sum, difference, product, and quotient on different lines.