Computer Science, asked by SuryanshRam, 5 hours ago

WAP to accept 2 numbers and stuude the first number with the Display the remainder. Without using % operator second one​

Answers

Answered by anindyaadhikari13
29

\textsf{\large{\underline{Correct Question}:}}

  • WAP to accept two numbers and calculate their remainder without using % operator.

\textsf{\large{\underline{Solution}:}}

Here, I am solving this problem using Java.

import java.util.Scanner;

public class Remainder{

   public static void main(String s[]){

       Scanner sc=new Scanner(System.in);

       int a,b;

       System.out.print("Enter first number: ");

       a=sc.nextInt();

       System.out.print("Enter second number: ");

       b=sc.nextInt();

       sc.close();

       System.out.printf("%d divided by %d leaves %d as remainder.",a,b,a-b*(a/b));

   }

}

\textsf{\large{\underline{Explanation}:}}

There is a formula to calculate remainder!

Suppose that there are two numbers - a and b. Then the remainder will be:

\rm\implies R = a - b\bigg(\dfrac{a}{b}\bigg)

where a/b is an integer division.

Lets see how it works.

When a is divided by b, the result is the quotient obtained after division. So, the remainder part is excluded. Now, if we multiply the same quotient with b, then we will get the value (a - remainder). From here, we can obtain remainder.

Example:

a = 5 and b = 4

>> a/b = 5/4 = 1  (Quotient)

>> b * (a/b) = 4 * 1 = 4

>> a - b * (a/b) = 5 - 4 = 1 which is the remainder.

Attachments:
Similar questions