These are used for repeating some portion of the program either by specified number of times or until a particular condition is true.
1. while statement
2. do while statement
3. for loop
while loop
This loop is that in which we can execute a set of statements again and again starting from given starting value of the variable up-to that value of the variable which is given in the termination condition.While loop starts with the the keyword ‘while’ followed by expression and a block is used there after it includes set of statements. It’s format is:
while(expression)
{
Statement1;
Statement2;
Statement3;
}
In this while loop starting value of the variable is initialized above the loop. Termination condition is given with the ‘while’ keyword and increment and decrement of the variable takes place inside the loop.
/* Program to enter limit by the user and find sum upto limit */
#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number whose sum is to be found";
cin>>n;
n1=n;
while(n>0)
{
r=n%10;
n=n/10;
sum=sum+r;
}
cout<<"\n Sum of digits of number "<<n1<<" is "<<sum;
getch();
}
/* Program to enter limit and find sum of squares upto that limit*/
#include<iostream.h>
#include<conio.h>
main()
{
int n,i=1,sum=0;
cout<<"\n Enter the limit of the series";
cin>>n;
while(i<=n)
{
sum=sum+(i*i);
i++;
}
cout<<"\n Sum of the Squares upto limit "<<n<<": "<<sum;
getch();
}
/* Program to find factorial of a number */
#include<iostream.h>
#include<conio.h>
main()
{
int n,i=1,fact=1;
cout<<"\n Enter the interger whose factorial is to be find";
cin>>n;
while(i<=n)
{
fact=fact*i;
i++;
}
cout<<"\n Factorial of "<<n<<" is "<<fact;
getch();
}
/* Program to enter a number and print its reverse number */
#include<iostream.h>
#include<conio.h>
main()
{
int n,r=0,rev=0,n1;
cout<<"\n Enter any number to see its reverse";
cin>>n;
n1=n;
while(n>0)
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
cout<<"\n Reverse of number "<<n1<<" is "<<rev;
getch();
}
/* Program to enter a number and find sum of its digits*/
#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number whose sum is to be found";
cin>>n;
n1=n;
while(n>0)
{
r=n%10;
n=n/10;
sum=sum+r;
}
cout<<"\n Sum of digits of number "<<n1<<" is "<<sum;
getch();
}
Do while looping
This loop is also an inbuilt structure in the language and it is just like for loop and while loop. But this loop is different from while loop because while loop checks the terminating condition at the top but this loop checks the condition at the bottom. This loop is a free entry loop and unlike while loop this loop will execute at least once even if the condition is false.Some basic difference is:
/* Program to enter limit and find sum of series upto that limit */
#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number to find its digit sum";
cin>>n;
n1=n;
do
{
r=n%10;
n=n/10;
sum=sum+r;
}
while(n>0);
cout<<"\n Sum of digits of "<<n1<<" is "<<sum;
getch();
}
/* Program to enter limit and find sum of squares upto that limit*/
#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,i=1;
cout<<"\n Enter the limit of the Square sum series";
cin>>n;
do
{
sum=sum+(i*i);
i++;
}
while(i<=n);
cout<<"\n Sum of Square Series upto limit "<<n<<" is "<<sum;
getch();
}
/* Program to find factorial of a number */
#include<iostream.h>
#include<conio.h>
main()
{
int n,i=1,fact=1;
cout<<"\n Enter any value whose factorial is to be found";
cin>>n;
do
{
fact=fact*i;
i++;
}
while(i<=n);
cout<<"\n Factorial of "<<n<<" is "<<fact;
getch();
}
/* Program to enter a number and print its reverse number */
#include<iostream.h>
#include<conio.h>
main()
{
int n,rev=0,r,n1;
cout<<"\n Enter any number to find its reverse number";
cin>>n;
n1=n;
do
{
r=n%10;
n=n/10;
rev=rev*10+r;
}
while(n>0);
cout<<"\n Reverse of number "<<n1<<" is "<<rev;
getch();
}
/* Program to enter a number and find sum of its digits */
#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number to find its digit sum";
cin>>n;
n1=n;
do
{
r=n%10;
n=n/10;
sum=sum+r;
}
while(n>0);
cout<<"\n Sum of digits of "<<n1<<" is "<<sum;
getch();
}
C++ Program to Reverse a Number
ReplyDeleteReverse of number means reverse the position of all digits of any number. For example reverse of 536 is 635