Write a C program for electing a candidate in Elections by calling functions using Switch case

This C program demonstrates how to create an electronic voting system using functions and switch case statements. The program allows users to cast votes for candidates and view the vote count results.

Syntax

switch(expression) {
    case constant1:
        // statements
        break;
    case constant2:
        // statements
        break;
    default:
        // statements
}

Problem

How to cast vote, count, and display the votes for each candidate that participates in elections using C language?

Solution

Let's consider three persons who participated in elections. Here we need to write a code for the following −

  • Cast vote − Selecting a candidate by pressing the cast vote
  • Find vote count − Finding the total number of votes each candidate gains declaring the winner

Example

All these operations are performed by calling each function using Switch case −

#include<stdio.h>

#define CANDIDATE1 "ABC"
#define CANDIDATE2 "XYZ"
#define CANDIDATE3 "PQR"

int votescount1=0, votescount2=0, votescount3=0;

void castvote(){
    int choice;
    printf("

### Please choose your Candidate ####

"); printf("
1. %s", CANDIDATE1); printf("
2. %s", CANDIDATE2); printf("
3. %s", CANDIDATE3); printf("
4. %s", "None of These"); printf("\nInput your choice (1 - 4) : "); scanf("%d",&choice); switch(choice){ case 1: votescount1++; break; case 2: votescount2++; break; case 3: votescount3++; break; case 4: printf("
You chose None of These"); break; default: printf("
Error: Wrong Choice !! Please retry"); } printf("
Thanks for vote !!"); } void votesCount(){ printf("

##### Voting Statistics ####"); printf("
%s - %d ", CANDIDATE1, votescount1); printf("
%s - %d ", CANDIDATE2, votescount2); printf("
%s - %d ", CANDIDATE3, votescount3); /* Find winner */ if(votescount1 > votescount2 && votescount1 > votescount3) printf("

Winner: %s", CANDIDATE1); else if(votescount2 > votescount1 && votescount2 > votescount3) printf("

Winner: %s", CANDIDATE2); else if(votescount3 > votescount1 && votescount3 > votescount2) printf("

Winner: %s", CANDIDATE3); else printf("

Result: Tie or No votes cast"); } int main(){ int choice; do{ printf("

###### Welcome to Election/Voting 2024 #####"); printf("

1. Cast the Vote"); printf("
2. Find Vote Count"); printf("
0. Exit"); printf("
Please enter your choice : "); scanf("%d", &choice); switch(choice){ case 1: castvote(); break; case 2: votesCount(); break; case 0: printf("
Thank you for using the voting system!"); break; default: printf("
Error: Invalid Choice"); } }while(choice!=0); return 0; }
###### Welcome to Election/Voting 2024 #####

 1. Cast the Vote
 2. Find Vote Count
 0. Exit
 Please enter your choice : 1

 ### Please choose your Candidate ####

 1. ABC
 2. XYZ
 3. PQR
 4. None of These
Input your choice (1 - 4) : 1
 Thanks for vote !!

###### Welcome to Election/Voting 2024 #####

 1. Cast the Vote
 2. Find Vote Count
 0. Exit
 Please enter your choice : 2

 ##### Voting Statistics ####
 ABC - 1 
 XYZ - 0 
 PQR - 0 

 Winner: ABC

###### Welcome to Election/Voting 2024 #####

 1. Cast the Vote
 2. Find Vote Count
 0. Exit
 Please enter your choice : 0
 Thank you for using the voting system!

How It Works

  • The program uses global variables to store vote counts for each candidate
  • The castvote() function displays candidates and records votes using a switch case
  • The votesCount() function displays statistics and determines the winner
  • The main function provides a menu-driven interface using a do-while loop and switch case

Conclusion

This voting system demonstrates practical use of functions, switch case statements, and loops in C. The modular design makes the code easy to understand and extend with additional features like more candidates or voting validation.

Updated on: 2026-03-15T13:40:02+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements