Java Program to find remainder without using the modulus operator


In this program, You will learn how to find the remainder without using a modulus operator in java.


0 = 10 % 5

5 = 5 % 10

Example: How to find the remainder without using modulus operator in java

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        int a,b,q=0;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter two numbers:");
        a = sc.nextInt();
        b = sc.nextInt();

        while(a >= b){
            a = a - b;
        }

        System.out.println("Remainder is:"+a);

    }
}

Output:

Enter two numbers:10 3
Remainder is:1