Ask Question

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. Sample output is as follows:Candidate Names Number Of Votes Recieved Percentage Of The Total Votes RecievedJohnson 5000 25.91Miller 4000 20.72Duffy 6000 31.09Robinson 2500 12.95Sam 1800 9.33Total 19300

+5
Answers (1)
  1. 16 January, 04:31
    0
    Explanation & Answer:

    //written in java

    import java. util.*;

    public class Election{

    public static void main (String[] args) {

    String[] candidate = new String[5];

    int[] votes = new int[5];

    double percent;

    int i, sum = 0, highest = 0, winner = 0;

    Scanner s = new Scanner (System. in);

    //get input

    for (i = 0; i < 5; i++) {

    System. out. print ("Enter last name of Candidate " + (i + 1) + ":");

    candidate[i] = s. next ();

    System. out. print ("Enter number of votes received: ");

    votes[i] = s. nextInt ();

    if (votes[i] > highest) {

    highest = votes[i];

    winner = i;

    }//if

    sum + = votes[i];

    System. out. println ();

    }//for

    //generate output

    System. out. println ("The results are:");

    System. out. println ("Candidate/tVotes Received/t% of TotalVotes/n");

    for (i = 0; i < 5; i++) {

    percent = (((float) votes[i]) * 100) / sum;

    System. out. println (candidate[i] + "/t/t" + votes[i] + "/t/t/t" + String. format ("%.2f", percent));

    }//for

    System. out. println ("/nTotal:/t" + sum);

    System. out. println ("The winner of the Election is: " + candidate[winner]);

    }//main

    }//Election
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers