
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Write a C program for electing a candidate in Elections by calling functions using Switch case
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 CANDIDATE_COUNT #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("
Input your choice (1 - 4) : “); scanf("%d",&choice); switch(choice){ case 1: votescount1++; break; case 2: votescount2++; break; case 3: votescount3++; break; default: printf("
Error: Wrong Choice !! Please retry"); //hold the screen getchar(); } printf(“
thanks for vote !!"); } void votesCount(){ printf("
##### Voting Statics ####"); printf("
%s - %d ", CANDIDATE1, votescount1); printf("
%s - %d ", CANDIDATE2, votescount2); printf("
%s - %d ", CANDIDATE3, votescount3); } int main(){ int i; int choice; do{ printf("
###### Welcome to Election/Voting 2019 #####"); 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; default: printf("
Error: Invalid Choice"); } }while(choice!=0); //hold the screen getchar(); return 0; }
Output
###### Welcome to Election/Voting 2019 ##### 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 2019 ##### 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 2019 ##### 1. Cast the Vote 2. Find Vote Count 0. Exit Please enter your choice : 2 ##### Voting Statics #### ABC - 2 XYZ - 0 PQR - 0 ###### Welcome to Election/Voting 2019 ##### 1. Cast the Vote 2. Find Vote Count 0. Exit Please enter your choice :
- Related Articles
- Write a C program of library management system using switch case
- Java program to generate a calculator using the switch case
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- Haskell Program to Make a Simple Calculator Using switch...case
- Using range in switch case in C/C++
- C program to find the areas of geometrical figures using switch case
- Calling virtual functions inside constructors in C++
- C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
- Switch case statement in C
- Write a C program to work on statements using functions and loops
- Converting digits to word format using switch case in C language
- Explain nested switch case in C language
- C program to print area of triangle, square, circle, rectangle and polygon using switch case.
- What is difference between using if/else and switch-case in C#?

Advertisements