Group
Flow Control in C++
Objective
1. Ask the user to input a number.
2. Use the modulus operator to check if the number is divisible by both 5 and 3.
3. Display a message indicating whether the number is divisible by both 5 and 3 or not.
4. Handle both positive and negative inputs.
Write a program that checks if a number is divisible by both 5 and 3 simultaneously.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Main function - the entry point of the program
int main() {
int number; // Variable to store the user's input
// Ask the user for input
cout << "Enter a number: ";
cin >> number; // Read the input number
// Check if the number is divisible by both 5 and 3
if (number % 5 == 0 && number % 3 == 0) { // If the number is divisible by both
cout << "The number is divisible by both 5 and 3." << endl;
}
else { // If the number is not divisible by both
cout << "The number is NOT divisible by both 5 and 3." << endl;
}
return 0; // Return 0 to indicate that the program executed successfully
}
Output
Enter a number: 15
The number is divisible by both 5 and 3.
//Or for a number that is not divisible by both:
Enter a number: 7
The number is NOT divisible by both 5 and 3.