An election is contested by 5 caddidates.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 ‘spoilt’ and the program should also count the number of spoilt ballots


import java.io.*;
class election
{
            public static void main(String args[])throws
            IOException
            {
                        DataInputStream in = new
                        DataInputStream(System.in);

                        int i,n,a=0,spoilt=0;
                        System.out.print("Enter number of ballots: ");
                        n = Integer.parseInt(in.readLine());
                        System.out.println();
                        int count[] = new int[n];
                        for(i=0;i

                        {
                                    System.out.print("Enter Vote: ");
                                    count[i] = Integer.parseInt(in.readLine());
                        if( count[i] == 1 || count[i] == 2 || count[i] == 3 || count[i] == 4 || count[i] ==5)
                                    a++;
                                    else
                                    {
                                                System.out.println("\t\tOutside the range");
                                                spoilt++;
                                    }
                         }
                        System.out.println("bollte paper: " + a);
                        System.out.println("spoilt parep: " + spoilt);
    }
}

0 comments: