﻿ Programming using nested loop in c++ About  Contact

# Programs Using Loops

Nested loop:When a loop is used inside another loop then it is called nested loops. Here for each iteration of the outer loop- the inner loop starts from the begining. For example if the outer loop iterates 10 times and inner loop iterates 5 times then total number of iterations of the two loops will be 50 times.

Let us write few more programs and study the use of loops practically-
Q31.Write a program to find the sum of the series :
1 + x + x2/2! + x3/3! + x4/4! + ... till nth terms.
Q32.Write a program to to find the sum of the series : x - x3/3! + x5/5! - x7/7! + x9/9! - ... till nth terms.
Q33.Write a program to to accept an 4 digit integer then check if it is a palindrome or not.
Q34.Write a program to to accept an integer then check if it is prime or not.
Q35.Write a program to accept to accept a 4 digit number then find the greatest digit of it.
Program-31

```#include <iostream.h>
#include <math.h>
void main(){
float sum=1, x;
int n,i, f;
cout<< "Enter the number of terms(>1):";
cin>>n;
cout<<"Enter the value of x :";
cin>>x;
for(i=1; i<= n-1; i++){
f=1;
for(k=1; k<=i; k++){
f=f*k;
}
sum=sum + pow(x, i)/f;
}
cout<<"The sum="<<sum;
}
```

In this program see that "sum=1" the first terms is already considered as constant, that is why the outer loop is iterating till "n-1" times. The factorial term is calculated by the inner loop. Lastly each term is generated and added with sum then displayed it outside the loops.
Program-32
```#include <iostream.h>
#include <math.h>
void main(){
float sum=0, x,t;
int n,i, f;
cout<< "Enter the number of terms:";
cin>>n;
cout<<"Enter the value of x :";
cin>>x;
for(i=1; i<= n; i++){
f=1;
for(k=1; k<=2*i-1; k++){
f=f*k;
}
t = pow(-1,i+1) * pow(x, 2*i-1)/f;
sum = sum + t;
}
cout<<"The sum="<<sum;
}
```
In this series first see the first term is positive, second term is negative and so on. The sign of each term is generated by "pow( -1, i+1)" therefore when "i" is 1 the -1 to the power (i+1) means 2 is positive, same way when "i" is 2 then -1 to the power 2 is positive and so on, which is multiplied with each term. Then see that in first term denominator is 1 same as factorial 1, second term the denominator is factorial 3 and so on, that is why the inner loop finding factorial is iterating till 2*i-1. Notice that the power of each term is odd i.e 1, 3, 5, 7 and so on, which is taken care by "pow(x, 2*i-1). A variable "t" is taken for simplicity to generate each term then added with variable "sum". Lastly the asnwer is displayed outside the loops
Program-33
```#include <iostream.h>
void main(){
int n, p=0, d, a;
cout<<"Enter the Number :";
cin>>n;
a=n;
while(n>0){
d=n%10;
p=p*10+d;
n=n/10;
}
if(a==p){
cout<<"It is a palindrome";
}
else{
cout<<"It is not palindrome";
}
}
```
To check palindrome we have to reversed the digits of number then compare with the original number if they are equal then the number is palindrome otherwise it is not. e.g 1221 is palindrome because if you reverse the digits it remains same, but 1234 is not palindrme as the reversed number 4321 is not same as the original number. In this program see that we have kept a copy of the given number is variable "a" because the original number is geting changed in the loop (n=n/10). To reverse the digits of a number we have to do modulus or remainder operation (%) by 10, because if you divide a number by 10 then take the remainder it would be always the last digit of the number. ( take an example and see ). Now let us run the program ourself statement by statement - p=0, take an 4 digit number is entered as n=3443, so also a=3443, see in the while loop iteration - 1, the statement d=3443%10 will give d=3, p=0*10+3, which gives p=3, n=3443/10 will give n=344 because "n" in integer can not store fractional part. iteration-2 d=344%10 will give d=4, p=3*10+4 will give p=34, n=344/10 will give n=34. Iteration-3 d=4, p=344, n=3. Iteration-4, d=3%10 will give 3 as quotient is zero and remainder is 3, p=3443, n=3/10 is zero, now the looping condition is false so it comes out of the loop and the "if" statement displays that it is a palindrome.
Program-34
```#include <iostream.h>
void main(){
int n, c=0,i;
cout<<"Enter the Number :";
cin>>n;
for(i=1; i<=n; i++){
if(n%i==0){
c=c+1;
}
}
if(c==2){
cout<<"It is a prime number";
}
else{
cout<<"It is not a prime number";
}
}
```

This algorithm (logic) is very simple one, the given number is divided by all the numbers from 1 to the number itself and count how many times the get divided with remainder zero, this count is done in the variable "c". If the number is a prime one then it will be divided twice once by 1 then at last by the number itself. That is why we have checked if c==2 in the program.

Program-35
```#include <iostream.h>
void main(){
int n, g, d;
cout<<"Enter the Number :";
cin>>n;
g=n%10;
n=n/10;
while(n>0){
d=n%10;
if(d>g){
g=d;
}
n=n/10;
}
cout<<"The greatest digit is"<<g;
}
```

This program is all most similar as program 33, first we have taken the last digit as greatest (g), then within the loop we have extracted one digit in each iteration and compare it with the current greatest- if extracted digit is greater than the current greatest (g) the we have changed current greatest. At last we got the greatest digit in "g" which is displayed. If you want to work with larger than four digit number then take the variable "n" as "long" type.

Q. Write a program to display the difference between greatest and smallest digits of a four digit number. (Do it youeself) 