Program to display a pesudo random message to a user

This program is about one of the most common uses of the modulus operator. This operator can be used to cycle through various case labels of a switch. For example, modulus of any number with 5 will give one of these five digits only 0, 1, 2, 3 or 4,

Last Reviewed and Updated on May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

Program to display a pesudo-random message to a user

Write a program that asks the user to enter any number of her choice. When she enters her number, your program should use that number in a certain way to display one of these five messages: "Hello", "Hi", "Fine", "OK" and "Nice". The user will have a feeling that she is getting a random message. Use a loop to keep asking her and keep showing her a message each time.

There are five messages, we can use a modulus operator with a switch to show a different message to the user.

int main()
{

    cout << "Enter any number: ";

    int i;

    while(true)
    {

        cin >> i;

        switch(i % 5)
        {

        case 0:
            {

                cout << "Hello" << endl;

            }

            break;

        case 1:
            {

                cout << "Hi" << endl;

            }

            break;

        case 2:
            {

                cout << "Fine" << endl;

            }

            break;

        case 3:
            {

                cout << "OK" << endl;

            }

            break;

        default:
        case 4:
            {

                cout << "Nice" << endl;

            }

            break;

        }

    }

    return 0;

}


My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.



Creative Commons License
This Blog Post/Article "Program to display a pesudo random message to a user" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-05-17.


Advertisement