Computer Applications

You can multiply two numbers 'm' and 'n' by repeated addition method.
For example, 5 * 3 = 15 can be performed by adding 5 three times ⇒ 5 + 5 + 5 = 15

Similarly, successive subtraction of two numbers produces 'Quotient' and 'Remainder' when a number 'a' is divided by 'b' (a>b).
For example, 5/2 ⇒ Quotient = 2 and Remainder = 1
Follow steps shown below:

ProcessResultCounter
5 - 231
3 - 212

Sample Output: The last counter value represents 'Quotient' ⇒ 2
The last result value represents 'Remainder' ⇒ 1

Write a program to accept two numbers. Perform multiplication and division of the numbers as per the process shown above by using switch case statement.

Java

Java Iterative Stmts

18 Likes

Answer

import java.util.Scanner;

public class KboatArithmetic
{
    public void computeResult() {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int a = in.nextInt();
        System.out.print("Enter second number: ");
        int b = in.nextInt();
        System.out.println("1. Multiplication");
        System.out.println("2. Division");
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        switch (ch) {
            case 1:
            int rMul = 0;
            for (int i = 1; i <= b; i++) {
                rMul += a;
            }
            System.out.println("Multiplication result = " + rMul);
            break;
            
            case 2:
            int count = 0, t = a;
            while (t > b) {
                t -= b;
                count++;
            }
            System.out.println("Divison Result");
            System.out.println("Quotient = " + count);
            System.out.println("Remainder = " + t);
            break;
            
            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }

}

Variable Description Table

Program Explanation

Output

Answered By

6 Likes


Related Questions