Share this post on:

Now let see what and how if else and switch statement is used in C Programming.

if else : This format is also used for to execute the statement solution of complex nested if structure.

The general format is of this statement is :

if(conditions)

statement;

else if(condition)

statement;

else if(condition)

statement;

else

statement;

Question : Write a Program to find the largest value among 4 in C language?

#include<stdio.h>

void main()

{  

int a,b,c,d,max;   

printf(“Enter four no.”);   

scanf(“%d%d%d%d,&a,&b,&c,&d”);   

if(a>b) && (a>c)  && (a>d)   

max=a;   

else if((b>a) && (b>c) && (b>d))   

max=b;   

else if((c>a) && (c>b) && (c>d))   

max=c;   

else   

max=d;   

printf(“%d”,max);

}

Question: Write a Program to find Factorial of a Number in C language?

int main()
{
int n,c,i=1;
printf(“enter the number\n”);
scanf(“%d”,&n);
if(n<=0)
i=1;
else
{
for(c=1;c<=n;c++)
{
i=i*c;
}
}
printf(“factorial is\n%d”);
return 1;
}

Question : Write a Program to input the marks of three subjects and calculate total obtained marks, percentage and division.

#include<stdio.h>

void main(){       

int maths,phy,eng, T.M;   

float per;   

printf(“Enter the marks of the sub”);   

scanf(“%d%d%d”,&maths,&phy,&eng);   

T.M=maths+phy+eng;   

per=((T.M *100)/300);   

printf(“Total marks obtained = %d”,per);   

printf(“percentage = %d”,per);   

if(per >= 60)   

printf(“1st Div”);   

else if(per>=45)   

printf(“2nd Div”);   

else if(per >= 30)   

printf(“3rd Div”);   

else  

printf(“Fail”);}

Switch Statement : Switch is also a selective statement used to compare one value with multiple constant value.

The General format is:

switch(expression)

{

case value:

statement;

break;

case value:

statement;

break;

default;

statement;

}

Question : Write a program to demonstrate switch statement in C language?

#include<stdio.h>

void main(){      

int num;  

printf(“Enter a no.”);  

scanf(“%d”,&num);  

switch(num)  

{      

case 5:      

printf(“five”);      

break;      

case 10:     

 printf(“Ten”);      

break;      

case 15:      

printf(“fifteen”);      

break;      

default;      

printf(“Bye”)  

}

}

Some Important points related to Switch statement in C Programming:

  • Switch is also a selective statement useful to compare one value with multiple constant value.
  • Data type of switch either in the expression or as a case value must be int or char.
  • Case value must be a constant value. we can’t use variable as a case value.
  • we can’t use same base value more than once in a single switch.
  • we can’t use same base value more than once in a single switch.
  • we can’t use string as a case value.
  • we can’t use any relational or logical operator as case value.
  • In switch variable used in the expression will be compare with case value from top to bottom, a soon as it found the first matched case value, the body associated with that case value will be executed and this execution will be continued until it get if statement or end of the switch.
  • If there is no any matched case found, default will be executed if it is given.
  • Default is also optional in switch, we can also write default in between two case value but t is executed at last.

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

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