Press "Enter" to skip to content

C- Switch Case (Calculator)

In C language , There are some term  we use when we define the case using with the Switch Case construction. When we press the these terms , Cases will be happen in the defined case.

Now , I will make a program, program will be get user for library. When the user press + , that user will enter the library and when the user press – , user will be out of the library and program will count the total user number and will press the screen. Code Syntax;

#include<stdio.h>
int main()
{
int enter;
int user=0;
// enter is the term which i will user for switch
printf("**Welcome To The Library**");
printf("Number Of The Active User : %d \n\n",user);
while(1)
{
printf("For The Enter Library Press 1 And For The Out Press 2 : ");
scanf("%d",&enter);
switch(enter)
{
case 1 : {
printf("Welcome");
user=user+1;
printf("Number Of The Active User : %d \n\n",user);
break;}
case 2 : {
printf("Bye Bye");
user=user-1;
printf("Number Of The Active User : %d \n\n",user);
break;
}
default :{
printf("Wrong Entered!!");
break;
}
}
}
return 0;
}

We can make the same program using if else instead of the switch case . It’s entirely your choice.

Screen Output;

**Welcome To The Library**
Number Of The Active User : 0

For The Enter Library Press 1 And For The Out Press 2 : 1
Welcome
Number Of The Active User : 1

For The Enter Library Press 1 And For The Out Press 2 : 1
Welcome
Number Of The Active User : 2

For The Enter Library Press 1 And For The Out Press 2 : 2
Bye ByeNumber Of The Active User : 1

Now i will make calculator using with the Switch Case . Making a calculator actually very simple . Also you can do it using with the if else. Code Syntax;

#include<stdio.h>
int main()
{
int number1 , number2 , process;
printf("*/*Welcome To The Calculator*/*\n");	
printf("1:Addition\n2:Subtraction\n3:Multiplication\n4:Division\n");
scanf("%d",&process);
printf("Enter the First Number:");
scanf("%d",&number1);
printf("Enter The Second Number:");
scanf("%d",&number2);
switch(process){
case 1 : {
printf("Result : %d ", number1+number2);
break;
}
case 2 :{
printf("Result : %d ", number1-number2);
break;
}
case 3: {
printf("Result : %d ", number1*number2);
break;
}
case 4 :{
printf("Result : %d ", number1/number2);
break;
}
default :{
printf("Wrong Entered . Please Try Again!!");
break;
}
}		
return 0;
}

Screen Output;

*/*Welcome To The Calculator*/*
1:Addition
2:Subtraction
3:Multiplication
4:Division
3
Enter the First Number:42
Enter The Second Number:3
Result : 126

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *