0% found this document useful (0 votes)
5 views

C++ Loops

The document provides examples of using for, while, and do-while loops in C++. It includes 8 programs demonstrating basic for loops, nested for loops, and examples calculating factorials, finding greatest common divisor and least common multiple using while loops. It ends with an example of a do-while loop. The examples cover common uses of loops to iterate through ranges of numbers, arrays, and in general to repeat blocks of code multiple times.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C++ Loops

The document provides examples of using for, while, and do-while loops in C++. It includes 8 programs demonstrating basic for loops, nested for loops, and examples calculating factorials, finding greatest common divisor and least common multiple using while loops. It ends with an example of a do-while loop. The examples cover common uses of loops to iterate through ranges of numbers, arrays, and in general to repeat blocks of code multiple times.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

for LOOP IN C++

for LOOP

PROGRAM 1:

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"Alphabets"<<endl;
for(ch='a';ch<='z';ch++)
{
cout<<ch<<"\t";
}
getch();
}
PROGRAM 2:

#include<iostream.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
cout<<"Enter a number to generate multiplication table = ";
cin>>num;
for(i=1;i<=10;i++)
{
cout<<num<<" * "<<i<<" = "<<num*i<<endl;
}
getch();
}
PROGRAM 3:

#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,sum=0;
clrscr();
cout<<"Enter any number = ";
cin>>num;
for(i=1;i<=num;i++)
{
sum+=i;
}
cout<<"Sum from 1 to "<<num<<" = "<<sum;
getch();
}
PROGRAM 4:

#include<iostream.h>
#include<conio.h>
void main()
{
int term,num,i;
float sum=0.0,avg;
clrscr();
cout<<"How many terms you want to calculate average = ";
cin>>term;
for(i=1;i<=term;i++)
{
cout<<"Enter number "<<i<<" = ";
cin>>num;
sum+=num;
}
cout<<"Sum = "<<sum;
avg=sum/term;
cout<<endl<<"Average = "<<avg;
getch();
}
NESTED for LOOP

PROGRAM 1:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<i;
}
cout<<endl;
}
getch();
}
PROGRAM 2:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
cout<<endl;
}
getch();
}
PROGRAM 3:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=5-i;j++)
{
cout<<" ";
}
for(k=1;k<2*i;k++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}
PROGRAM 4:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,space,num=1,row;
clrscr();
cout<<"Pascal's Triangle \n";
cout<<"Enter number of rows to generate pascal's triangle = ";
cin>>row;
for(i=0;i<row;i++)
{
for(space=1;space<=row-i;space++)
{
cout<<" ";
}
for(j=0;j<=i;j++)
{
if(i==0||j==0)
num=1;
else
num=(num*(i-j+1))/j;
cout<<num<<" ";
}
cout<<endl;
}
getch();
}
PROGRAM 5:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,space;
clrscr();
for(i=1;i<=5;i++)
{
for(space=1;space<=5-i;space++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}
PROGRAM 6:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,space;
clrscr();
for(i=5;i>=1;i--)
{
for(space=1;space<=5-i;space++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}
PROGRAM 7:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,row;
clrscr();
cout<<"Enter number of rows to generate pattern = ";
cin>>row;
for(i=1;i<=row;i++)
{
for(j=1;j<=row;j++)
{
if(i==1||i==row||j==1||j==row)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<endl;
}
getch();
}
PROGRAM 8:

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,row,count=1;
clrscr();
cout<<"Enter number of rows to generate Floyd's pattern = ";
cin>>row;
for(i=1;i<=row;i++)
{
for(j=1;j<=i;j++)
{
cout<<count<<" ";
count++;
}
cout<<endl;
}
getch();
}
WHILE LOOP IN C++

PROGRAM 1:

#include<iostream.h>
#include<conio.h>
void main()
{
int number,sum=0;
clrscr();
cout<<"Enter 0 to exit"<<endl;
cout<<"Enter a number = ";
cin>>number;
while(number!=0)
{
sum+=number;
cout<<"Enter a number = ";
cin>>number;
}
cout<<"Sum of all above numbers = "<<sum<endl;
getch();
}
PROGRAM 2:

#include<iostream.h>
#include<conio.h>
void main()
{
int i=1,number,factorial=1;
clrscr();
cout<<"enter any number = ";
cin>>number;
while(i<=number)
{
factorial=factorial*i;
i++;
}
cout<<"factorial of "<<number<<" is "<<factorial;
getch();
}
PROGRAM 3:

#include<iostream.h>
#include<conio.h>
void main()
{
int num1,num2,value1,value2,temp,gcd,lcm;
clrscr();
cout<<"Enter two integer numbers : "<<endl;
cin>>num1>>num2;
value1=num1;
value2=num2;
while(num2!=0)
{
temp=num2;
num2=num1%num2;
num1=temp;
}
gcd=num1;
lcm=(value1*value2)/gcd;
cout<<"Highest Commom Factor of "<<value1<<" and "<<value2<<
" = "<<gcd<<endl;
cout<<"Least Commom Multiple of "<<value1<<" and "<<value2<<
" = "<<lcm<<endl;
getch();
}
Do WHILE LOOP IN C++

PROGRAM 1:

#include<iostream.h>
#include<conio.h>
void main()
{
int number,sum=0;
clrscr();
cout<<"Enter 0 to exit"<<endl;
do
{
cout<<"Enter a number = ";
cin>>number;
sum+=number;
}while(number!=0);
cout<<"Sum of all above numbers = "<<sum<endl;
getch();
}

You might also like