C++ Program to Check if a Number is Divisible by Second Number

«
»
This C++ Program which checks if a number is divisible by second number. The program takes two numbers as the input and uses modulus operator to determine whether the first number is completely divisible by second number.

Here is source code of the C++ program which checks if a number is divisible by second number. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Check if a Number is Divisible By Second Number
  3.  */
  4.  
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int first, second;
  11.  
  12.     cout << "Enter the numbers : ";
  13.     cin >> first >> second;
  14.  
  15.     if (first % second == 0)
  16.         cout << "First number " << first
  17.              << " is divisible by second number "
  18.              << second;
  19.     else
  20.         cout << "First number " << first 
  21.              << " is not divisible by second number "
  22.              << second;
  23. }

$ g++ main.cpp
$ ./a.out
Enter the numbers : 34 17
First number 34 is divisible by second number 17
$ ./a.out
Enter the numbers : 104 12
First number 104 is not divisible by second number 12

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.