In the world of programming, learning basic tasks like adding, mutiplying, deviding two numbers is a starting point for every beginner. So, in this post, we are going to explore how to perform all arithmetic operations using two numbers in Java, with a step-by-step explanation of the program. But before delving into that, you need to understand what the Arithmetic Operators in Java are.
- Create a Scanner class object to accept input from the user.
- Accept two numbers from the user using the `Scanner` class object and store the accepted values in the variables called num1 and num2.
- Declare a third variable called add to store the addition of those two numbers.
- Print the addition on the console.
Java Program to Add Two Numbers:
import java.util.Scanner;
public class AddNumbers
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
int add = num1 + num2;
System.out.println("Addition of Given two Numbers is: "+add);
}
}
Enter the first number:
55
Enter the second number:
45
Addition of Given two Numbers is: 100
Java Program to Subtract Two Numbers:
import java.util.Scanner;
public class SubtractNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
int subtract = num1 - num2;
System.out.println("Subtraction of Given two Numbers is: "+subtract);
}
}
Enter the first number:
55
Enter the second number:
45
Subtraction of Given two Numbers is: 10
Java Program to Multiply Two Numbers:
import java.util.Scanner;
public class MultiplyNumbers
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
int multiply = num1 * num2;
System.out.println("Multiplication of Given two Numbers is: "+multiply);
}
}
Enter the first number:
55
Enter the second number:
45
Multiplication of Given two Numbers is: 2475
Java Program to Divide Two Numbers:
import java.util.Scanner;
public class DivideNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
int divide = num1 / num2;
System.out.println("Division of Given two Numbers is: "+divide);
}
}
Enter the first number:
55
Enter the second number:
5
Division of Given two Numbers is: 11
Java Program to Print Remainder of Two Numbers:
import java.util.Scanner;
public class Modulo
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number: ");
int num1 = sc.nextInt();
System.out.println("Enter the second number: ");
int num2 = sc.nextInt();
int mod = num1 % num2;
System.out.println("Remainder of Given two Numbers is: "+mod);
}
}
Enter the first number:
10
Enter the second number:
3
Remainder of Given two Numbers is: 1
No comments:
Post a Comment
Share your thoughts or ask questions below!...👇