Monday, 23 February 2015

Conditional and Looping Structures in C


Conditions:

If, If else, If else if  ..…….

Loops:

For, While, Do-While ………….

Switch (case) Statements;

If Condition:

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j;
clrscr();
printf(“Enter i value:”);
scanf(“%d”,&i);
printf(“Enter j value:”);
scanf(“%d”,&j);
if(i>j)
printf(“I value is greater”);
if(j>i)
printf(“J value is greater”);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&i)
if(i>=18)
{
printf(“Eligible for vote”);
}
if(i<18)
{
printf(“Not eligible for vote”);
}
getch();
}

If else:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
printf(“Enter i value:”);
scanf(“%d”,&i);
printf(“Enter j value:”);
scanf(“%d”,&j);
if(i>j)
printf(“I value is greater”);
else
printf(“J value is greater”);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&i)
if(i>=18)
{
printf(“Eligible for vote”);
}
else
{
printf(“Not eligible for vote”);
}
getch();
}

If else if:

#include<stdio.h>
#include<conio.h>
void main()
{
int input;
clrscr();
printf(“Enter character:”);
input=getchar();
if(input>=’A’ && input<=’Z’)
{
printf(“Upper case character”);
}
else if(input>=’a’ && input<=’z’)
{
printf(“Lower case character’);
}
else if(input>=’0’ && input<=’9’)
{
printf(“Digit”);
}
getch();
}

Switch case statements:

#include<stdio.h>
#include<conio.h>
void main()
{
char choice;
clrscr();
printf(“Enter character:”);
scanf(“%c”,&choice);
switch(choice)
{
case ‘R’:
printf(“\n Red”);
break;
case ‘G’:
printf(”\n Green”);
break;
case ‘Y’:
printf(“\n Yellow”);
break;
default:
pintf(“\n Error”);
break;
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char choice;
clrscr();
printf(“Enter character:”);
scanf(“%s”,&choice);
switch(choice)
{
case ‘R’:
case ‘r’:
printf(“\n Red”);
break;
case ‘G’:
case ‘g’:
printf(”\n Green”);
break;
case ‘Y’:
case ‘y’:
printf(“\n Yellow”);
break;
default:
printf(“\n Error”);
break;
}
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter number:”);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“\n One”);
break;
case 2:
printf(”\n Two”);
break;
case 3:
printf(“\n Three”);
break;
default:
printf(“\n That’s wrong number”);
break;
}
getch();
}

For Loop:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
printf(“Number \t Square(x)\n\n”);
for(i=0;i<=5;i++)
{
printf(“%d\t%d\n”, i, i*i);
}
getch();
}

While Loop:

#include<stdio.h>
#include<conio.h>
void main()
{
int digit=1;
clrscr();
while(digit<=10)
{
printf(“%d\n”,digit++);
}
getch();
}

Do-While:
#include<stdio.h>
#include<conio.h>
void main()
{
int c,nl=0;
clrscr();
do
{
c=getchar();
if(c= =’\n’)
++nl;
}while(c!=EOF);
printf(“The no. of lines entered are:%d\n”,nl);
getch();
}

Nested Loop:

#include<stdio.h>
#include<conio.h>
void main()
{
int multiplier,multiplicand,product;
clrscr();
for(multiplicand=1;multiplicand<=3;multiplicand++)
{
printf(“\n Multiplication table for %d\n”,multiplicand);
printf(“\n******************************\n\n”);
for(multiplier=1;multiplier<=10;multiplier++)
{
product=multiplier * multiplicand;
printf(“\n %d * %d = %d \n”, multiplicand, multiplier, product);
}
getchar();
}
}

While loop:

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,tot,avg,i;
char name[15];
clrscr();
i=1;
while(i<=10)
{
printf(“Enter the name:”);
scanf(“%s”,&name);
printf(“Enter m1,m2,m3:”);
scanf(“%d%d%d”,&m1,&m2,&m3);
tot=m1+m2+m3;
printf(“Total=%d\n”,tot);
avg=tot/3;
printf(“Average=%d”,avg);
if(avg>=80)
{
printf(“A grade”);
}
else if(avg>=60 && avg<=80)
{
printf(“B grade”);
}
else if(avg>=40 && avg<=60)
{
printf(“C grade”);
}
i++;
}
getch();
}

For Loop

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,tot,avg,i;
char name[15];
clrscr();
for(i=0;i<10;i++)
{
printf(“Enter the name:”);
scanf(“%s”,&name);
printf(“Enter m1,m2,m3:”);
scanf(“%d%d%d”,&m1,&m2,&m3);
tot=m1+m2+m3;
printf(“Total=%d\n”,tot);
avg=tot/3;
printf(“Average=%d”,avg);
if(avg>=80)
{
printf(“A grade”);
}
else if(avg>=60 && avg<=80)
{
printf(“B grade”);
}
else if(avg>=40 && avg<=60)
{
printf(“C grade”);
}
}
getch();
}

General

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,tot,avg,i;
char name[15];
clrscr();
printf(“Enter the name:”);
scanf(“%s”,&name);
printf(“Enter m1,m2,m3:”);
scanf(“%d%d%d”,&m1,&m2,&m3);
tot=m1+m2+m3;
printf(“Total=%d\n”,tot);
avg=tot/3;
printf(“Average=%d”,avg);
if(avg>=80)
{
printf(“A grade”);
}
else if(avg>=60 && avg<=80)
{
printf(“B grade”);
}
else if(avg>=40 && avg<=60)
{
printf(“C grade”);
}
getch();
}

Do - While

#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,tot,avg;
char name[15], wish=’y’;
clrscr();
do
{
printf(“Enter the name:”);
scanf(“%s”,&name);
printf(“Enter m1,m2,m3:”);
scanf(“%d%d%d”,&m1,&m2,&m3);
tot=m1+m2+m3;
printf(“Total=%d\n”,tot);
avg=tot/3;
printf(“Average=%d”,avg);
if(avg>=80)
{
printf(“A grade”);
}
else if(avg>=60 && avg<=80)
{
printf(“B grade”);
}
else if(avg>=40 && avg<=60)
{
printf(“C grade”);
}
printf(“\n Do your want to continue:”);
scanf(“%s”,&wish);
}while(wish= =’y’);
getch();
}



No comments:

Post a Comment