Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to read the ballots and count the votes cast for each candidate using an array variable count

Posted By: Easy Tutor     Category: Java     Views: 7630

An election is contested by 5 candidates. The candidates are numbered 1 to 5 and the voting is
done by marking the candidate number on the ballot paper. Write a 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 'spoil ballot' and the program should also count
the number of split ballots.

Code for Program to read the ballots and count the votes cast for each candidate using an array variable count in Java

import java.io.*;

class  Election
{
    publicstaticvoid main(String args[]) 
    {
         int Candidate[]=newint[6];
        int SpoiltBallot=0;
        int voters;
        int check=0,winner=1,badlooser=1;
        String str;

        try{
             BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
             System.out.println("\n=====ELECTION BOARD=====\n");
             System.out.print("\nEnter number of voters appeared for voting : ");
             System.out.flush();
             str=obj.readLine();
             voters=Integer.parseInt(str);

    loop: for(int i=1;i<=voters;i++)
             {
                    System.out.print("\nVote given by voter("+i+") to Candidate Id : ");
                    System.out.flush();
                    str=obj.readLine();
                    check=Integer.parseInt(str);

                    if (check<1 && check>5)
                    {
                            SpoiltBallot=SpoiltBallot+1;
                            continue loop;
                    }
                    else
                    {
                            Candidate[check]=Candidate[check]+1;
                    }
            }

            System.out.println("\nVOTING RESULT ARE AS UNDER");
            for(int i=1;i<=6;i++)
            {
                 System.out.println("CANDIDATE ID : "+i+" has recieved  "+Candidate[i]+" Votes");
            }

            for(int i=1;i<=5;i++)
            {
                 for(int j=1;j<=5;j++)
                 {
                         if(i==1)
                          {
                             winner=i;
                             badlooser=i;
                          }

                          if(Candidate[i]<Candidate[j])
                          {
                                winner=j;
                          }

                          if(Candidate[i]>Candidate[j])
                          {
                                badlooser=j;
                          }
                }
            }
        //    System.out.println("\nWinner is Candidate "+winner+" recieved "+Candidate[winner]+" votes.");//    System.out.println("Candidate "+badlooser+" recieved the lowest votes that is "+Candidate[badlooser]);//    System.out.println("Spoilt Ballot are : "+SpoiltBallot);
        }
        catch(Exception e)
        {}
    }
}

  
Share: 



Easy Tutor
Easy Tutor author of Program to read the ballots and count the votes cast for each candidate using an array variable count is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!