Switch case Statement in C language

•Switch case statement:

This control statement , which allows us to make a decision from the number of choice, is called a switch. with the help of switch case statement we may choose any number of decisions, or more correctly a switch-case default, since the combination of these three key words go together to make up the control statement.

Syntax:

switch (expression)
{
     case 1:
     statement (Group 1)
     case 2:
     statement ( Group 2)
     case 3:
     statement ( Group 3)
     default
     statement ( Group 4)
}
The switch statement checks, whether an expression matches with number of integer or character constant.

Example-

For "if - else"
#include<stdio.h>
void main()
{
int age;
printf ("Enter your age");
scanf("%d",&age);
if (age>=18)
printf("you can vote ")
else 
printf ("You can't vote");
}

Example: For "Switch - case"

To calculate simple interest and compound interest.

#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float ,p,r,t,si,ci;
clrscr();
printf ("Enter principal");
scanf("%f,&p);
printf ("Enter rate\n");
scanf ("%f" ,& t);
printf("n1.Simple Interest");
printf("n2.Compound Interest");
printf ("\n Enter your choice \n");
scanf ("%d" , &ch);
switch (ch)
{
case 1:
si=(p*r*t*)/100;
printf ("%f\n" ,si);
break;
case 2:
ci= p((1+r/100)^t-p);
printf ("%f\n",ci);
break;
default:
printf ("wrong choice");
}
getch();
}

Comments

Popular posts from this blog

Remove Tools From Termux | How can I reopen the installed tool in termux? | TECH WORTHY MIND

How To Hack Wifi | Hack Wifi Password | TECH WORTH MIND

Best 250+ termux hacking tools