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

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
}

Q-1.WAP which display 10 times "Hello"

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
hello()
{
int a;
for(a=1;a<=10;a++)
{
cout<<"Hello"<<endl;
}
}
};
void main()
{
aptech ap;
ap.hello();
getch();
}

Q-2.WAP which display 1-10

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a;
for(a=1;a<=10;a++)
{
cout<<a<<endl;
}
}
};
void main()
{
aptech ap;
ap.series();
getch();
}

Q-3.WAP which accept end limit from the user and display 1 to n

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a,n;
cout<<"Enter limit=";
cin>>n;
for(a=1;a<=n;a++)
{
cout<<a<<endl;
}
}
};
void main()
{
aptech ap;
ap.series()
getch();
}

Q-4.WAP which accept start and end limit from the user and display n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
cout<<n1;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}

Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
cout<<n1;
}
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
cout<<<<n1;
}
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
sum=sum+n1;
}
cout<<sum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,esum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
}
cout<<esum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
if(n1%2!=0)
{
osum=osum+n1;
}
}
cout<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
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<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0,esum=0,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
for(;n1<=n2;n1++)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
}
sum=esum+osum;
cout<<sum<<esum<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


While loop

while check the condition and then execute.

Syntax:-

while(condition)
{
statement
}

Q-1.WAP which display 10 times "Hello"

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
hello()
{
int a=1;
while(a<=10)
{
cout<<"Hello"<<endl;
a++;
}
}
};
void main()
{
aptech ap;
ap.hello();
getch();
}

Q-2.WAP which display 1-10

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a=1;
while(a<=10)
{
cout<<a<<endl;
a++;
}
}
};
void main()
{
aptech ap;
ap.series();
getch();
}

Q-3.WAP which accept end limit from the user and display 1 to n

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a=1,n;
cout<<"Enter limit=";
cin>>n;
while(a<=n)
{
cout<<a<<endl;
a++;
}
}
};
void main()
{
aptech ap;
ap.series()
getch();
}

Q-4.WAP which accept start and end limit from the user and display n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
cout<<n1;
n1++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}

Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
if(n1%2==0)
{
cout<<n1;
}
n1++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
if(n1%2!=0)
{
cout<<<<n1;
}
n1++;
}
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
sum=sum+n1;
n1++;
}
cout<<sum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,esum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
cout<<esum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
if(n1%2!=0)
{
osum=osum+n1;
}
n1++;
}
cout<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
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<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0,esum=0,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
while(n1<=n2)
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
sum=esum+osum;
cout<<sum<<esum<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Do While loop


Do while execute first and then check the condition.

Syntax:-

do
{
statement
}
while(condition);

Q-1.WAP which display 10 times "Hello"

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
hello()
{
int a=1;
do
{
cout<<"Hello"<<endl;
a++;
}
while(a<=10);
}
};
void main()
{
aptech ap;
ap.hello();
getch();
}

Q-2.WAP which display 1-10

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a=1;
do
{
cout<<a<<endl;
a++;
}
while(a<=10);
}
};
void main()
{
aptech ap;
ap.series();
getch();
}

Q-3.WAP which accept end limit from the user and display 1 to n

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int a=1,n;
cout<<"Enter limit=";
cin>>n;
do
{
cout<<a<<endl;
a++;
}
while(a<=n);
}
};
void main()
{
aptech ap;
ap.series()
getch();
}

Q-4.WAP which accept start and end limit from the user and display n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
cout<<n1;
n1++;
}
while(n1<=n2);
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}

Q-5.WAP which accept start and end limit from the user and display even number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
if(n1%2==0)
{
cout<<n1;
}
n1++;
}
while(n1<=n2);
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-6.WAP which accept start and end limit from the user and display odd number between n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
if(n1%2!=0)
{
cout<<<<n1;
}
n1++;
}
while(n1<=n2);
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-7.WAP which accept start and end limit from the user and display total sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
sum=sum+n1;
n1++;
}
while(n1<=n2);
cout<<sum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-8.WAP which accept start and end limit from the user and display even sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,esum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
if(n1%2==0)
{
esum=esum+n1;
}
n1++;
}
while(n1<=n2);
cout<<esum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}


Q-9.WAP which accept start and end limit from the user and display odd sum of n1 to n2

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
if(n1%2!=0)
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
cout<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
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<iostream.h>
#include<conio.h>
class aptech
{
public:
series()
{
int n1,n2,sum=0,esum=0,osum=0;
cout<<"Enter start and end limit=";
cin>>n1>>n2;
do
{
if(n1%2==0)
{
esum=esum+n1;
}
else
{
osum=osum+n1;
}
n1++;
}
while(n1<=n2);
sum=esum+osum;
cout<<sum<<esum<<osum;
}
};
void main()
{
aptech ap;
clrscr();
ap.series();
getch();
}