6. Program that will ask the user a question with four possible answers. The program should output the number of times each answer was selected.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 6:

Write a program that will ask the user a question with four possible answers. The question should be asked 20 times. After all the input is gathered, the program should output the number of times each answer was selected.

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int n=1, num, num_1=0, num_2=0, num_3=0, num_4=0;
    cout<<"One execution of a loop is known as:\n 1.Cycle \t2.Duration \t3.Iteration \t4.Test";
    while(n<=20)
    {
        cout<<"\nEnter your choice:";
        cin>>num;
        if(num==1)
        num_1++;
        else if(num==2)
        num_2++;
        else if(num==3)
        num_3++;
        else if(num==4)
        num_4++;
        else
        cout<<"\nInvalid input.";
        n++;
    }
    cout<<"\nThe choice 1 is selected "<<num_1<<" times.";
    cout<<"\nThe choice 2 is selected "<<num_2<<" times.";
    cout<<"\nThe choice 3 is selected "<<num_3<<" times.";
    cout<<"\nThe choice 4 is selected "<<num_4<<" times.";
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
5. Program to find the largest, smallest, and average of n whole numbers.

Comments