Search This Blog

Thursday 28 January 2016

An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a C++ program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a 'spolit ballot', and the program should also count the number of spoilt ballots.

#include<iostream>

using namespace std;

int main()
{
int n,x=0,i,ch,one=0,two=0,three=0,four=0,five=0;
cout<<"Enter total num of students";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"\nEnter votes for \n1 \n2 \n3 \n4 \n5\nEnter vote for:";
cin>>ch;

switch(ch)
{
case 1:one++;
break;

case 2:two++;
break;

case 3:three++;
break;

case 4:four++;
break;

case 5:five++;
break;

default:x++;
}

}


cout<<"\nvotes given to person 1 is:"<<one<<endl;
cout<<"\nvotes given to person 2 is:"<<two<<endl;
cout<<"\nvotes given to person 3 is:"<<three<<endl;
cout<<"\nvotes given to person 4 is:"<<four<<endl;
cout<<"\nspoiled vote are:"<<x;

return 0;
}

5 comments: