Programming Exercise Looping Structure P-11

Looping Structure

Programming Exercise

Programming Exercise Looping Structure P-10

IF Statement Programming Exercise Part-1

Q.72 Write a program that displays the following series and its sum using the for loop:
// 1 + 2 + 3 + 4 + 5 + 6…………………………+ 20

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

int n, s = 0;
clrscr();
for(n = 1; n<=20; n++)
s = s + n ;
cout<<"Sume of series = "<<s;
getch();
}

Q.73 Write a program that shows the sum of the following series using the for loop:
// 1 + 3 + 5 + 7 + 9 + 11 +…………………………… + 21

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

int n, s = 0;
clrscr();
for(n = 1;n<=21; n+=2)
s = s + n ;
cout<<"Sume of series = "<<s;
getch();
}

Q.74 Write a program that displays the sum of the following series using the for loop:
// 1.0 + 1.1 + 1.2 + 1.3 + 1.4 + 1.5 +………………+ 10.0

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

float n, s = 0;
clrscr();
for(n = 1.0;n<=10.0; n+=0.1)
s = s + n ;
cout<<"Sume of series = "<<s;
getch();
}

Q.75 Write a program that displays the sum of the following series using the for loop:
// 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +………………… + 1/20

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

float s = 0;
clrscr();
for(int n = 1; n<=20; n++)
s = s + 1.0/n ;
cout<<"Sume of series = "<<s;
getch();
}

Q.76 Write a program that shows the sum of the following series using the for loop:
// 1 + 1/3 + 1/5 +1/7 + 1/9 +……………………………+ 1/45

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

float s = 0;
clrscr();
for(int n = 1; n<=45; n+=2)
s = s + 1.0/n ;
cout<<"Sume of series = "<<s;
getch();
}

Q.77 Write a program to calculate and display sum of the following series using for loop:
// 1/2 + 2/3 + 3/4 +……………………………+ 99/100

#include<iostream.h>
#include<conio.h>
main()
{
float s = 0;
clrscr();
for(int n = 1, m = 2; n<=99; n++, m++)
s = s + float(n)/float(m);
cout<<"Sume of series = "<<s;
getch();
}

Q.78 Write a program which reads an integer value from the keyboard during program execution/runtime. It calculates binary of the input value and displays the result on the screen.

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

long n, r;
clrscr();
cout<<"Enter an integer value ";
cin>>n;
cout<<"(";
for(;n>=1;)
{
r = n % 2;
n = n / 2;
cout<<r;
}
cout<<")2";
getch();
}

Q.79 Write a program which reads an integer value from the keyboard during program execution/runtime. It calculates octal of input value and displays the result on the screen.

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

long n, r;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"(";
for(;n>=1;)
{
r = n % 8;
n = n / 8;
cout<<r;
}
cout<<")8";
getch();
}

Q.80 Write a program which reads a decimal integer value from keyboard during program execution/runtime. It calculates the hexadecimal of input value and displays the result on the screen.

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

long n, r;
clrscr();
cout<<"Enter an integer value ";
cin>>n;
cout<<"(";
for(;n>=1;)
{
r = n % 16;
n = n / 16;
if(r==10) 
cout<<'A';
else if(r==11) 
cout<<'B';
else if(r==12) 
cout<<'C';
else if(r==13) 
cout<<'D';
else if(r==14) 
cout<<'E';
else if(r==15) 
cout<<'F';
else
cout<<r;
}
cout<<")16";
getch();
}

Q.81 Write a program that displays all the ASCII values and their corresponding characters using any loop. The ASCII values vary from 0 to 255.

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

int asc;
char ch;
cout<<"ASCII value\t Character";
for(asc = 0; asc<=255; asc++)
{
ch = asc;
cout<<asc<<'='<<ch<<'\t';
}
getch();
}

Q.82 By using nested ‘for’ loop, display the following output:
// 1 4 9
// 16 25 36
// 49 64 81

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

int i, n, j;
clrscr();
for(i = 1, n = 1; i<=3; i++)
{
for(j = 1; j<=3; j++, n++)
cout<<n*n<<'\t';
cout<<endl;
}
getch();
}

Q.83 Write a program that displays the following output using nested for loop:
// 1 2 3 4 5 6 7 8 9
// 1 4 9 16 25 36 49 64 81
// 1 8 27 64 125 216 343 512 729

#include<iostream.h>
#include<conio.h>
#include<math.h>

main()
{

int i, j;
clrscr();
for(i = 1; i<=3; i++)
{
for(j = 1; j<=9;j++)
cout<<pow(j,i)<<'\t';
cout<<endl;
}
getch();
}

 

Leave a Reply