Skip to main content

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

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

Code:
#include <iostream>
using namespace std;
int main()
{
  
  int ques1=0,ques2=0,ques3=0,ques4=0,num=0;
  cout<<"Enter the no: of desired question "<<endl;
  cout<<"Press 1 for question 1 "<<endl;
  cout<<"Press 2 for question 2 "<<endl;
  cout<<"Press 3 for question 3 "<<endl;
  cout<<"Press 4 for question 4 "<<endl;
  
  cout<<endl;
  for (int i=1;i<=5;i++)
  {
  cout<<"Enter no: of Question = ";
  cin>>num;
  if (num==1)
  {
  ques1=ques1+1;
  }
  else if (num==2)
  {
  ques2+=1;
  }
    else if (num==3)
    {
    ques3+=1;
}
    else if (num==4)
{
ques4+=1;
  }  
    else 
    {
    cout<<"Invalid Entry "<<endl;
}

  }
    cout<<"Number of times you enter question 1 is "<<ques1<<endl;
    cout<<"Number of times you enter question 2 is "<<ques2<<endl;
    cout<<"Number of times you enter question 3 is "<<ques3<<endl;
    cout<<"Number of times you enter question 4 is "<<ques4<<endl;

return 0;

}

Result:



Comments

Popular posts from this blog

C++ = Code to check number is a prime or not. If not find the nearest prime number.

Code:  #include <iostream> using namespace std; bool prime(int n) { if (n<=1) { return false; }        for (int i=2;i<=n/2;i++)    {       if (n%i==0)   {   return false;   }    }         return true; } int nearest(int n) { for (int i=n-1;i>=2;i--) { if (prime(i)) { return i; } } return 0; } int main () { int number; cout<<"Enter any number : "; cin>>number; if (prime(number)) { cout<<"The number is prime. "<<endl; }     else     {     cout<<"The entered number is compositive."<<endl;     cout<<"The nearest prime number will be "<<nearest(number)<<endl; } } Result:

C++ = Code to check number wheater it is prime or not.

Code no 1= Without any other function with using another function. #include <iostream> using namespace std; int main() { int number; bool prime=true; cout<<"Enter any number : "; cin>>number; if (number<=1) { prime=false; } else { for (int i=2;i<number;i++) { if (number%i==0) { prime=false; break; } } } if (prime) { cout<<"The number is a prime number."<<endl; } else { cout<<"The number is not a prime number."<<endl; } } Code no: 2= With using another function #include <iostream> #include <cmath> using namespace std; bool prime(int n) { if (n<=1) { return false; } for (int i=2;i<n/2;i++) { if (n%i==0) { return false; } } return true; } int main() { int number; cout<<"Enter any number : "; cin>>number; if (prime(number)) { cout<<"The numbe...

C++ = Printing diamond with asterik

 CODE: #include <iostream> using namespace std; int main() {     for (int i=1;i<=5;i++)     {     for (int j=1;j<=5-i;j++)     {     cout<<" "; }     for (int k=1;k<=(2*i)-1;k++)     {     cout<<"*"; }   cout<<endl; } for (int i=4;i>=1;i--)     { for (int j=1;j<=5-i;j++)     {     cout<<" "; }     for (int k=1;k<=(2*i)-1;k++)     {     cout<<"*"; }        cout<<endl; } return 0; } RESULT: