Home » » Conditional statements in C Programming with Example

Conditional statements in C Programming with Example

Conditional statements in C Programming with Example

Conditional statements in C Programming with Example


Conditional statements

     1. if     2. if else     3. if else if     4. switch statement     5. conditional operator or ternary operator     6. while     7. do while     8. for     9. go to    10. type conversion

If Program :


#include

int main()
{
int a;
printf("Enter the value of a is:");
scanf("%d",&a);
if(a>=18)
{
printf("I am elgible for vote");
}
return 0;
}

Output :


Enter the value of a is:22
I am elgible for vote

If else Program :


else
printf("I am not eligible for vote");
}

Vowels Condition :


if((a=='a')||(a=='e')||(a=='i')||(a=='o')||(a=='u'))

If else if Program :


#include

int main()
{
int a,b,c;
printf("Enter the value of a and b and c is:");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("a is greater");
else if((b>a)&&(b>c))
printf("b is greater");
else
printf("c is greater");
return 0;
}

Output:


Enter the value of a and b and c is:4
4
6
c is greater

Switch Statement Program :


#include

int main()
{
int a;
printf("Enter the value of a is:");
scanf("%d",&a);
switch(a)
{
case 1:
{
int a,b,c,d;
printf("Enter the value of b and c is:");
scanf("%d%d",&b,&c);
d=b+c;
printf("The value of c is %d",d);
break;
}
case 2:
{
int b,c,d;
printf("Enter the value of b and c is:");
scanf("%d%d",&b,&c);
d=b-c;
printf("The value of d is %d",d);
break;
}
case 3:
{
int b,c,d;
printf("Enter the value of b and c is:");
scanf("%d%d",&b,&c);
d=b*c;
printf("The value of d is: %d",d);
break;
}
case 4:
{
int b,c,d;
printf("Enter the value of b and c is:");
scanf("%d%d",&b,&c);
d=b/c;
printf("The value of d is %d",d);
break;
}
default:
{
printf("You are enter the wrong number");
break;
}
return 0;
}
}

Output :

Enter the value of  a is:2                                                                                         
Enter the value of b and c is:23
2
The value of d is 21

Conditional Operator :
Symbol - ?, :

Program :

#include

int main()
{
int a,b,c;
printf("Enter the value of a and b is:");
scanf("%d%d",&a,&b);
c=a>b?a:b;
printf("The value of c is %d",c);
return 0;
}

Output :


Enter the value of a and b is:3
6
The value of c is 6

While Program :


#include

int main()
{
int a;
printf("Enter the a is:");
scanf("%d",&a);
while(a<=10)
{
printf("\n arun");
a++;
}

return 0;
}

Output :

Enter the a is:4                                                                                                     

arun
arun
arun
arun
arun
arun
arun

Do while Program :


#include

int main()
{
int a;
printf("Enter the value of a is:");
scanf("%d",&a);
do
{
printf("\narun");
a++;
}
while(a<=10);
return 0;
}

Output :


Enter the value of a is:3

arun
arun
arun
arun
arun
arun
arun
arun

For
1. intialization
2. conditional
3. increment(or)decrement

Program :


#include

int main()
{
int i,n;
printf("Enter the value of n is:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n%d",i);
}
return 0;
}

Output :


Enter the value of n is:2

1
2

Type conversion : 
1. float-int
2. int-float

Program :

#include

int main()
{
float a,b;
int c;
printf("Enter the value of a and b is:");
scanf("%f%f",&a,&b);
c=(int)a*(int)b;
printf("The value of c is%d",c);
return 0;
}

Output :

Enter the value of a and b is:3
5
The value of c is15

Go to
Program :


#include
#include
void main()
{
int a,b,c;
clrscr();
go to arun;
selva:
printf("The value of c is %d",c);
go to end;
kumar;
c=a+b;
go to selva;
arun;
printf("Enter the value of a and b is :");
scanf("%d%d",&a,&b);
go to kumar;
end:
getch();
}

Output :


Enter the value of a and b is : 2
2
The value of c is 4

0 coment�rios:

Post a Comment

Link you don't want to vouch for Read more: https://html.com/seo/#ixzz5zVltiZ5g