What is Loop
Loop is used to reuse any code again and again there are three types of loop in C
For loop
While loop
Do while loop
For loop
While loop
Do while loop
For loop
For initialize the value a=2
check the condition a<=10
and inc/dec the value a++/a--
Syntax:-
For(initialization;condition;inc/dec)
{
statement
}
check the condition a<=10
and inc/dec the value a++/a--
Syntax:-
For(initialization;condition;inc/dec)
{
statement
}
Q-1.WAP which display 10 times "Hello"
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
printf("Hello");
}
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
printf("Hello");
}
getch();
}
Q-2.WAP which display 1-10
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
printf("%d\n",a);
}
getch();
}
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
printf("%d\n",a);
}
getch();
}
Q-3.WAP which accept end limit from the user and display 1 to n
#include<stdio.h>
#include<conio.h>
void main()
{
int a,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
printf(%d\n",a);
}
getch();
}
#include<conio.h>
void main()
{
int a,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
printf(%d\n",a);
}
getch();
}
Q-4.WAP which accept start and end limit from the user and display n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
printf(%d\n",n1);
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
printf(%d\n",n1);
}
getch();
}
Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
printf(%d\n",n1);
}
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
printf(%d\n",n1);
}
}
getch();
}
Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
}
getch();
}
Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
sum=sum+n1;
}
printf("sum=%d",sum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
sum=sum+n1;
}
printf("sum=%d",sum);
getch();
}
Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,esum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
}
printf("even sum=%d",esum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,esum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
}
printf("even sum=%d",esum);
getch();
}
Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,osum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
osum=osum+n1;
}
}
printf("odd sum=%d",osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,osum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
osum=osum+n1;
}
}
printf("odd sum=%d",osum);
getch();
}
Q-10.WAP which accept start and end limit from the user and display total,even and odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
}
sum=esum+osum;
printf("total sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
}
sum=esum+osum;
printf("total sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}
While loop
while check the condition and then execute.
Syntax:-
while(condition)
{
statement
}
Syntax:-
while(condition)
{
statement
}
Q-1.WAP which display 10 times "Hello"
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf("Hello");
a++;
}
getch();
}
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf("Hello");
a++;
}
getch();
}
Q-2.WAP which display 1-10
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf("%d\n",a);
a++;
}
getch();
}
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf("%d\n",a);
a++;
}
getch();
}
Q-3.WAP which accept end limit from the user and display 1 to n
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
while(a<=n)
{
printf(%d\n",a);
a++;
}
getch();
}
#include<conio.h>
void main()
{
int a=1,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
while(a<=n)
{
printf(%d\n",a);
a++;
}
getch();
}
Q-4.WAP which accept start and end limit from the user and display n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
printf(%d\n",n1);
n1++;
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
printf(%d\n",n1);
n1++;
}
getch();
}
Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
printf(%d\n",n1);
}
n1++;
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
printf(%d\n",n1);
}
n1++;
}
getch();
}
Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
n1++;
}
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
n1++;
}
getch();
}
Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
sum=sum+n1;
n1++;
}
printf("sum=%d",sum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
sum=sum+n1;
n1++;
}
printf("sum=%d",sum);
getch();
}
Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,esum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
printf("even sum=%d",esum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,esum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
printf("even sum=%d",esum);
getch();
}
Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2!=0)
{
osum=osum+n1;
}
n1++;
}
printf("odd sum=%d",osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2!=0)
{
osum=osum+n1;
}
n1++;
}
printf("odd sum=%d",osum);
getch();
}
Q-10.WAP which accept start and end limit from the user and display total,even and odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
sum=esum+osum;
printf("sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
sum=esum+osum;
printf("sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}
Do While loop
Do while execute first and then check the condition.
Syntax:-
do
{
statement
}
while(condition);
Syntax:-
do
{
statement
}
while(condition);
Q-1.WAP which display 10 times "Hello"
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
do
{
printf("Hello");
a++;
}
while(a<=10);
getch();
}
#include<conio.h>
void main()
{
int a=1;
clrscr();
do
{
printf("Hello");
a++;
}
while(a<=10);
getch();
}
Q-2.WAP which display 1-10
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
do
{
printf("%d\n",a);
a++;
}
while(a<=10);
getch();
}
#include<conio.h>
void main()
{
int a=1;
clrscr();
do
{
printf("%d\n",a);
a++;
}
while(a<=10);
getch();
}
Q-3.WAP which accept end limit from the user and display 1 to n
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
do
{
printf("%d\n",a);
a++;
}
while(a<=n);
getch();
}
#include<conio.h>
void main()
{
int a=1,n;
clrscr();
printf("Enter limit=");
scanf("%d",&n);
do
{
printf("%d\n",a);
a++;
}
while(a<=n);
getch();
}
Q-4.WAP which accept start and end limit from the user and display n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
printf(%d\n",n1);
n1++;
}
while(n1<=n2);
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
printf(%d\n",n1);
n1++;
}
while(n1<=n2);
getch();
}
Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
printf(%d\n",n1);
}
n1++;
}
while(n1<=n2);
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
printf(%d\n",n1);
}
n1++;
}
while(n1<=n2);
getch();
}
Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
n1++;
}
while(n1<=n2);
getch();
}
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2!=0)
{
printf(%d\n",n1);
}
n1++;
}
while(n1<=n2);
getch();
}
Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
sum=sum+n1;
n1++;
}
while(n1<=n2);
printf("sum=%d",sum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
sum=sum+n1;
n1++;
}
while(n1<=n2);
printf("sum=%d",sum);
getch();
}
Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,esum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
while(n1<=n2);
printf("even sum=%d",esum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,esum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
while(n1<=n2);
printf("even sum=%d",esum);
getch();
}
Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
printf("odd sum=%d",osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
printf("odd sum=%d",osum);
getch();
}
Q-10.WAP which accept start and end limit from the user and display total,even and odd sum of n1 to n2
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
sum=esum+osum;
printf("sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}
#include<conio.h>
void main()
{
int n1,n2,sum=0,esum=0,osum=0
clrscr();
printf("Enter start and end limit=");
scanf("%d%d",&n1,&n2);
do
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
sum=esum+osum;
printf("sum=%d\neven sum=%d\nodd sum=%d",sum,esum,osum);
getch();
}