In: Computer Science
Please write the code JAVA
Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, 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:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.73
Duffy 6000 31.09
Robinson 2500 12.95
Ashtony 1800 9.33
Total 19300
The Winner of the Election is Duffy.
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
//importing package
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//n is used to store the no. of candidates
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no.of candidates: ");
//asking the user to input the no. of candidates
n = sc.nextInt();
//using three arrays to store names, votes,
percentage
String name[] = new
String[n];
int votes[] = new int[n];
float per[] = new float[n];
//total votes will be stored in
totalvotes
float totalvotes = 0;
//declaring two variables
int max = 0,a = 0;
//for loop will iterate n
times
System.out.println();
for (int i = 0; i < n;
i++)
{
Scanner scnr = new
Scanner(System.in);
System.out.print("Enter candidate
last name: ");
//asking the user to enter name of
the candidate
name[i] = scnr.nextLine();
System.out.print("Enter no. of votes received for this candidate:
");
//asking the user to enter no. of votes they got
votes[i] = scnr.nextInt();
//adding total votes and storing it in totalvotes
totalvotes = totalvotes + votes[i];
System.out.println();
}
//for loop will iterate n
times
for(int j = 0; j < n; j++)
{
//calculating percentage and
storing it in per[]
per[j] =
votes[j]*100/totalvotes;
}
System.out.println("----------------------------------------------------------------");
System.out.println("Name\t\t Votes
Received\t\t % of Total Votes");
System.out.println("----------------------------------------------------------------");
//for loop will iterate n
times
for(int k = 0; k < n; k++)
{
//printing name, votes, percentage
of each candidate
System.out.printf(name[k]+"\t\t\t"+votes[k]+"\t\t\t"+"%.2f",per[k]);
System.out.println();
//to check that who is winner
//initially max = 0 if votes>
max then
if(max<votes[k])
{
//that votes will store in
max
max = votes[k];
//index value of that votes is
stored in a
a = k;
}
}
System.out.println("----------------------------------------------------------------");
//printing totalvotes
System.out.println("Total\t\t\t"+totalvotes);
System.out.println("----------------------------------------------------------------");
//printing the winner name using
index value stored in a
System.out.println("The Winner of
the Election is "+name[a]);
}
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!