# Programming Exercise

Programming Exercise Looping Structure P-9

IF Statement Programming Exercise Part-1

#### Q.62 Write a program to generate the following output using the for loop:// 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21,……………, 97, 99, 101

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
for( n = 1; n<=101; n+=2)
cout<<n<<'\t';
getch();
}```

#### Q.63 Write a program that displays the following output using the for loop:// 1, 7, 13, 19, 25, 31, 37, 43, 49,…………….,73, 79, 85, 91, 97, 103

```#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
for( n = 1; n<=103; n+=6)
cout<<n<<'\t';
getch();
}```

#### Q.64 Write a program that displays the sum of the following series using the continue statement in the body of the for loop.// 1 + 3 + 5 + 7 +……. + 201

```#include<iostream.h>
#include<conio.h>
main()
{
int n, s = 0;
clrscr();
for( n = 1; n<=201; n+=2)
s = s + n;
cout<<"Sum of series = "<<s;
getch();
}```

#### Q.65 Write a program which reads an integer value of three digits from the keyboard and calculates sum of cubes of its all digits such as:// 153 = (1*1*1) + (5*5*5) + (3*3*3)

```#include<iostream.h>
#include<conio.h>
main()
{
long r, n, cube, s = 0, d = 100;
clrscr();
cout<<"Enter 3 digits integer valuer : ";
cin>>n;
for(;n!=0;)
{
r = n/d;
n = n%d;
d = d/10;
cube = r*r*r;
cout<<"Cube of "<<r<<" is :"<<cube<<endl;
s = s + cube;
}
cout<<"Sum sum of cubes of all digits :"<<s<<endl;
getch();
}```

#### Q.66 Find the weighted average of input numbers by the following formula:// Average = i = N i = 1 fi xi i = N i = 1 fi// Input the value of N, f and x during program execution. Use the for loop in the program.

```#include<iostream.h>
#include<conio.h>
main()
{
int i, N, f, x, sum_fx = 0, sum_f = 0;
double average = 0;
clrscr();
cout<<"Enter value of N : ";
cin>>N;
for(i = 1;i<=N;i++)
{
cout<<"Enter value of f : ";
cin>>f;
cout<<"Enter value of x : ";
cin>>x;
sum_fx = sum_fx + f*x;
sum_f = sum_f + f;
}
average = sum_fx/sum_f;
cout<<"weighted average of input numbers :"<<average<<endl;
getch();
}```

#### Q.67 Write a program that inputs a number from the user and displays all prime numbers which are less than the input number using the for loop.

```#include<iostream.h>
#include<conio.h>
main()
{
int d, p, num, i;
clrscr();
cout<<"Enter an integer value ?";
cin>>num;
for(i = 2; i < num; i++)
{
p = 1;
for(d = 2; d < i; d++)
{
if (i%d ==0)
{
p = 0;
break;
}

}
if(p == 1)
cout<<i<<'\t';
}
getch();
}```

#### Q.68 Write a program that computes and displays the sum of the following series using the for loop statement.// 1/x + 1/x2 + 1/x3 +…..+ 1/xn// Input the values of ‘x’ and ‘n’ by user.

```#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
int x, n, i;
float s = 0;
clrscr();
cout<<"Enter value of x ?";
cin>>x;
cout<<"Enter value of n ?";
cin>>n;
for(i = 1; i <= n; i++)
s = s + 1.0 /pow(x, i);
cout<<"Sum of series = "<<s;

getch();
}```

#### Q.69 Write a program that inputs two numbers and finds out the greatest common divisor of both numbers using for loop.

```#include<iostream.h>
#include<conio.h>
main()
{

int x, y, i, gcd = 1;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
for(i = 2;i<=x; i++)
if(x%i== 0 && y%i== 0)
gcd = i;
cout<<"Greatet common divisor is "<<gcd;

getch();
}```

#### Q.70 Write a program that displays the following output using the for loop:// -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

```#include<iostream.h>
#include<conio.h>
main()
{

int n;
clrscr();
for(n = -10;n<=10; n++)
cout<<n<<'\t';
getch();
}```

#### Q.71 Write a program that displays the following output using the for loop:// 100, 99, 98, 97, 96, 95,…………………….…, 8, 7, 6, 5, 4, 3, 2, 1

```#include<iostream.h>
#include<conio.h>
main()
{

int n;
clrscr();
for(n = 100;n>=1; n--)
cout<<n<<'\t';
getch();
}```