Question

Write a program that allows the user to enter the last names of five candidates in...

Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate’s name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follows:

Candidate
Johnson
Miller
Duffy
Robinson
Sam
Votes Received
  5000
  4000
  6000
  2500
  1800
  19300

%

of Total Votes 25.91
20.72
31.09

12.95 9.33

Total
The Winner of the Election is Duffy.

Homework Answers

Answer #1

#include <stdio.h>
int main(void){
char can[5][100];
int i=0,vote[5],sum=0,x,pos=0;
printf("Enter 5 Candidates names\n");
for(i=0; i<5; i++){
scanf("%s[^\n]",can[i]);}
printf("Enter 5 Votes Recieved\n");
for(i=0; i<5; i++){
scanf("%d",&vote[i]);
  
}
printf("Candidate\n");

for(i=0;i<5;i++){
puts(can[i]);
  
}
printf("Votes Received\n");
for(i=0;i<5;i++){
printf("%d\n",vote[i]);
sum=sum+vote[i];
}

printf("%% of Total votes\n");
  
x=vote[0];
pos=0;

for(i=0; i<5; i++){
double x=vote[i]*100.0/sum;
printf("%.2lf\n",x);
}

for(i=0;i<5;i++){
if(vote[i]>x){
pos=i;
}
}
printf("The winner of the election is ");
puts(can[pos]);

  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT