KnowledgeBoat Logo

Computer Applications

Write a menu driven program to input two positive numbers m and n (where m>n) and perform the following tasks:

(a) Find the sum of two numbers without using '+' operator.

(b) Find the product of two numbers without using '*' operator.

(c) Find the quotient and remainder of two numbers without using '/' and '%' operator.

[Hint: The last value obtained after each subtraction is the remainder and the number of iterations results in quotient.]

Sample Input: m=5, n=2
5 - 2 =3
3 - 2 = 1, thus Quotient = 2 and Remainder = 1

Java

Java Iterative Stmts

ICSE

39 Likes

Answer

import java.util.Scanner;

public class KboatNumberOperations
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. Sum without '+' operator");
        System.out.println("2. Product without '*' operator");
        System.out.println("3. Quotient and Remainder without '/' & '%' operators");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        System.out.print("Enter m: ");
        int m = in.nextInt();
        System.out.print("Enter n: ");
        int n = in.nextInt();
        
        if (m > n) {
            switch (choice) {
                case 1:
                    while (n > 0) {
                        m++;
                        n--;
                    }
                    System.out.println("Sum = " + m);
                    break;
                
                case 2:
                    int p = 0;
                    while (n > 0) {
                        p += m;
                        n--;
                    }
                    System.out.println("Product = " + p);
                    break;
                
                case 3:
                    int q = 0;
                    while (m >= n) {
                        m = m - n;
                        q++;
                    }
                    System.out.println("Quotient = " + q);
                    System.out.println("Remainder = " + m);
                    break;
                
                default:
                    System.out.println("Incorrect Choice");
                    break;
            }
        }
        else {
            System.out.println("Invalid Inputs");
        }
    }
}

Output

BlueJ output of Write a menu driven program to input two positive numbers m and n (where m>n) and perform the following tasks: (a) Find the sum of two numbers without using '+' operator. (b) Find the product of two numbers without using '*' operator. (c) Find the quotient and remainder of two numbers without using '/' and '%' operator. [Hint: The last value obtained after each subtraction is the remainder and the number of iterations results in quotient.] Sample Input: m=5, n=2 5 - 2 =3 3 - 2 = 1, thus Quotient = 2 and Remainder = 1BlueJ output of Write a menu driven program to input two positive numbers m and n (where m>n) and perform the following tasks: (a) Find the sum of two numbers without using '+' operator. (b) Find the product of two numbers without using '*' operator. (c) Find the quotient and remainder of two numbers without using '/' and '%' operator. [Hint: The last value obtained after each subtraction is the remainder and the number of iterations results in quotient.] Sample Input: m=5, n=2 5 - 2 =3 3 - 2 = 1, thus Quotient = 2 and Remainder = 1BlueJ output of Write a menu driven program to input two positive numbers m and n (where m>n) and perform the following tasks: (a) Find the sum of two numbers without using '+' operator. (b) Find the product of two numbers without using '*' operator. (c) Find the quotient and remainder of two numbers without using '/' and '%' operator. [Hint: The last value obtained after each subtraction is the remainder and the number of iterations results in quotient.] Sample Input: m=5, n=2 5 - 2 =3 3 - 2 = 1, thus Quotient = 2 and Remainder = 1

Answered By

18 Likes


Related Questions