C++ Program to check whether a number is divisible by another number

This example provides you with a program in C++ that is used to check whether a number (entered by the user) is divisible by another number (also entered by the user) or not.

How can we check if a Number is Divisible by Another?

A number is divisible by another number if it can be divided equally by that number; that is if it yields a whole number when divided by that number. For example, 6 is divisible by 3 (we say "3 divides 6") because 6/3 = 2, and 2 is a whole number. 6 is not divisible by 4, because 6/4 = 1.5, and 1.5 is not a whole number. Or check whether the second number divides first without leaving any remainder or not.

C++ program to check  Number is Divisible by Another using if-else

Here we are going to check whether the first number % and the second number is 0 or not. If the answer is 0 then the number is divisible by another number. Else it is not divisible by that number.
Ask the user to enter two numbers. The first one is the numerator and the second one is the denominator.
Get the numbers into the variables nume and denom.
Use an if-else statement to check for the %.



The above program code checks whether the denom divides the nume without leaving any remainder, or leaving 0 as remainder or not. The % operator gives the remainder therefore if nume?nom gives means that denom divides nume without leaving a remainder.


Step 1: Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare two integer type variables nume and denom;

Step 5: Ask the user to enter a number;

Step 6: Get the number into the variable nume;

Step 7: Ask the user to enter the second number;

Step 8: Get the number into the variable denom;

Step 9: Check if nume?nom is 0;

Step 10: If yes then print the first number is divisible by the second number;

Step 11: else print the first number is not divisible by the second number

Step 12: Exit

C++ Source Code

using namespace std;
int main()
   int nume, denom;
   cout<<"Enter a Number (Numerator): ";
   cout<<"Enter another Number (Denominator): ";
      cout<<endl<<nume<<" is divisible by "<<denom;
      cout<<endl<<nume<<" is not divisible by "<<denom;
   return 0;


Run 1
Enter a Number (Numerator): 85
Enter another Number (Denominator): 5
85 is divisible by 5
Run 2
Enter a Number (Numerator): 6
Enter another Number (Denominator): 5
6 is not divisible by 5