Multiples of three with do-while loop – C++

What it does:

A simple C++ program which finds the multiples of three. It will prompt the user to enter a number where it will then use an if statement to find out if the number is a multiple of three or not. If it the number is a multiple it will print out that it is and if it is not the program will print out that it is not. Once that is complete the user will be then prompted whether or not they would like to run the program again. This is done using a do-while loop as the program has to run once but maybe repeated a number of unknown times.

Code:


#include <iostream>

using namespace std;

int main()
{
    //declaring variable
    int number;
    int runAgain;

    //welcome msg
    cout << "Welcome to the multiples of three with a do-while loop" << endl;

    do
    {
        //promting user
        cout << "Please enter a number which you would like to check" << endl;

        //reading in user input
        cin >> number;

        if (number % 3 ==0)
        {
            cout << "The number you entered was " << number << " and it a multiple of three" << endl;
        }//if

        else
        {
            cout << "The number you entered was " << number << " and it was not a multiple of three " << endl;
        }//else

        //prompting the user to see if they want to run again
        cout << "Would you like to run the program again?" << endl;
        cout << "1. Yes" << endl;
        cout << "2. No" << endl;

        //reading in their answer
        cin >> runAgain;

    }//do

    while(runAgain!=2);

    cout << "You have choosen to exit. Goodbye!" << endl;

    return 0;
}//main

One thought on “Multiples of three with do-while loop – C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s