This is a C program that implements a simple voting system. It includes the following steps:
- Preprocessor directives define the names of four candidates and an empty vote option.
- Global variables are initialized to store the number of votes cast for each candidate and for empty votes.
- A function named
castVote()
is defined that asks the user to select a candidate and increments the corresponding vote count. - A function named
votesCount()
is defined that displays the total number of votes for each candidate and for empty votes. - A function named
getLeadingCandidate()
is defined that displays the name of the candidate with the highest number of votes. - In the
main()
function, a menu is displayed that allows the user to select one of three options: vote, view vote statistics, or view the leading candidate. The program loops until the user chooses to exit. - Depending on the user’s selection, the corresponding function is called.
- When the user chooses to exit, the program thanks them and terminates.
Here’s the code with comments:
#include <stdio.h>
// Define the names of the candidates and the number of votes each candidate has
#define CANDIDATE1 "Rahul Singh"
#define CANDIDATE2 "Shyamlal Banerjee"
#define CANDIDATE3 "Surendra Kumar"
#define CANDIDATE4 "Mukul Singh"
// Initialize the number of votes for each candidate and for "None of the Above" (NOTA)
int num_of_votes_C1 = 0, num_of_votes_C2 = 0, num_of_votes_C3 = 0, num_of_votes_C4 = 0, nota = 0;
// Function to allow the user to cast their vote
void castVote() {
int choice;
// Display the candidates and NOTA option
printf("\n\nPlease Select Candidate by selecting option between 1 - 5\n\n");
printf("\n '1' for %s", CANDIDATE1);
printf("\n '2' for %s", CANDIDATE2);
printf("\n '3' for %s", CANDIDATE3);
printf("\n '4' for %s", CANDIDATE4);
printf("\n '5' for %s", "NOTA: None of the Above");
// Prompt the user to enter their choice
printf("\n\nEnter your choice between 1 - 5 : ");
scanf("%d", &choice);
// Increment the number of votes for the selected candidate or NOTA
switch (choice) {
case 1:
num_of_votes_C1++;
break;
case 2:
num_of_votes_C2++;
break;
case 3:
num_of_votes_C3++;
break;
case 4:
num_of_votes_C4++;
break;
case 5:
nota++;
break;
default:
printf("\nPlease Select an option between 1 - 5 Only");
getchar();
}
// Display a message thanking the user for their vote
printf("\n Thanks for your Valuable Vote !");
}
// Function to display the total number of votes for each candidate and NOTA
void votesCount() {
printf("\n\n Number of Votes are: ");
printf("\n %s: %d ", CANDIDATE1, num_of_votes_C1);
printf("\n %s: %d ", CANDIDATE2, num_of_votes_C2);
printf("\n %s: %d ", CANDIDATE3, num_of_votes_C3);
printf("\n %s: %d ", CANDIDATE4, num_of_votes_C4);
printf("\n %s: %d ", "NOTA", nota);
}
// Function to display the leading candidate based on the total number of votes
void getLeadingCandidate(){
printf("\n\nLeading Candidate is ");
if(num_of_votes_C1>num_of_votes_C2 && num_of_votes_C1>num_of_votes_C3 && num_of_votes_C1 >num_of_votes_C4)
printf("%s",CANDIDATE1);
else if (num_of_votes_C2>num_of_votes_C3 && num_of_votes_C2>num_of_votes_C4 && num_of_votes_C2 >num_of_votes_C1)
printf("%s",CANDIDATE2);
else if(num_of_votes_C3>num_of_votes_C4 && num_of_votes_C3>num_of_votes_C2 && num_of_votes_C3 >num_of_votes_C1)
printf("%s",CANDIDATE3);
else if(num_of_votes_C4>num_of_votes_C1 && num_of_votes_C4>num_of_votes_C2 && num_of_votes_C4 >num_of_votes_C3)
printf("%s",CANDIDATE4);
else
printf("Oh No! No Win-Situation");
}
int main(){
int i, choice;
do{
printf("\n\n\n*******************************************************");
printf("\n\n Welcome to Voting Application");
printf("\n\nA Mini Project by Kulbhushan (Krazy Kaksha)");
printf("\n\n 1. Vote a Candidate");
printf("\n 2. View Vote Stats");
printf("\n 3. View leading Candidate");
printf("\n 0. Exit");
printf("\n\n Please select option: ");
scanf("%d", &choice);
switch(choice){
case 0: printf("Thank You! For using this Application");break;
case 1: castVote();break;
case 2: votesCount();break;
case 3: getLeadingCandidate();break;
default: printf("\nPlease enter valid option!");
}
}while(choice!=0);
getchar();
return 0;
}