Table of Contents
Introduction
What are the repetition and loop statements in C++? In programming, there are many tasks that we want to do repeatedly. For this we use the repetitive statements. Such statements help the users to write the program in compact way. Three types of repetitive statements are
- For loop
- while loop
- do-while loop
For Loop:
The syntax of for loop is as follows
for(variable initialization; test condition; increment or decrement in the variable) e.g.,
for(int i=0;i<=100;i++)
{
cout<<i<<endl;
}
this program will print the numbers from 0 to 100 in new line always. Now, if you want to print the numbers with the increment of 2. then the program can be modified as:
for(int i=0;i<=100;i=i+2)
{
cout<<i<<endl;
}
https://www.youtube.com/watch?v=aMA1mcDdGdw
While Loop:
While loop has the syntax like
while(condition)
{
statements
}
In while loop, first the condition is tested and then the following set of statements is executed. For example the above problem of printing then numbers from 0 to 100 can be done using while loop as:
int i=0;
while(i!=100)
{
cout<<i<<endl;
i++;
}
Do- While Loop:
Its syntax is as follows:
do
{
statements
}
while(condition);
in do-while loop first the set of statements execute for a single time and then the condition is tested. This is how do-while loop is different from the simple while loop. The implementation of the same problem of printing first 100 numbers using do-while loop is as follows:
int i=0;
do
{
cout<<i<<endl;
i++;
}
while(i!=101);
Lets do some problems where we can better understand how these loops are used.
Question 1
Write a C++ program for determining the factorial of a number.
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, factorial=1;
cout<<“enter the number whose factorial is to be determined”<<endl;
cin>>n;
while(n!=0)
{
factorial=n*factorial;
n–;
}
cout<<“the factorial of” << n<< “is”<< factorial<<endl;
getch();
}
Question No. 2
How can we test a number entered by the user is perfect or not? Write a C++ program for testing either the entered number is perfect or not.
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, sum=0;
cout<<“enter the number to check either its perfect or not”<<endl;
cin>>n;
for(int i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
cout<<“the number is perfect”;
else
cout<<“the number is not perfect”
getch();
}
Question No. 3
Exponential function or exp (x) = ex is a mathematical function learned in calculus. ex can be represented as a power series ex = 1 + x + x2 / 2! + x3 / 3! + … + xn / n! .
Write a C++ program for calculating exponential function using series expansion.
Solution
void main()
{
clrscr();
double n,x,div,sum=0,power;
cout<<“enter the number of terms you want to add<<endl;
cin>>n;
cout<<“enter the value of x”<<endl;
cin>>x;
for(int loop=n; loop>=0;loop–)
{
n=loop;
int factorial=1;
power=pow(x,n);
while(n>0)
{
factorial=factorial*n;
n–;
}
div=power/factorial;
sum=sum+div;
}
cout<<“the value of exponential function for n=”<n<<“and x=”<<x<<“is”<<sum;
getch();
}
Output window
Question No. 4
Write a C++ program to count the number of words and number of alphabets in a phrase.
Solution:
void main()
{
clrscr();
int numberofwords=1, numberofalph=0;
char ch=’a’;
cout<<“enter the sentence”<<endl;
while(ch!=’\r’)
{
ch=getche();
if(ch==’ ‘)
numberofwords++;
else
numberofalph++;
}
cout<<“number of words in the sentence are:”<<numberofwords<<endl;
cout<<“number of alphabets in the sentence are:”<<numberofalph-1<<endl;
getch();
}
Watch here
Also read here about functions in C++