Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

GOVERNMENT POSTGRADUATE COLLEGE

JHANG
Department of Computer Science

[DOCUMENT TITLE]
[Document subtitle]

Loop constructs
(It is chapter No. 06, IT Series book:- Object Oriented Programming
using C++ by Prof. Tasleem Mustafa )
Here we will cover the following topics:
 Loops
 Types of loop
 while loop and programs related to this loop.
 do while loop and programs related to this loop.
 for loop and programs related to this loop

Note:
Prepare this chapters. If there is any difficulty, contact me.

Muhammad Saleem Raza


Department of Computer Sciences
Government Postgraduate College, Jhang
0345-4707500,0313-7035400

0
GOVERNMENT POSTGRADUATE COLLEGE 1

JHANG
Department of Computer Science

LOOP CONSTRUCTS
QUESTION NO.01:- What is loop? Why we need loops in programming?
Write names of different loops. Explain different parts of loop.
LOOP
A type of control structure that repeats a statement or block of
statements is known as looping structure. The mechanism through
which a statement or set of statements are executed repeatedly for
specific number of times or until some given condition remains true is
called loop.

The statements that are executed repeatedly are called body of


the loop. The structure used to repeat statements is known repetitive,
iterative or looping structure.
A loop statement allows us to execute a statement or group of
statements multiple times and following is the general from of a loop
statement in most of the programming languages.

NEED FOR LOOP


Loop helps us in programming. It makes programming easy and
simple.
TYPES OF LOOP
There are THREE different types of loop in C++ Language. These
are:
 while ( )…. Loop  do…. while( )…. Loop  for…... Loop
PARTS OF A LOOP
A Loop consists of three basic parts which are:
GOVERNMENT POSTGRADUATE COLLEGE 2

JHANG
Department of Computer Science

Starting value.  Ending Value(condition).


 Increment / Decrement.
Starting value is the value with which the loop starts. Ending
Value consists of condition or the value at which loop stops.
Increment or Decrement value increases or decreases the initial or
starting value of the loop according to the requirement.

QUESTION NO.02:- Differentiate between Counter Controlled and


Sentinel Controlled Loops.
Counter controlled LOOPs
This type of loop depends on the value of variable known as
counter variable. The value of counter variable is incremented or
decremented each time the body of the loop is executed. This loop
terminates when the value of counter variable reaches a particular
value. The number of iterations of the counter-controlled loop is
known in advance. The iterations of this loop depend on the following:
 Initial value of the counter variable
 Terminating condition.
 Increment or decrement value.
Sentinel controlled LOOPs
This type of loop depends on a special value known as sentinel
value. Sentinel value identifies whether the loop will continue or
terminate. These types of loop are called conditional loops. For
example, loop may execute while the value of variable is not -1. This
means that-1 is the sentinel value used to terminate the loop.

QUESTION NO.03:- Explain


while ( ) …….Loop structure in detail.
While( )…. LOOP structure
The While()…..loop statement is used to carry out looping
operations. It is used to execute a statement or group of statements
as long as the given conditions remain true.

While ( )…loop is a conditional loop structure. Its repetition


continues until the condition becomes false. This loop is called top
checking loop.
SYNTAX:- for single statement
Initialization;
While (condition) {
Statement;
Increment/Decrement (Jump); }
GOVERNMENT POSTGRADUATE COLLEGE 3

JHANG
Department of Computer Science

Where
Initialization:- It consists of the initial value of the variable.
Condition:- It consists of Relational Expression or logical
expression. If this condition is true then statement/group of
statements given in the body of while( )… loop structure is executed.
Statement:- It represents the body of the Loop. Furthermore, if the
body of the loop structure consists of set of Statements, then these
statements are written within two curly braces { }.
SYNTAX:- for block of statement
Initialization;
While (condition) {
Statement-1;
Statement-2;
Statement-3;
----------------
----------------
----------------
Statement-n;
Increment/decrement; }

PROGRAM-01
Write a program in C++ Language to print “Government College,
Jhang” 10-times by using while ( )… loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a =1;
while (a<=10){
cout<<"Government College Jhang"<<endl;
a=a+1;}
getch( );}
PROGRAM-02
Write a program in C++ Language to print first ten natural numbers
using while ( ) loop.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a=1;
1 2 3 4 5 6 7 8 9 10
while(a<=10) {
cout<< a<<"\t";
GOVERNMENT POSTGRADUATE COLLEGE 4

JHANG
Department of Computer Science

a=a+1; }
getch( ); }
PROGRAM-03
Write a Program in C++ Language to print first ten even numbers.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a=2;
while(a<=10) { 2 4 6 8 10 12 14 16 18 20
cout<< a<<"\t";
a=a+2; }
getch( ); }

PROGRAM-04
Write a Program in C++ Language for producing integers in the range
0 to n by using while( ) loop. Where “n” is a given number.
SOLUTION
Enter the value for n: =10
#include<conio.h>
0
#include<iostream.h> 1
void main(void) { clrscr(); 2
3
int n,a; 4
a=0; 5
6
cout<<" Enter the value for n:= "; 7
cin>>n; 8
9
while(a<=n) { 10
cout<< a<<endl;
a=a+1; }
getch( ); }
PROGRAM-05
Write a program in C++ Language to write first ten odd numbers on
Computer screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
GOVERNMENT POSTGRADUATE COLLEGE 5

JHANG
Department of Computer Science

a=1;
while(a<=20) {
cout<< a<<"\t";
1 3 5 7 9 11 13 15 17 19
a=a+2; }
getch( ); }

PROGRAM-06
Write a program in C++ Language to write first ten odd numbers and
their sum on Computer screen. 1
3
SOLUTION 5
#include<conio.h> 7
#include<iostream.h> 9
11
void main(void) { clrscr(); 13
int a, s; 15
17
s=0; 19
a=1;
while(a<=20) {
cout<<a<<endl; Sum of odd Numbers:=100
s=s+a;
a=a+2; }
cout<<" Sum of odd Numbers:="<<s<<endl;
getch();}
PROGRAM-07
Write a program in C++ Language that takes two numbers (First, Last)
from user and display all the numbers and their squares in the form of
ordered pairs between first and last number.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
Enter First Number: =-1
cout<<" Enter First Number:="; Enter Last Number: =3
cin>>a; (-1,1) (0,0) (1,1) (2,4) (3,9)

cout<<" Enter Last Number:=";


cin>>b;
while (a <= b){
cout<<"(" <<a<< "," << a * a<< ")"<<" ";
a = a + 1; }
getch();}
GOVERNMENT POSTGRADUATE COLLEGE 6

JHANG
Department of Computer Science

PROGRAM-08
Write a program in C++ Language to find out the factorial of the
number. Formula for finding factorial is: n!= (n*(n-1)*(n-2)-------1)
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n;
long int fact;
cout<<" Enter any positive Number:= ";
cin>>n;
fact=1;
while (n>0) {
Enter any positive Number:=5
fact=fact*n; Factorial of Number is :=120
n=n-1; }
cout<<"Factorial of Number is :="<<fact<<endl;
getch();}
PROGRAM-09
Write a program in C++ Language to print the even number series
between two limits given by the user.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b ;
cout<<" Enter Initial Limit:-";
cin>>a;
cout<<" Enter Final Limit :-"; Enter Initial Limit :- 5
Enter Final Limit: - 20
cin>>b; 6
if(a%2==0) 8
10
a=a; 12
else 14
16
a=a+1; 18
while (a<=b) { 20

cout<< a<<endl;
a=a+2; }
getch( ); }
ALTERNATIVE
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 7

JHANG
Department of Computer Science

#include<iostream.h>
void main(void) { clrscr(); Enter Initial Limit: - 5
int a, b ; Enter Final Limit: - 20
6
cout<<" Enter Initial Limit:-"; 8
cin>>a; 10
12
cout<<" Enter Final Limit :-"; 14
cin>>b; 16
18
while (a<=b) { 20
if(a%2==0)
cout<< a<<endl;
a=a+1; }
getch( ); }
PROGRAM-10
Write a program in C++ Language to input a positive number through
keyboard and print its reverse.

SOLUTION
#include<conio.h>
#include<iostream.h> Enter any Number:=987654321
void main(void) { clrscr(); ORIGINAL NUMBER IS:=987654321
long int number, reverse; Reverse is :=123456789

reverse=0;
cout<<"Enter Number:= ";
cin>>number;
cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;
while(number>=1){
reverse= reverse*10+ number%10;
number=number/10; }
cout<<"Reverse is := "<<reverse<<endl;
getch(); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h> Enter Number:= 523
void main(void) { clrscr(); ORIGINAL NUMBER IS:= 523
long int number, reverse; Reverse is := 325
reverse=0;
cout<<"Enter Number:= ";
cin>>number;
GOVERNMENT POSTGRADUATE COLLEGE 8

JHANG
Department of Computer Science

cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;


cout<<"Reverse is := ";
while (number>0){
reverse = number%10;
cout<<reverse;
number = number/ 10; }
getch(); }
PROGRAM-11
Write a program in C++ Language that prints the following series
X, X3/3, X5/3, X7/3, X9/3. Suppose if we take x=2 then series will be
2, 8/3, 32/3, 128/3, 512/3
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h>

void main(void) { clrscr();


int p, a, x,b;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
a=3;
ENTER THE VALUE OF X:=2
b=3; 2 8/3 32/3 128/3 512/3
while(a<10) {
p=pow(x, a);
cout<<p<<"/"<<b<<"\t";
a=a+2; }
getch( ); }
PROGRAM-12
Write a program in C++ Language to print first ten even numbers in
descending order. 20
18
SOLUTION 16
#include<conio.h> 14
#include<iostream.h> 12
10
void main(void) { clrscr(); 8
int a; 6
a=20; 4
2
while (a>0) {
GOVERNMENT POSTGRADUATE COLLEGE 9

JHANG
Department of Computer Science

cout<<a<<endl;
a=a-2; }
getch( ); }

PROGRAM-13
Write a program in C++ Language that prints the following series on
Computer screen. 1, 1/2, 1/3, 1/4,………1/45 using while( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a , c;
a=1; 1 1/2 1/3 1/4 1/5 ………1/45
c=1;
while(c<=45) {
cout<<a<<"/ "<<c<<"\t ";
c=c+1; }
getch( ); }
PROGRAM-14
Write a program in C++ Language to print multiplication table for a
number entered through keyboard.
SOLUTION Enter Table No.:=5
#include<conio.h> 5 * 1=5
5 * 2=10
#include<iostream.h> 5 * 3=15
void main(void) { clrscr(); 5 * 4=20
5 * 5=25
int a, b;
5 * 6=30
a=1; 5 * 7=35
cout<<" Enter Table No"; 5 * 8=40
5 * 9=45
cin>>b; 5 * 10=50
while(a<=10) {
cout<<a<<"*"<<a<< b<<"="<<a*b <<endl;
a=a+1; }
getch( );}

PROGRAM-15
Write a program in C++ Language to compute and print the sum of
the following series: X+X3/3+X5/3+X7/3+X9/3. Input the value of X during
Program execution.
SOLUTION
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 10

JHANG
Department of Computer Science

#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
int a;
float p, x, b, sum=0.0;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
ENTER THE VALUE OF X:=2
a=3; 2 8/3 32/3 128/3 512/3
b=3; Sum of the series is =228.667

sum=x;
while(a<10) {
p=pow(x, a);
cout<<p<<"/"<<b<<"\t";
sum = sum + p / b;
a=a+2; }
cout<<endl;
cout<<"Sum of the series is =" << sum<<endl;
getch( ); }

PROGRAM-16
Write a program in C++ Language to print multiplication table
between two limits for a number entered through keyboard.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter Table No.:= 5
void main(void) { clrscr(); Enter Initial Limit: = 10
int a,b,c ; Enter Final Limit:= 15
5 *10=50
cout<<" Enter Table No.:- "; 5 *11=55
cin>>a; 5 *12=60
5 *13=65
cout<<" Enter Initial Limit :-"; 5 *14=70
cin>>b; 5 *15=75

cout<<" Enter Final Limit:-";


cin>> c;
while(b<=c) {
cout<<a<<"*"<< b<<"= "<<a*b <<endl;
b=b+1; }
getch( );}
GOVERNMENT POSTGRADUATE COLLEGE 11

JHANG
Department of Computer Science

PROGRAM-17
Write a program in C++ Language to print numbers from 1 to 10 and
their running sum.
SOLUTION
#include<conio.h> NUMBER SUM
#include<iostream.h> 1 1
void main(void) { clrscr(); 2 3
int a, sum; 3 6
4 10
a=1;
5 15
sum=0; 6 21
cout<<" NUMBER SUM"<<endl; 7 28
while(a<=10){ 8 36
9 45
sum=sum+a;
10 55
cout<<a<<"\t\t"<<sum<<endl;
a=a+1; }
getch( ); }

PROGRAM-18
Write a program in C++ Language to print the sum of the following
series. 1+1/2+1/3+1/4+………….1/45
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
float s , c;
s=0.0;
c=1.0; Sum of the series is=4.394948
while (c<=45) {
s=s+1.0/c;
c=c+1; }
cout<<" Sum of the series is="<< s<<endl;
getch( ); }
PROGRAM-19
Write a program in C++ Language to print first ten natural numbers in
descending order.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); 10 9 8 7 6 5 4 3 2 1
int a;
GOVERNMENT POSTGRADUATE COLLEGE 12

JHANG
Department of Computer Science

a=10;
while (a>0) {
cout<<a<<"\t";
a=a-1;}
getch(); }
PROGRAM-20
Write a program in C++ Language to input FIVE numbers from user.
Compute their sum and average using While( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
Enter value for number 1:=1
void main(void) { clrscr(); Enter value for number 2:=5
int a; Enter value for number 3:=7
Enter value for number 4:=9
float n, s, avg; Enter value for number 5:=17
Sum of the Numbers is=39
a = 1; Average of Numbers is:=7.8
s = 0;
while (a <= 5) {
cout<<"Enter value for number="<<a<<":=";
cin>>n;
s = s + n;
a = a + 1; }
cout<<"Sum of the Numbers is="<<s<<endl;
avg = s / 5;
cout<<" Average of Numbers is:=" <<avg<<endl;
getch();}
PROGRAM-21
Write a program in C++ Language to print the odd number series
between two limits given by the user.
SOLUTION Enter Initial Limit :- 5
#include<conio.h> Enter Final Limit :- 25
5
#include<iostream.h>
7
void main(void) {clrscr(); 9
int a, b ; 11
13
cout<<" Enter Initial Limit:-"; 15
cin>>a; 17
19
cout<<" Enter Final Limit :-"; 21
cin>>b; 23
25
if(a%2==1) a=a;
else a=a+1;
GOVERNMENT POSTGRADUATE COLLEGE 13

JHANG
Department of Computer Science

while (a<=b) {
cout<< a<<endl;
a=a+2; }
getch( ); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); Enter Initial Limit :- 5
int a, b ; Enter Final Limit :- 25
5
cout<<" Enter Initial Limit:-";
7
cin>>a; 9
cout<<" Enter Final Limit :-"; 11
13
cin>>b; 15
while (a<=b) { 17
19
if(a%2==1) 21
cout<< a<<endl; 23
25
a=a+1; }
getch( ); }

PROGRAM-22
Write a program in C++ Language to check whether the number is
prime or not using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, d, p;
p = 1;
cout<<" Enter any number:=";
cin>>n;
d = 2;
Enter any number:=17
while (d <= n / 2){ It is Prime Number
if (n% d == 0)
p = 0;
d = d + 1; }
if (p == 0) cout<<" It is not Prime Number"<<endl;
else cout<<" It is Prime Number"<<endl;
getch();}
GOVERNMENT POSTGRADUATE COLLEGE 14

JHANG
Department of Computer Science

PROGRAM-23
Write down a program in C++ Language to produce the following
output:
0 1
1 2
2 4
3 8
4 16
5 32
6 64
SOLUTION
#include<conio.h>
#include<iostream.h>
0 1
void main(void) { clrscr(); 1 2
int n1, n2; 2 4
3 8
n1 = 0; 4 16
n2 = 1; 5 32
6 64
while (n1 <= 6){
cout<<n1<< " "<<n2<<endl;
n1 = n1 + 1;
n2 = n2 * 2;}
getch();}
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
0 1
int n1, n2; 1 2
n1 = 0; 2 4
3 8
while (n1 <= 6){ 4 16
n2=pow(2, n1); 5 32
6 64
cout<<n1<< " "<<n2<<endl;
n1 = n1 + 1; }
getch();}
PROGRAM-24
Write a program in C++ Language that prints the following series on
Computer screen: 1, 1/3, 1/5 , 1/7 , ……………………1/99
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
GOVERNMENT POSTGRADUATE COLLEGE 15

JHANG
Department of Computer Science

int a, c;
a = 1; 1 1/3 1/5 1/7 1/9 ………1/99
c = 3;
cout<<a<<" ";
while (c <= 100){
cout<<a<< "/ "<<c<<" ",
c = c + 2; }
getch();}
PROGRAM-25
Write a program in C++ Language that prints the following series along
with sum of series.: 1, 1/3, 1/5 , 1/7 , …………………1/99.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
float c, s;
a = 1;
c = 3; 1 1/3 1/5 1/7 1/9 ………1/99
cout<<a<<" "; Sum of the series is:=2.93778
s = a;
while (c <= 100){
cout<<a<< "/ "<<c<<" ";
s = s + (a / c);
c = c + 2; }
cout<<endl;
cout<<" Sum of the series is=" <<s<<endl;
getch();}
PROGRAM-26
Write a program in C++ Language to generate the following series:
1,5,25,125,625 using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a = 1; 1 5 25 125 625
while (a <= 625){
cout<<a<<" ";
GOVERNMENT POSTGRADUATE COLLEGE 16

JHANG
Department of Computer Science

a = a * 5; }
getch();}
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
int a, b;
a = 0; 1 5 25 125 625
while (a < 5) {
b=pow(5,a);
cout<<b<<" ";
a = a + 1;}
getch();}
PROGRAM-27
Write down the output of the following program.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int x, y, sum; 1
x = 1; 4
9
sum = 0; 16
while (x <= 10){ y = x * x; 25
36
cout<<y<<endl; 49
64
sum = sum + y; 81
x = x + 1; } 100
Sum is :=385
cout<<"Sum is:= "<< sum<<endl;
getch();}
PROGRAM-28
Write a program in C++ Language to print sum of odd numbers and
product of even umbers from 1 10 using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
Sum of odd Numbers:=25
void main(void) { clrscr(); Product of Even Numbers:=3840
int a, s, p;
a = 1;
GOVERNMENT POSTGRADUATE COLLEGE 17

JHANG
Department of Computer Science

s = 0;
p = 1;
while (a <= 10){
if (a% 2 == 1) s = s + a;
else p = p * a;
a = a + 1;}
cout<<" Sum of odd Numbers:= "<< s<<endl;
cout<<" Product Even of Numbers:=" << p<<endl;
getch();}

PROGRAM-29
Write a program in C++ Language to print numbers from 1 to 10 and
corresponding their squares and cubes.
SOLUTION
#include<conio.h> NUMBER SQUARE CUBE
#include<iostream.h> 1 1 1
#include<math.h> 2 4 8
3 9 27
void main(void) { clrscr();
4 16 64
int a, s, c; 5 25 125
a = 1; 6 36 216
7 49 343
cout<<" NUMBER SQUARE CUBE "<<endl;
8 64 512
while (a <= 10){ 9 81 729
s =pow( a,2); 10 100 1000

c =pow( a,3);
cout<<a << " "<<s<< " " <<c<<endl;
a = a + 1;}
getch();}

PROGRAM-30
Write a program in C++ Language to input the number “n” through
keyboard. Compute and print sum of odd numbers and sum of even
umbers from 1 n using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter any positive Number: =10
void main(void) { clrscr();
int a, n, OS, ES;
cout<<" Enter any positive Number: = ";
cin>>n;
a = 1;
GOVERNMENT POSTGRADUATE COLLEGE 18

JHANG
Department of Computer Science

ES = 0; EVEN ODD
OS = 0; 1
2
cout<<" EVEN ODD "<<endl; 3
while (a <= n) { 4

if (a %2 == 0) { 5
cout<<a<<endl; 6
7
ES = ES + a;} 8
else { 9
10
cout<<" "<< a<<endl;
OS = OS + a; } Sum of Even Numbers:=30
Sum of Odd Numbers:=25
a = a + 1; }
cout<<" Sum of Even Numbers:=" <<ES<<endl;
cout<<" Sum of odd Numbers:= "<<OS<<endl;
getch();}
PROGRAM-31
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding ASCII values using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); Letter ASCII Value
int ch; A 65
char ch1; B 66
C 67
ch = 65;
D 68
cout<<" Letter ASCII Value"<<endl; ----------------------
----------------------
while (ch <= 90){ ----------------------
Z 90
ch1 = ch;
cout<<ch1 << " "<<ch<<endl;
ch = ch + 1;}
getch();}
PROGRAM-32
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding Lower case letters( a to z) using while ( ) loop
structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
GOVERNMENT POSTGRADUATE COLLEGE 19

JHANG
Department of Computer Science

int ch1, ch2;


Upper Lower
ch1 = 65;
A a
cout<<" Upper Lower "<<endl; B b
while (ch1 <= 90){ C c
ch2 = ch1 + 32; D d
---------------------
cout<<char(ch1) <<" " << char(ch2)<<endl; ---------------------
---------------------
ch1=ch1+1;} Z z
getch();}
PROGRAM-33
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, low, high, r;
cout<<" Enter any positive Number:= ";
cin>>n;
low = n % 10;
Enter any Positive Number:=371
high = n % 10;
Highest Digit:=7
n = n / 10; Lowest Digit:=1
while (n > 0){
r = n% 10;
if (r > high) high = r;
if (r < low) low = r;
n = n / 10; }
cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
getch();}
PROGRAM-34
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number and their
sum.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
GOVERNMENT POSTGRADUATE COLLEGE 20

JHANG
Department of Computer Science

int n, low, high, r,s;


s=0;
cout<<" Enter any positive Number:= ";
cin>>n;
low = n % 10; Enter any Positive Number:=371
Highest Digit:=7
high = n % 10; Lowest Digit:=1
n = n / 10;
while (n > 0){ Sum of the Lowest and Highest Digit in Number=8

r = n% 10;
if (r > high) high = r;
if (r < low) low = r;
n = n / 10; }
cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
s=low+high;
cout<<"Sum of Lowest and Highest Digit in Number=" <<s<<endl;
getch();}
PROGRAM-35
Write a program in C++ Language to input the number “n” through
keyboard. Compute the sum of its digits.
SOLUTION
#include<conio.h>
Enter any Positive Number:=371
#include<iostream.h> Original Number is:=371
void main(void) { clrscr(); Sum of Digits of Number:=11

int N, Digit, Sum, P;


cout<<" Enter any Positive Number:= ";
cin>>N;
Sum = 0;
cout<<" Original Number is:= "<< N<<endl;
while (N > 0) {
Digit = N % 10;
Sum = Sum + Digit;
N = N / 10; }
cout<<" Sum of Digits of Number:=" <<Sum<<endl;
getch();}
PROGRAM-36
Write a program in C++ Language to display all odd integers from 1
100 skipping those that are divisible by 7.
GOVERNMENT POSTGRADUATE COLLEGE 21

JHANG
Department of Computer Science

SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
1 3 5 9 13 15………………99
a = 1;
while (a <= 100) {
if (a %7 !=0) cout<<a<< " ";
a = a + 2; }
getch();}
PROGRAM-37
Write a program in C++ Language to compute and print the sum of
the following series by using for( ) loop. 12+22+32+42+52+…….n2 Input
the value of n during the execution of program
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, n, sum;
cout<<"Enter the value for n=";
cin>>n;
Enter the value for n=5
a = 1; 1 4 9 16 25
sum = 0; Sum of the series is=55
while (a <= n){ b = a * a;
cout<<b<<" ";
sum = sum + b;
a = a + 1;}
cout<<endl;
cout<<" Sum of the series is="<< sum<<endl;
getch();}
PROGRAM-38
Write a program in C++ Language to generate the following series on
the Computer Screen 64 32 16 8 4 2 using While ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a; 64 32 16 8 4 2
a = 64;
GOVERNMENT POSTGRADUATE COLLEGE 22

JHANG
Department of Computer Science

while (a > 1){


cout<< a<<" ";
a = a / 2;}
getch();}
PROGRAM-39
Write a program in C++ Language to print numbers from 1 10 and
corresponding 10 1
SOLUTION
#include<conio.h> 1 10
#include<iostream.h> 2 9
void main(void) { clrscr(); 3 8
int a, b; 4 7
5 6
a = 1;
6 5
b = 10; 7 4
while (a <= 10){ 8 3
cout<<a<< " "<< b<<endl; 9 2
a = a + 1; 10 1
b = b – 1; }
getch();}
PROGRAM-40
Write a program in C++ Language to generate the following series of
numbers :3,12,21,30,39,48,57,66,……………
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, a; Enter the Limit=50
cout<<"Enter the Limit="; 3
12
cin>>n; 21
a=3; 30
while(a<=n){ 39
48
cout<<a<<endl;
a=a+9; }
getch();}
PROGRAM-41
Write a program in C++ Language to print the following series:
1,3,9,27,81,----------up to 300
GOVERNMENT POSTGRADUATE COLLEGE 23

JHANG
Department of Computer Science

SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a=1;
while(a<=300){ 1 3 9 27 81 243
cout<<a<<" ";
a=a*3;}
getch();}
PROGRAM-42
Write a program in C++ Language that takes “n” numbers as input. It
outputs the frequency of positive and negative numbers.
SOLUTION
#include<conio.h> How many Numbers?:=5
#include<iostream.h>
void main(void) { clrscr(); Enter value for Number 1:=4
int a, n, Num, Pos, Neg; Enter value for Number 2:=-2
Enter value for Number 3:=-5
Pos = 0; Enter value for Number 4:=1
Neg = 0; Enter value for Number 5:=-12
cout<<" How many Numbers?:=";
cin>>n; Frequency of Positive Numbers is := 2
a = 1; Frequency of Negative Numbers is := 3
while(a<=n){
cout<<" Enter value for Number " << a<< ":=";
cin>>Num;
if (Num > 0) Pos = Pos + 1;
else Neg = Neg + 1;
a = a + 1; }
cout<<" Frequency of Positive Numbers is := "<<Pos<<endl;
cout<<" Frequency of Negative Numbers is := " << Neg<<endl;
getch();}
QUESTION NO.04:- Explain the concept of “ do…. While( ) loop.
THE DO…. WHILE() loop
The “do… while( ) loop” is a conditional loop. This loop is very
similar to while( ) loop but in this loop condition is tested after the
loop is executed once.
GOVERNMENT POSTGRADUATE COLLEGE 24

JHANG
Department of Computer Science

In “do… while()…Loop ” structure, the body of the loop is


executed at least once before the condition is tested. It is repeatedly
executed as long as the given condition remains true. If the condition
becomes false at any stage during the program execution, the loop
terminates and control shifts to the statement or set of statements
that comes after the loop body.
Syntax: Initialization;
do { Statements;
Increment/Decrement; }
While ( condition ) ;
Where
do It is a keyword. It indicated the starting of do while loop.
Statements Statements enclosed in braces represent the body of loop.
Condition It is the condition that must remain true for execution of the loop.

PROGRAM-43
Write a program in C++ Language to print “Government College,
Jhang” 10-times by using do… while ( )… loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a =1;
do {
cout<<"Government College Jhang"<<endl;
a=a+1;}
while (a<=10);
getch( );}
PROGRAM-44
Write a program in C++ Language to print first ten natural numbers
using do…while ( ) loop.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a=1;
1 2 3 4 5 6 7 8 9 10
do{ cout<< a<<"\t";
a=a+1; }
while(a<=10) ;
GOVERNMENT POSTGRADUATE COLLEGE 25

JHANG
Department of Computer Science

getch( ); }
PROGRAM-45
Write a Program in C++ Language to print first ten even numbers using
do while loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a=2;
do{ 2 4 6 8 10 12 14 16 18 20
cout<< a<<"\t";
a=a+2; }
while(a<=20) ;
getch( ); }

PROGRAM-46
Write a Program in C++ Language for producing integers in the range
0 to n by using do…..while( ) loop. Where “n” is a given number.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter the value for n: =10
void main(void) { clrscr(); 0
1
int n, a=0; 2
cout<<" Enter the value for n:= "; 3
4
cin>>n; 5
do { 6
7
cout<< a<<endl; 8
a=a+1; } 9
10
while(a<=n) ;
getch( ); }
PROGRAM-47
Write a program in C++ Language to write first ten odd numbers on
Computer screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
GOVERNMENT POSTGRADUATE COLLEGE 26

JHANG
Department of Computer Science

int a;
a=1; 1 3 5 7 9 11 13 15 17 19
do {
cout<< a<<"\t";
a=a+2; }
while(a<=20);
getch( ); }

PROGRAM-48
Write a program in C++ Language to write first ten odd numbers and
their sum on Computer screen.
1
SOLUTION 3
#include<conio.h> 5
7
#include<iostream.h>
9
void main(void) { clrscr(); 11
int a, s; 13
15
s=0; 17
a=1; 19

do{ Sum of odd Numbers:=100


cout<<a<<endl;
s=s+a;
a=a+2; }
while(a<=20) ;
cout<<" Sum of odd Numbers:="<<s<<endl;
getch();}

PROGRAM-49
Write a program in C++ Language that takes two numbers (First, Last)
from user and display all the numbers and their squares in the form of
ordered pairs between first and last number.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
Enter First Number: =-1
cout<<" Enter First Number:="; Enter Last Number: =3
cin>>a; (-1,1) (0,0) (1,1) (2,4) (3,9)

cout<<" Enter Last Number:=";


cin>>b;
do {
GOVERNMENT POSTGRADUATE COLLEGE 27

JHANG
Department of Computer Science

cout<<"(" <<a<< "," << a * a<< ")"<<" ";


a = a + 1; }
while (a <= b) ;
getch();}
PROGRAM-50
Write a program in C++ Language to find out the factorial of the
number. Formula for finding factorial is: n!= (n*(n-1)*(n-2)-------1)
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n;
long int fact;
cout<<" Enter any positive Number:= ";
cin>>n;
fact=1;
do { Enter any positive Number:=5
Factorial of Number is :=120
fact=fact*n;
n=n-1; }
while (n>0) ;
cout<<"Factorial of Number is :="<<fact<<endl;
getch();}
PROGRAM-51
Write a program in C++ Language that prints the following series
X, X3/3, X5/3, X7/3, X9/3. Suppose if we take x=2 then series will be
2, 8/3, 32/3, 128/3, 512/3
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
int p, a, x, b;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
a=3;
ENTER THE VALUE OF X:=2
b=3; 2 8/3 32/3 128/3 512/3
do {
GOVERNMENT POSTGRADUATE COLLEGE 28

JHANG
Department of Computer Science

p=pow(x, a);
cout<<p<<"/"<<b<<"\t";
a=a+2; }
while(a<10) ;
getch( ); }
PROGRAM-52
Write a program in C++ Language to print the even number series
between two limits given by the user.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
Enter Initial Limit :- 5
int a, b ; Enter Final Limit: - 20
cout<<" Enter Initial Limit:-"; 6
8
cin>>a; 10
cout<<" Enter Final Limit :-"; 12
14
cin>>b;
16
if(a%2==0) 18
a=a; 20

else
a=a+1;
do {
cout<< a<<endl;
a=a+2; }
while (a<=b) ;
getch( ); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
Enter Initial Limit: - 5
int a, b ; Enter Final Limit: - 20
cout<<" Enter Initial Limit:-"; 6
8
cin>>a; 10
cout<<" Enter Final Limit :-"; 12
14
cin>>b; 16
do { if(a%2==0) 18
20
cout<< a<<endl;
a=a+1; }
while (a<=b);
GOVERNMENT POSTGRADUATE COLLEGE 29

JHANG
Department of Computer Science

getch( ); }
PROGRAM-53
Write a program in C++ Language to input a positive number through
keyboard and print its reverse.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter any Number:=987654321
void main(void) { clrscr(); ORIGINAL NUMBER IS:=987654321
long int number, reverse; Reverse is :=123456789
reverse=0;
cout<<"Enter Number:= ";
cin>>number;
cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;
do {
reverse= reverse*10+ number%10;
number=number/10; }
while(number>=1) ;
cout<<"Reverse is := "<<reverse<<endl;
getch(); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h> Enter Number:= 523
void main(void) { clrscr(); ORIGINAL NUMBER IS:= 523
long int number, reverse; Reverse is := 325
reverse=0;
cout<<"Enter Number:= ";
cin>>number;
cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;
cout<<"Reverse is := ";
do { reverse = number%10;
cout<<reverse;
number = number/ 10; }
while (number>0) ;
getch(); }
PROGRAM-54
Write a program in C++ Language to print first ten even numbers in
descending order.
GOVERNMENT POSTGRADUATE COLLEGE 30

JHANG
Department of Computer Science

SOLUTION 20
18
#include<conio.h> 16
#include<iostream.h> 14

void main(void) { clrscr(); 12


10
int a; 8
6
a=20; 4
2
do { cout<<a<<endl;
a=a-2; }
while (a>0) ;
getch( ); }

PROGRAM-55
Write a program in C++ Language that prints the following series:
1, 1/2, 1/3, 1/4,………1/45 using do while( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a , c;
a=1;
c=1; 1 1/2 1/3 1/4 1/5 ………1/45
do {
cout<<a<<"/ "<<c<<"\t ";
c=c+1; }
while(c<=45) ;
getch( ); }

PROGRAM-56
Write a program in C++ Language to print multiplication table for a
number entered through keyboard.
SOLUTION Enter Table No.:=5
#include<conio.h> 5 * 1=5
5 * 2=10
#include<iostream.h> 5 * 3=15
void main(void) { clrscr(); 5 * 4=20
5 * 5=25
int a,b;
5 * 6=30
a=1; 5 * 7=35
cout<<" Enter Table No.:="; 5 * 8=40
5 * 9=45
cin>>b; 5 * 10=50
do {
cout<<b<<"*"<<a<< "="<<a*b <<endl;
GOVERNMENT POSTGRADUATE COLLEGE 31

JHANG
Department of Computer Science

a=a+1; }
while(a<=10);
getch( );}

PROGRAM-57
Write a program in C++ Language to compute and print the sum of
the following series: X+X3/3+X5/3+X7/3+X9/3. Input the value of X during
Program execution.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h> ENTER THE VALUE OF X:=2
2 8/3 32/3 128/3 512/3
void main(void) { clrscr(); Sum of the series is =228.667
int a;
float p,x,b, sum=0.0;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
a=3;
b=3;
sum=x;
do {
p=pow(x, a);
cout<<p<<"/"<<b<<"\t";
sum = sum + p / b;
a=a+2; }
while(a<10) ;
cout<<endl;
cout<<"Sum of the series is =" << sum<<endl;
getch( ); }

PROGRAM-58
Write a program in C++ Language to print multiplication table
between two limits for a number entered through keyboard.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a,b,c ;
cout<<" Enter Table No.:- ";
GOVERNMENT POSTGRADUATE COLLEGE 32

JHANG
Department of Computer Science

cin>>a;
cout<<" Enter Initial Limit :-"; Enter Table No.:= 5
Enter Initial Limit: = 10
cin>>b; Enter Final Limit:= 15
cout<<" Enter Final Limit:-"; 5 *10=50
5 *11=55
cin>> c;
5 *12=60
do { cout<<a<<"*"<< b<<"= "<<a*b <<endl; 5 *13=65
5 *14=70
b=b+1; }
5 *15=75
while(b<=c) ;
getch( );}

PROGRAM-59
Write a program in C++ Language to print the sum of the following
series. 1+1/2+1/3+1/4+………….1/45
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
Sum of the series is=4.394948
float s , c;
s=0.0;
c=1.0;
do {
s=s+1.0/c;
c=c+1; }
while (c<=45) ;
cout<<" Sum of the series is="<< s<<endl;
getch( ); }

PROGRAM-60
Write a program in C++ Language to print numbers from 1 to 10 and
their running sum.
SOLUTION
#include<conio.h> NUMBER SUM
#include<iostream.h> 1 1
void main(void) { clrscr(); 2 3
int a, sum; 3 6
4 10
a=1;
5 15
sum=0; 6 21
cout<<" NUMBER SUM"<<endl; 7 28
while(a<=10){ 8 36
9 45
sum=sum+a;
10 55
cout<<a<<"\t\t"<<sum<<endl;
GOVERNMENT POSTGRADUATE COLLEGE 33

JHANG
Department of Computer Science

a=a+1; }
getch( ); }

PROGRAM-61
Write a program in C++ Language to input FIVE numbers from user.
Compute their sum and average of the numbers with do while( ) loop
structure.
SOLUTION Enter value for number 1:=1
#include<conio.h> Enter value for number 2:=5
Enter value for number 3:=7
#include<iostream.h> Enter value for number 4:=9
void main(void) { clrscr(); Enter value for number 5:=17
Sum of the Numbers is=39
int a; Average of Numbers is:=7.8

float n, s, avg;
a = 1;
s = 0;
do {
cout<<"Enter value for number="<<a<<":=";
cin>>n;
s = s + n;
a = a + 1; }
while (a <= 5);
cout<<"Sum of the Numbers is="<<s<<endl;
avg = s / 5;
cout<<" Average of Numbers is:=" <<avg<<endl;
getch();}
PROGRAM-62
Write a program in C++ Language to print the odd number series
between two limits given by the user.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter Initial Limit :- 5
Enter Final Limit :- 25
void main(void) { clrscr(); 5
int a, b ; 7
9
cout<<" Enter Initial Limit:-";
11
cin>>a; 13
cout<<" Enter Final Limit :-"; 15
17
cin>>b; 19
if(a%2==1) 21
23
a=a; 25
else
GOVERNMENT POSTGRADUATE COLLEGE 34

JHANG
Department of Computer Science

a=a+1;
do { cout<< a<<endl;
a=a+2; }
while (a<=b) ;
getch( ); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); Enter Initial Limit :- 5
int a, b ; Enter Final Limit :- 25
cout<<" Enter Initial Limit:-"; 5
7
cin>>a; 9
cout<<" Enter Final Limit :-"; 11
13
cin>>b; 15
do { if(a%2==1) 17
19
cout<< a<<endl; 21
a=a+1; } 23
25
while (a<=b) ;
getch( ); }
PROGRAM-63
Write a program in C++ Language to print first ten natural numbers in
descending order.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a=10;
do {
cout<<a<<"\t"; 10 9 8 7 6 5 4 3 2 1
a=a-1;}
while (a>0);
getch(); }
PROGRAM-64
Write a program in C++ Language that prints the following series on
Computer screen: 1, 1/3, 1/5 , 1/7 , ……………………1/99
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 35

JHANG
Department of Computer Science

void main(void) { clrscr();


int a, c;
a = 1; 1 1/3 1/5 1/7 1/9 ………1/99
c = 3; Sum of the series is:=2.93778
cout<<a<<" ";
do { cout<<a<< "/ "<<c<<" ",
c = c + 2; }
while (c <= 100) ;
getch();}
PROGRAM-65
Write a program in C++ Language that prints the following series along
with sum of series.: 1, 1/3, 1/5 , 1/7 , …………………1/99
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
float c, s;
a = 1;
c = 3; 1 1/3 1/5 1/7 1/9 ………1/99
cout<<a<<" "; Sum of the series is:=2.93778
s = a;
do {
cout<<a<< "/ "<<c<<" ",
s = s + (a / c);
c = c + 2; }
while (c <= 100) ;
cout<<endl;
cout<<" Sum of the series is=" <<s<<endl;
getch();}
PROGRAM-66
Write a program in C++ Language to generate the following series:
1,5,25,125,625 using do while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
a = 1; 1 5 25 125 625
GOVERNMENT POSTGRADUATE COLLEGE 36

JHANG
Department of Computer Science

do {
cout<<a<<" ";
a = a * 5; }
while (a <= 625) ;
getch();}
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
int a,b;
a = 0; 1 5 25 125 625
do { b=pow(5,a);
cout<<b<<" ";
a = a + 1;}
while (a < 5) ;
getch();}
PROGRAM-67
Write down a program in C++ Language to produce the following
output using do while( ) structure :
0 1
1 2
2 4
3 8
4 16
5 32
6 64
SOLUTION
#include<conio.h>
#include<iostream.h>
0 1
void main(void) { clrscr(); 1 2
int n1, n2; 2 4
3 8
n1 = 0; 4 16
n2 = 1; 5 32
6 64
do {
cout<<n1<< " "<<n2<<endl;
n1 = n1 + 1;
n2 = n2 * 2;}
while (n1 <= 6) ;
getch();}
ALTERNATIVE
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 37

JHANG
Department of Computer Science

#include<iostream.h>
#include<math.h>
0 1
void main(void) { clrscr(); 1 2
int n1, n2; 2 4
3 8
n1 = 0; 4 16
do { 5 32
6 64
n2=pow(2,n1);
cout<<n1<< " "<<n2<<endl;
n1 = n1 + 1; }
while (n1 <= 6);
getch();}
PROGRAM-68
Write a program in C++ Language to check whether the number is
prime or not using do while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, d, p;
p = 1;
cout<<" Enter any number:=";
cin>>n;
d = 2;
Enter any number:=17
do { It is Prime Number
if (n% d == 0)
p = 0;
d = d + 1; }
while (d <= n / 2) ;
if (p == 0) cout<<" It is not Prime Number"<<endl;
else cout<<" It is Prime Number"<<endl;
getch();}
PROGRAM-69
Write down the output of the following program.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int x, y, sum;
GOVERNMENT POSTGRADUATE COLLEGE 38

JHANG
Department of Computer Science

x = 1; 1
sum = 0; 4
9
while (x <= 10){ y = x * x; 16
cout<<y<<endl; 25
36
sum = sum + y; 49
x = x + 1; } 64
81
cout<<"Sum is:= "<< sum<<endl; 100
Sum is :=385
getch();}

PROGRAM-70
Write a program in C++ Language to print sum of odd numbers and
product of even umbers from 1 10 using do while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, s, p;
a = 1;
s = 0; Sum of odd Numbers:=25
Product of Even Numbers:=3840
p = 1;
do { if (a% 2 == 1)
s = s + a;
else
p = p * a;
a = a + 1;}
while (a <= 10) ;
cout<<" Sum of odd Numbers:= "<< s<<endl;
cout<<" Product Even of Numbers:=" << p<<endl;
getch();}

PROGRAM-71
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding ASCII values using do while ( ) loop structure.
SOLUTION
#include<conio.h> Letter ASCII Value
#include<iostream.h> A 65
B 66
void main(void) { clrscr();
C 67
int ch; D 68
char ch1; ----------------------
----------------------
----------------------
ch = 65;
Z 90
GOVERNMENT POSTGRADUATE COLLEGE 39

JHANG
Department of Computer Science

cout<<" Letter ASCII Value"<<endl;


do {
ch1 = ch;
cout<<ch1 << " "<<ch<<endl;
ch = ch + 1;}
while (ch <= 90);
getch();}
PROGRAM-72
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding Lower case letters( a to z) using do while ( ) loop
structure.
SOLUTION Upper Lower
#include<conio.h> A a
#include<iostream.h> B b
void main(void) { clrscr(); C c
int ch1, ch2; D d
---------------------
ch1 = 65; ---------------------
---------------------
cout<<" Upper Lower "<<endl; Z z
do {
ch2 = ch1 + 32;
cout<<char(ch1) <<" " << char(ch2)<<endl;
ch1=ch1+1;}
while (ch1 <= 90);
getch();}

PROGRAM-73
Write a program in C++ Language to print numbers from 1 to 10 and
corresponding their squares and cubes.
SOLUTION
#include<conio.h> NUMBER SQUARE CUBE
#include<iostream.h> 1 1 1
#include<math.h> 2 4 8
3 9 27
void main(void) { clrscr();
4 16 64
int a, s, c; 5 25 125
a = 1; 6 36 216
7 49 343
cout<<" NUMBER SQUARE CUBE "<<endl;
8 64 512
do { 9 81 729
s =pow( a,2); 10 100 1000

c =pow( a,3);
GOVERNMENT POSTGRADUATE COLLEGE 40

JHANG
Department of Computer Science

cout<<a << " "<<s<< " " <<c<<endl;


a = a + 1;}
while (a <= 10) ;
getch();}

PROGRAM-74
Write a program in C++ Language to input the number “n” through
keyboard. Compute and print sum of odd numbers and sum of even
umbers from 1 n using do while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter any positive Number: =10
void main(void) { clrscr();
int a, n, OS, ES;
cout<<" Enter any positive Number: = ";
cin>>n;
a = 1;
ES = 0; EVEN ODD
OS = 0; 1
2
cout<<" EVEN ODD "<<endl; 3
do { 4
if (a %2 == 0) { 5
cout<<a<<endl; 6
7
ES = ES + a;}
8
else { 9
cout<<" "<< a<<endl; 10

OS = OS + a; } Sum of Even Numbers:=30


a = a + 1; } Sum of Odd Numbers:=25

while (a <= n) ;
cout<<" Sum of Even Numbers:=" <<ES<<endl;
cout<<" Sum of odd Numbers:= "<<OS<<endl;
getch();}
PROGRAM-75
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 41

JHANG
Department of Computer Science

void main(void) { clrscr();


int n, low, high, r;
cout<<" Enter any positive Number:= ";
cin>>n;
low = n % 10;
Enter any Positive Number:=371
high = n % 10;
Highest Digit:=7
n = n / 10; Lowest Digit:=1
do { r = n% 10;
if (r > high) high = r;
if (r < low) low = r;
n = n / 10; }
while (n > 0) ;
cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
getch();}
PROGRAM-76
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number and their
sum.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, low, high, r,s;
s=0;
cout<<" Enter any positive Number:= ";
cin>>n; Enter any Positive Number:=371
low = n % 10; Highest Digit:=7
Lowest Digit:=1
high = n % 10;
n = n / 10; Sum of the Lowest and Highest Digit in Number=8
do { r = n% 10;
if (r > high) high = r;
if (r < low) low = r;
n = n / 10; }
while (n > 0) ;
cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
GOVERNMENT POSTGRADUATE COLLEGE 42

JHANG
Department of Computer Science

s=low+high;
cout<<"Sum of Lowest and Highest Digit in Number=" <<s<<endl;
getch();}
PROGRAM-77
Write a program in C++ Language to input the number “n” through
keyboard. Compute the sum of its digits.
SOLUTION Enter any Positive Number:=371
#include<conio.h> Original Number is:=371
Sum of Digits of Number:=11
#include<iostream.h>
void main(void) { clrscr();
int N, Digit, Sum, P;
cout<<" Enter any Positive Number:= ";
cin>>N;
Sum = 0;
cout<<" Original Number is:= "<< N<<endl;
do { Digit = N % 10;
Sum = Sum + Digit;
N = N / 10; }
while (N > 0);
cout<<" Sum of Digits of Number:=" <<Sum<<endl;
getch();}
PROGRAM-78
Write a program in C++ Language to compute and print the sum of
the following series by using for( ) loop. 12+22+32+42+52+…….n2 Input
the value of n during the execution of program
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, n, sum; Enter the value for n=5
cout<<"Enter the value for n="; 1 4 9 16 25
Sum of the series is=55
cin>>n;
a = 1;
sum = 0;
do { b = a * a;
cout<<b<<" ";
sum = sum + b;
a = a + 1;}
while (a <= n);
GOVERNMENT POSTGRADUATE COLLEGE 43

JHANG
Department of Computer Science

cout<<endl;
cout<<" Sum of the series is="<< sum<<endl;
getch();}
PROGRAM-79
Write a program in C++ Language to display all odd integers from 1
100 skipping those that are divisible by 7.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
1 3 5 9 13 15………………99
a = 1;
do {
if (a %7 !=0) cout<<a<< " ";
a = a + 2; }
while (a <= 100) ;
getch();}
PROGRAM-80
Write a program in C++ Language to print numbers from 1 10 and
corresponding 10 1
SOLUTION
#include<conio.h> 1 10
#include<iostream.h> 2 9
void main(void) { clrscr(); 3 8
4 7
int a, b;
5 6
a = 1;
6 5
b = 10; 7 4
do { cout<<a<< " "<< b<<endl; 8 3
a = a + 1; 9 2
b = b – 1; } 10 1
while (a <= 10);
getch();}
PROGRAM-81
Write a program in C++ Language to generate the following series on
the Computer Screen 64 32 16 8 4 2 using do While ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 44

JHANG
Department of Computer Science

void main(void) { clrscr();


int a;
a = 64;
do { cout<< a<<" ";
a = a / 2;}
64 32 16 8 4 2
while (a > 1) ;
getch();}

PROGRAM-82
Write a program in C++ Language to print the following series:
1,3,9,27,81,------------------200
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a=1;
do {
cout<<a<<" "; 1 3 9 27 81

a=a*3;}
while(a<=200) ;
getch();}
PROGRAM-83
Write a program in C++ Language that takes “n” numbers as input. It
outputs the frequency of positive and negative numbers.
SOLUTION
#include<conio.h> How many Numbers?:=5
#include<iostream.h>
void main(void) { clrscr(); Enter value for Number 1:=4
int a, n, Num, Pos, Neg; Enter value for Number 2:=-2
Enter value for Number 3:=-5
Pos = 0; Enter value for Number 4:=1
Neg = 0; Enter value for Number 5:=-12
cout<<" How many Numbers?:=";
cin>>n; Frequency of Positive Numbers is := 2
a = 1; Frequency of Negative Numbers is := 3
while(a<=n){
cout<<" Enter value for Number " << a<< ":=";
cin>>Num;
if (Num > 0) Pos = Pos + 1;
GOVERNMENT POSTGRADUATE COLLEGE 45

JHANG
Department of Computer Science

else Neg = Neg + 1;


a = a + 1; }
cout<<" Frequency of Positive Numbers is := "<<Pos<<endl;
cout<<" Frequency of Negative Numbers is := " << Neg<<endl;
getch();}
PROGRAM-84
Write a program in C++ Language to generate the following series of
numbers :3,12,21,30,39,48,57,66,……………
SOLUTION Enter the Limit=50
3
#include<conio.h>
12
#include<iostream.h>
21
void main(void) { clrscr(); 30
int n, a; 39
cout<<"Enter the Limit="; 48

cin>>n;
a=3;
while(a<=n){
cout<<a<<endl;
a=a+9; }
getch();}
QUESTION NO.05:- Differentiate between While( )…Loop and do…
while( ) loop Structure.
DIFFERENCE
There is minor difference between the working of While()…loop
and do…While( ) loops. This difference is the place where the condition
is tested.
 The While( )…loop tests the condition before executing any of the statements
within the body of while loop. First condition is tested and then body of the loop is
executed. If the condition is true, the body of the loop is executed. On the other
hand, the body of the loop is not executed and control is shifted outside the body
of the loop.
 The do… while( ) Loop Structure tests the condition after having executed the
single statement within the loop. In this structure, body of the loop comes before
the condition. Therefore, the body of the loops is executed at least once even the
condition is false.

QUESTION NO.06:- Differentiate between Pretest and Posttest in loops.


Pretest
A pretest is a condition that is used to test for the completion of
loop at the top of the loop. In this type of loop, the statements inside
GOVERNMENT POSTGRADUATE COLLEGE 46

JHANG
Department of Computer Science

the body of the loop can never execute if the condition is false at the
first time of condition.
Posttest
A posttest is a condition that is used to test for the completion
of loop at the bottom of the loop. In this type of loop, the statements
inside the body of the loop will always be executed at least once even
the condition is false because the condition is tested at the bottom of
the body of the loop.

QUESTION NO.07:- Write a note on for( )…… Loop?


THE FOR LOOP
The “for( )…… ” loop structure is used to execute a statement or
set of statements repeatedly for a fixed number of times. It is also
known as counter loop.
Syntax
for(Starting value; Condition ; Step increment/decrement) {
Statements (body of the loop); }
The for statement controls the loop. It consists of keyword for
followed by parenthesis. The statement of loop is divided by into 3
separate sections.
 Initialization.  Condition.
 Increment/Decrement section (Jump).
Starting value:
In this section, all counter variables are initialized.
Ending value
In this section final value is provided. It is evaluated each time
through the loop, just before the body of the loop is executed. It
determines whether the loop will be executed again. It is called
conditional section.
If the conditional section is true, the loop is executed one more
time, on the other hand, if it fails, the loop ends.
Step
This section is used to increase or decrease value of counter loop.

PROGRAM-85
Write a program in C++ Language to print “Government College,
Jhang” 10-times by using for( )… loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 47

JHANG
Department of Computer Science

void main(void) { clrscr();


int a ;
for(a=1;a<=10;a++)
cout<<"Government College Jhang"<<endl;
getch( );}
PROGRAM-86
Write a program in C++ Language to print first ten natural numbers
using for ( ) loop.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); 1 2 3 4 5 6 7 8 9 10
int a;
for(a=1;a<=10;a++)
cout<< a<<"\t";
getch( ); }
PROGRAM-87
Write a Program in C++ Language to print first ten even numbers.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
for(a=2; a<=20;a+=2)
cout<< a<<"\t";
2 4 6 8 10 12 14 16 18 20
getch( ); }

PROGRAM-88
Write a program in C++ Language to write first ten odd numbers on
Computer screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
for(a=1;a<=20; a+=2)
cout<< a<<"\t"; 1 3 5 7 9 11 13 15 17 19
getch( ); }
GOVERNMENT POSTGRADUATE COLLEGE 48

JHANG
Department of Computer Science

PROGRAM-89
Write a Program in C++ Language for producing integers in the range
0 to n by using for( ) loop. Where “n” is a given number.
SOLUTION Enter the value for n: =10
#include<conio.h> 0
#include<iostream.h> 1
void main(void) { clrscr(); 2
3
int a, n; 4
cout<<" Enter the value for n:= "; 5
6
cin>>n; 7
for(a=0; a<=n; a++) 8
9
cout<< a<<endl; 10
getch( ); }
PROGRAM-90
Write a program in C++ Language to write first ten odd numbers and
their sum on Computer screen.
SOLUTION 1
#include<conio.h> 3
5
#include<iostream.h>
7
void main(void) { clrscr(); 9
int a, s; 11
Sum of odd Numbers:=100 13
s=0; 15
for(a=1; a<=20; a+=2) { 17
19
cout<<a<<endl;
s=s+a; }
cout<<" Sum of odd Numbers:="<<s<<endl;
getch();}

PROGRAM-91
Write a program in C++ Language that takes two numbers (First, Last)
from user and display all the numbers and their squares in the form of
ordered pairs between first and last number.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b,c;
Enter First Number: =-1
cout<<" Enter First Number:="; Enter Last Number: =3
cin>>a; (-1,1) (0,0) (1,1) (2,4) (3,9)
GOVERNMENT POSTGRADUATE COLLEGE 49

JHANG
Department of Computer Science

cout<<" Enter Last Number:=";


cin>>b;
for(c=a; c <= b; c++)
cout<<"(" <<c<< "," << c * c<< ")"<<" ";
getch();}
PROGRAM-92
Write a program in C++ Language to find out the factorial of the
number. Formula for finding factorial is: n!= (n*(n-1)*(n-2)-------1)
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, a;
long int fact;
cout<<" Enter any positive Number:= ";
cin>>n;
Enter any positive Number:=5
fact=1; Factorial of Number is :=120
for(a=n; a>0; a--)
fact=fact*a;
cout<<"Factorial of Number is :="<<fact<<endl;
getch();}
PROGRAM-93
Write a program in C++ Language to print the even number series
between two limits given by the user.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b,c ;
cout<<" Enter Initial Limit:-"; Enter Initial Limit :- 5
Enter Final Limit: - 20
cin>>a; 6
cout<<" Enter Final Limit :-"; 8
10
cin>>b;
12
if(a%2==0) 14
a=a; 16
18
else 20
a=a+1;
for(c=a; c<=b; c+=2)
GOVERNMENT POSTGRADUATE COLLEGE 50

JHANG
Department of Computer Science

cout<< c<<endl;
getch( ); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
Enter Initial Limit: - 5
void main(void) { clrscr(); Enter Final Limit: - 20
int a, b,c ; 6
8
cout<<" Enter Initial Limit:-"; 10
cin>>a; 12
14
cout<<" Enter Final Limit :-"; 16
cin>>b; 18
20
for(c=a; c<=b; c++) {
if(c%2==0)
cout<< c<<endl; }
getch( ); }
PROGRAM-94
Write a program in C++ Language to input a positive number through
keyboard and print its reverse.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter any Number:=987654321
void main(void) { clrscr(); ORIGINAL NUMBER IS:=987654321
long int number, reverse, N; Reverse is :=123456789
reverse=0;
cout<<"Enter Number: = ";
cin>>number;
cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;
for(N=number; N>=1; N=N/10)
reverse= reverse *10+ N%10;
cout<<"Reverse is := "<<reverse<<endl;
getch(); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
Enter Number:= 523
void main(void) { clrscr(); ORIGINAL NUMBER IS:= 523
long int number, reverse, N; Reverse is := 325
reverse=0;
cout<<"Enter Number:= ";
GOVERNMENT POSTGRADUATE COLLEGE 51

JHANG
Department of Computer Science

cin>>number;
cout<< "ORIGINAL NUMBER IS:= "<<number<<endl;
cout<<"Reverse is := ";
for(N=number; N>0; N=N/10){
reverse = N%10;
cout<<reverse;}
getch(); }
PROGRAM-95
Write a program in C++ Language that prints the following series
X, X3/3, X5/3, X7/3, X9/3. Suppose if we take x=2 then series will be
2, 8/3, 32/3, 128/3, 512/3
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h>

void main(void) { clrscr();


int p, a, x, b;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
b=3;
for(a=3; a<10;a+=2) {
ENTER THE VALUE OF X:=2
p=pow(x, a); 2 8/3 32/3 128/3 512/3
cout<<p<<"/"<<b<<"\t"; }
getch( ); }
PROGRAM-96
Write a program in C++ Language to print first ten even numbers in
descending order.
SOLUTION 20
18
#include<conio.h> 16
#include<iostream.h> 14

void main(void) { clrscr(); 12


10
int a; 8
6
for( a=20; a>0; a-=2) 4
2
cout<<a<<endl;
getch( ); }
GOVERNMENT POSTGRADUATE COLLEGE 52

JHANG
Department of Computer Science

PROGRAM-97
Write a program in C++ Language that prints the following series on
Computer screen. 1, 1/2, 1/3, 1/4,………1/45 using while( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); 1 1/2 1/3 1/4 1/5 ………1/45
int a , c;
a=1;
for(c=1; c<=45;c++)
cout<<a<<"/ "<<c<<"\t ";
getch( ); }
PROGRAM-98
Write a program in C++ Language to print multiplication table for a
number entered through keyboard.
SOLUTION
#include<conio.h> Enter Table No.:=5
5 * 1=5
#include<iostream.h>
5 * 2=10
void main(void) { clrscr(); 5 * 3=15
int a,b; 5 * 4=20
5 * 5=25
cout<<" Enter Table No"; 5 * 6=30
cin>>b; 5 * 7=35
5 * 8=40
for(a=1; a<=10; a++) 5 * 9=45
cout<<b<<"*"<< a<<"="<<a*b <<endl; 5 * 10=50

getch( );}

PROGRAM-99
Write a program in C++ Language to print the sum of the following
series. 1+1/2+1/3+1/4+………….1/45
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
float s , c;
s=0.0;
for(c=1; c<=45;c++) Sum of the series is=4.39495
s=s+1.0/c;
cout<<" Sum of the series is="<< s<<endl;
getch( ); }
GOVERNMENT POSTGRADUATE COLLEGE 53

JHANG
Department of Computer Science

PROGRAM-100
Write a program in C++ Language to compute and print the sum of the
following series: X+X3/3+X5/3+X7/3+X9/3. Input the value of X during
Program execution.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h> ENTER THE VALUE OF X:=2
void main(void) { clrscr(); 2 8/3 32/3 128/3 512/3
Sum of the series is =228.667
int a;
float p, x, b, sum=0.0;
cout<<" ENTER THE VALUE OF X:= ";
cin>>x;
cout<<x<<"\t" ;
b=3;
sum=x;
for( a=3; a<10;a+=2) {
p=pow(x, a);
cout<<p<<"/"<<b<<"\t";
sum = sum + p / b; }
cout<<endl;
cout<<"Sum of the series is =" << sum<<endl;
getch( ); }
PROGRAM-101
Write a program in C++ Language to print multiplication table
between two limits for a number entered through keyboard.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter Table No.:= 5
void main(void) { clrscr(); Enter Initial Limit: = 10
int a, b, c, d ; Enter Final Limit:= 15
5 *10=50
cout<<" Enter Table No.:- "; 5 *11=55
cin>>a; 5 *12=60
5 *13=65
cout<<" Enter Initial Limit :-"; 5 *14=70
cin>>b; 5 *15=75

cout<<" Enter Final Limit:-";


cin>> c;
for(d=b;d<=c; d++)
cout<<a<<"*"<<d<<"= "<<a*d <<endl;
GOVERNMENT POSTGRADUATE COLLEGE 54

JHANG
Department of Computer Science

getch( );}

PROGRAM-102
Write a program in C++ Language to input FIVE numbers from user.
Compute their sum and average using for( ) loop structure.
SOLUTION
#include<conio.h> Enter value for number 1:=1
#include<iostream.h> Enter value for number 2:=5
Enter value for number 3:=7
void main(void) { clrscr(); Enter value for number 4:=9
int a; Enter value for number 5:=17
Sum of the Numbers is=39
float n, s, avg; Average of Numbers is:=7.8

s = 0;
for(a =1; a <= 5; a++) {
cout<<"Enter value for number="<<a<<":=";
cin>>n;
s = s + n;}
cout<<"Sum of the Numbers is="<<s<<endl;
avg = s / 5;
cout<<" Average of Numbers is:=" <<avg<<endl;
getch();}
PROGRAM-103
Write a program in C++ Language to print numbers from 1 to 10 and
their running sum.
SOLUTION
#include<conio.h> NUMBER SUM
#include<iostream.h> 1 1
void main(void) { clrscr(); 2 3
int a, sum; 3 6
4 10
sum=0;
5 15
cout<<" NUMBER SUM"<<endl; 6 21
for(a=1; a<=10; a++){ 7 28
sum=sum+a; 8 36
9 45
cout<<a<<"\t\t"<<sum<<endl; }
10 55
getch( ); }

PROGRAM-104
Write a program in C++ Language to print first ten natural numbers in
descending order.
SOLUTION
#include<conio.h> 10 9 8 7 6 5 4 3 2 1
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 55

JHANG
Department of Computer Science

void main(void) { clrscr();


int a;
for(a=10; a>0; a--)
cout<<a<<"\t";
getch(); }
PROGRAM-105
Write down a program in C++ Language to produce the following
output using loop structure:
0 1
1 2
2 4
3 8
4 16
5 32
6 64
SOLUTION
#include<conio.h>
#include<iostream.h>
0 1
void main(void) { clrscr(); 1 2
int n1, n2; 2 4
3 8
n2 = 1; 4 16
for(n1=0; n1 <= 6;n1++){ 5 32
6 64
cout<<n1<< " "<<n2<<endl;
n2 = n2 * 2;}
getch();}
ALTERNATIVE
#include<conio.h>
#include<iostream.h> 0 1
#include<math.h> 1 2
2 4
void main(void) { clrscr(); 3 8
int n1, n2; 4 16
5 32
for(n1=0; n1 <= 6;n1++){ 6 64
n2=pow(2, n1);
cout<<n1<< " "<<n2<<endl;}
getch();}
PROGRAM-106
Write a program in C++ Language to print the odd number series
between two limits given by the user.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) {clrscr();
GOVERNMENT POSTGRADUATE COLLEGE 56

JHANG
Department of Computer Science

int a, b ,c ;
cout<<" Enter Initial Limit:-";
cin>>a; Enter Initial Limit :- 5
Enter Final Limit :- 20
cout<<" Enter Final Limit :-"; 5
cin>>b; 7
9
if(a%2==1) a=a; 11
else a=a+1; 13
15
for(c=a; c<=b; c+=2) 17
cout<< c<<endl; 19

getch( ); }
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
Enter Initial Limit :- 5
int a, b, c ; Enter Final Limit :- 20
cout<<" Enter Initial Limit:-"; 5
7
cin>>a; 9
cout<<" Enter Final Limit :-"; 11
13
cin>>b; 15
for(c=a; c<=b; c++) { 17
19
if(c%2==1)
cout<< c<<endl; }
getch( ); }
PROGRAM-107
Write a program in C++ Language to check whether the number is
prime or not using for ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, d, p;
p = 1;
cout<<" Enter any number:=";
cin>>n;
d = 2;
Enter any number:=17
for(d=2; d <= n / 2; d++){ It is Prime Number
if (n% d == 0)
p = 0;}
if (p == 0) cout<<" It is not Prime Number"<<endl;
GOVERNMENT POSTGRADUATE COLLEGE 57

JHANG
Department of Computer Science

else cout<<" It is Prime Number"<<endl;


getch();}
PROGRAM-108
Write a program in C++ Language that prints the following series on
Computer screen: 1, 1/3, 1/5 , 1/7 , ……………………1/99
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, c;
a = 1; 1 1/3 1/5 1/7 1/9 ………1/99
cout<<a<<" ";
for(c=3; c <= 100; c+=2)
cout<<a<< "/ "<<c<<" ";
getch();}
PROGRAM-109
Write a program in C++ Language that prints the following series along
with sum of series.: 1, 1/3, 1/5 , 1/7 , …………………1/99.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
float c, s;
1 1/3 1/5 1/7 1/9 ………1/99
a = 1; Sum of the series is:=2.93778
cout<<a<<" ";
s = a;
for(c=3; c <= 100; c+=2){
cout<<a<< "/ "<<c<<" ";
s = s + (a / c); }
cout<<endl;
cout<<" Sum of the series is=" <<s<<endl;
getch();}
PROGRAM-110
Write a program in C++ Language to generate the following series:
1,5,25,125,625 using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 58

JHANG
Department of Computer Science

void main(void) { clrscr();


int a;
for( a = 1; a <= 625; a*=5) 1 5 25 125 625
cout<<a<<" ";
getch();}
ALTERNATIVE
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr(); 1 5 25 125 625
int a, b;
for( a = 0; a < 5;a++) {
b=pow(5,a);
cout<<b<<" "; }
getch();}
PROGRAM-111
Write down the output of the following program.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int x, y, sum; 1
x = 1; 4
9
sum = 0; 16
for(x=1; x <= 10;x++){ 25
36
y = x * x; 49
64
cout<<y<<endl; 81
sum = sum + y; } 100
Sum is :=385
cout<<"Sum is:= "<< sum<<endl;
getch();}
PROGRAM-112
Write a program in C++ Language to input the number “n” through
keyboard. Compute and print sum of odd numbers and sum of even
umbers from 1 n using for ( ) loop structure.
SOLUTION
Enter any positive Number: =10
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, n, OS, ES;
GOVERNMENT POSTGRADUATE COLLEGE 59

JHANG
Department of Computer Science

cout<<" Enter any positive Number: = "; EVEN ODD


cin>>n; 1
2
a = 1; 3
ES = 0; 4

OS = 0; 5
cout<<" EVEN ODD "<<endl; 6
7
for(a=1; a <= n; a++) { 8
if (a %2 == 0) { 9
10
cout<<a<<endl;
ES = ES + a;} Sum of Even Numbers:=30
Sum of Odd Numbers:=25
else {
cout<<" "<< a<<endl;
OS = OS + a; } }
cout<<" Sum of Even Numbers:=" <<ES<<endl;
cout<<" Sum of odd Numbers:= "<<OS<<endl;
getch();}
PROGRAM-113
Write a program in C++ Language to print sum of odd numbers and
product of even umbers from 1 10 using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr(); Sum of odd Numbers:=25
int a, s, p; Product of Even Numbers:=3840

s = 0;
p = 1;
for(a=1; a <= 10;a++){
if (a% 2 == 1) s = s + a;
else p = p * a; }
cout<<" Sum of odd Numbers:= "<< s<<endl;
cout<<" Product Even of Numbers:=" << p<<endl;
getch();}
PROGRAM-114
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding ASCII values using while ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 60

JHANG
Department of Computer Science

void main(void) { clrscr();


Letter ASCII Value
int ch;
A 65
char ch1; B 66
cout<<" Letter ASCII Value"<<endl; C 67
for(ch=65; ch <= 90; ch++){ D 68
ch1 = ch; ----------------------
----------------------
----------------------
cout<<ch1 << " "<<ch<<endl; } Z 90
getch();}
PROGRAM-115
Write a program in C++ Language to print Uppercase letters (A to Z)
and their corresponding Lower case letters( a to z) using while ( ) loop
structure.
SOLUTION Upper Lower
#include<conio.h> A a
#include<iostream.h> B b
void main(void) { clrscr(); C c
int ch1, ch2; D d
---------------------
cout<<" Upper Lower "<<endl; ---------------------
---------------------
for(ch=65; ch <= 90; ch++){ Z z
ch2 = ch1 + 32;
cout<<char(ch1) <<" " << char(ch2)<<endl;
ch1=ch1+1;}
getch();}
PROGRAM-116
Write a program in C++ Language to print numbers from 1 to 10 and
corresponding their squares and cubes.
SOLUTION
#include<conio.h> NUMBER SQUARE CUBE
#include<iostream.h> 1 1 1
#include<math.h> 2 4 8
3 9 27
void main(void) { clrscr(); 4 16 64
int a, s, c; 5 25 125
cout<<" NUMBER SQUARE CUBE "<<endl; 6 36 216
7 49 343
for(a=1; a <= 10; a++){ 8 64 512
s =pow( a,2); 9 81 729
10 100 1000
c =pow( a,3);
cout<<a << " "<<s<< " " <<c<<endl; }
getch();}
GOVERNMENT POSTGRADUATE COLLEGE 61

JHANG
Department of Computer Science

PROGRAM-117
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, low, high, r,k;
cout<<" Enter any positive Number:= ";
cin>>n;
Enter any Positive Number:=371
low = n % 10; Highest Digit:=7
high = n % 10; Lowest Digit:=1

n = n / 10;
for(k=n; k > 0; k=k/10){
r = k% 10;
if (r > high) high = r;
if (r < low) low = r; }
cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
getch();}
PROGRAM-118
Write a program in C++ Language to input the number “n” through
keyboard. Find the lowest and the highest digit of the number and their
sum.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int n, low, high, r,s,k;
s=0;
cout<<" Enter any positive Number:= ";
cin>>n;
low = n % 10; Enter any Positive Number:=371
Highest Digit:=7
high = n % 10; Lowest Digit:=1
n = n / 10;
for(k=n; k > 0; k=k/10){ Sum of the Lowest and Highest Digit in Number=8

r = k% 10;
if (r > high) high = r;
GOVERNMENT POSTGRADUATE COLLEGE 62

JHANG
Department of Computer Science

if (r < low) low = r; }


cout<<" Highest Digit in Number:=" <<high<<endl;
cout<<" Lowest Digit in Number:="<<low<<endl;
s=low+high;
cout<<"Sum of Lowest and Highest Digit in Number=" <<s<<endl;
getch();}
PROGRAM-119
Write a program in C++ Language to input the number “n” through
keyboard. Compute the sum of its digits.
SOLUTION
#include<conio.h>
Enter any Positive Number:=371
#include<iostream.h> Original Number is:=371
void main(void) { clrscr(); Sum of Digits of Number:=11

int N, Digit, Sum, P,K;


cout<<" Enter any Positive Number:= ";
cin>>N;
Sum = 0;
cout<<" Original Number is:= "<< N<<endl;
for(K=N; K > 0; K=K/10){
Digit = K % 10;
Sum = Sum + Digit; }
cout<<" Sum of Digits of Number:=" <<Sum<<endl;
getch();}
PROGRAM-120
Write a program in C++ Language to compute and print the sum of
the following series by using for( ) loop. 12+22+32+42+52+…….n2 Input
the value of n during the execution of program
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, n, sum;
cout<<"Enter the value for n=";
cin>>n;
Enter the value for n=5
a = 1; 1 4 9 16 25
sum = 0; Sum of the series is=55
for(a=1; a <= n; a++) {
b = a * a;
cout<<b<<" ";
GOVERNMENT POSTGRADUATE COLLEGE 63

JHANG
Department of Computer Science

sum = sum + b; }
cout<<endl;
cout<<" Sum of the series is="<< sum<<endl;
getch();}
PROGRAM-121
Write a program in C++ Language to display all odd integers from 1
100 skipping those that are divisible by 7.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) {
clrscr(); 1 3 5 9 13 15………99
int a;
for(a=1; a <= 100; a+=2)
if (a %7 !=0) cout<<a<< " ";
getch();}

PROGRAM-122
Write a program in C++ Language to print numbers from 1 10 and
corresponding 10 1
SOLUTION 1 10
2 9
#include<conio.h>
3 8
#include<iostream.h>
4 7
void main(void) { clrscr(); 5 6
int a, b; 6 5
b = 10; 7 4
for(a=1; a <= 10; a++){ 8 3
cout<<a<< " "<< b<<endl; 9 2
b = b – 1; } 10 1
getch();}
PROGRAM-123
Write a program in C++ Language to generate the following series on
the Computer Screen 64 32 16 8 4 2 using for ( ) loop structure.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a;
64 32 16 8 4 2
for( a = 64; a > 1; a=a/2)
GOVERNMENT POSTGRADUATE COLLEGE 64

JHANG
Department of Computer Science

cout<< a<<" ";


getch();}
PROGRAM-124
Write a program in C++ Language to generate the following series of
numbers :3,12,21,30,39,48,57,66,….
SOLUTION
#include<conio.h>
#include<iostream.h> Enter the Limit=50
void main(void) { clrscr(); 3
12
int n, a;
21
cout<<"Enter the Limit="; 30
cin>>n; 39
for( a=3; a<=n; a+=9) 48

cout<<a<<endl;
getch();}
PROGRAM-125
Write a program in C++ Language to print the following series:
1,3,9,27,81,----------up to 300
SOLUTION
#include<conio.h>
#include<iostream.h>
1 3 9 27 81 243
void main(void) { clrscr();
int a;
for(a=1; a<=300; a*=9){
cout<<a<<" ";
getch();}
PROGRAM-126
Write a program in C++ Language that takes “n” numbers as input. It
outputs the frequency of positive and negative numbers.
SOLUTION
#include<conio.h> How many Numbers?:=5
#include<iostream.h>
void main(void) { clrscr(); Enter value for Number 1:=4
int a, n, Num, Pos, Neg; Enter value for Number 2:=-2
Enter value for Number 3:=-5
Pos = 0; Enter value for Number 4:=1
Neg = 0; Enter value for Number 5:=-12
cout<<" How many Numbers?:=";
cin>>n;
GOVERNMENT POSTGRADUATE COLLEGE 65

JHANG
Department of Computer Science

for( a = 1; a<=n; a++){


cout<<" Enter value for Number " << a<< ":=";
cin>>Num;
Frequency of Positive Numbers is := 2
if (Num > 0) Pos = Pos + 1;
Frequency of Negative Numbers is := 3
else Neg = Neg + 1; }
cout<<" Frequency of Positive Numbers is := "<<Pos<<endl;
cout<<" Frequency of Negative Numbers is := " << Neg<<endl;
getch();}
QUESTION NO.08:- How loops are terminated early?
Termination of LOOP
Generally, termination of loop structure depends on the condition
but we can terminate loop without looking at the condition.
Exit For statement is used in the body of for…. loop to terminate
it abnormally.

QUESTION NO.09:- What is Nested Loop?


NESTED LOOP
A loop structure completely inside the body of another loop
structure is called a nested loop. In nested loops, the inner loop
structure is executed completely with each change in the value of
variable of outer loop.
Syntax:
for(initialization ; condition ; Step increment/decrement)

for(initialization ; condition ; Step increment/decrement))


Statements (body of the loop)

Outer Inner
loop loop
Example:
In order to understand the concept of nested loop, concentrate on
the following code segment.
1
#include<conio.h>
Garrison College, Jhang
#include<iostream.h> Garrison College, Jhang
void main(void) { clrscr(); 2
int a, b; Garrison College, Jhang
Garrison College, Jhang
for(a=1;a<=3; a++){ 3
cout<<a<<endl; Garrison College, Jhang
Garrison College, Jhang
for(b=1;b<=2;b++){
GOVERNMENT POSTGRADUATE COLLEGE 66

JHANG
Department of Computer Science

cout<<"Government College, Jhang "<<endl; } }


getch();}

PROGRAM-127
Write a program in C++ Language to show following output using loop
structure. 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, c;
for(a=1;a<=5; a++){
for(b=1;b<=5;b++){
c=a*b;
cout<<c<<" "; }
cout<<endl; }
getch();}
PROGRAM-128
Write a program in C++ Language to show following output using loop
structure.
* * * * *
* * * * *
* * * * *
* * * * *
SOLUTION * * * * *
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=1;a<=5; a++){
for(b=1;b<=5;b++){
cout<<" * "; }
cout<<endl; }
getch();}
GOVERNMENT POSTGRADUATE COLLEGE 67

JHANG
Department of Computer Science

PROGRAM-129
Write a program in C++ Language to show following output using loop
structure.
*
* *
* * *
* * * *
* * * * *
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=1;a<=5; a++){
for(b=1;b<=a; b++){
cout<<" * "<<" "; }
cout<<endl; }
getch();}

PROGRAM-130
Write a program in C++ Language to show following output using loop
structure. 0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5

SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=0;a<=5; a++){
for(b=0;b<=a; b++){
cout<<b<<" "; }
cout<<endl; }
getch();}
PROGRAM-131
Write a program in C++ language to input a number through
keyboard and display all primes numbers up to that number.
GOVERNMENT POSTGRADUATE COLLEGE 68

JHANG
Department of Computer Science

SOLUTION
#include<conio.h>
#include<iostream.h> Enter any number:=17
1
void main(void) { clrscr(); 2
int n, Num, p; 3
cout<<" Enter any number:="; 5
7
cin>>Num; 11
for( n = 1; n<=Num; n++) { 13
17
p = 1;
for( c = 2 ; c<= n \ 2; c++) {
if (n %c == 0) p = 0
if (p = =1) cout<<n<<endl;} }
getch();}

PROGRAM-132
Write a program in C++ Language to show following output using loop
structure. 1
1 6
1 6 36
1 6 36 216
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void) { clrscr();
int a, b, c;
for(a=0;a<=3; a++){
for(b=0;b<=a; b++){
c=pow(6,b);
cout<<c<<" "; }
cout<<endl; }
getch();}
PROGRAM-133
Write a program in C++ Language to show following output using loop
structure. 0
0 1
0 1 4
0 1 4 9
0 1 4 9 16
0 1 4 9 16 25
GOVERNMENT POSTGRADUATE COLLEGE 69

JHANG
Department of Computer Science

SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=0;a<=5; a++){
for(b=0;b<=a; b++){
cout<<b*b<<" "; }
cout<<endl; }
getch();}
PROGRAM-134
Write a program in C++ Language to show following output using loop
structure.
* * * * *
* * * *
* * *
* *
SOLUTION
*
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=1;a<=5; a++){
for(b=5;b>=a; b--){
cout<<" * "<<" "; }
cout<<endl; }
getch();}
PROGRAM-135
Write a program in C++ Language to show following output using loop
structure.
5 4 3 2 1 0
4 3 2 1 0
3 2 1 0
2 1 0
1 0
0
SOLUTION
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 70

JHANG
Department of Computer Science

#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=5;a>=0; a--){
for(b=a;b>=0; b--){
cout<<b<<" "; }
cout<<endl; }
getch();}
PROGRAM-136
Write a program in C++ Language to show following output using loop
structure.
5 5 5 5 5 5
4 4 4 4 4
3 3 3 3
2 2 2
1 1
0
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=5;a>=0; a--){
for(b=a;b>=0; b--){
cout<<a<<" "; }
cout<<endl; }
getch();}
PROGRAM-137
Write a program in C++ Language to show following output using loop
structure.
1 2 3 4 5
1 3 5 7 9
1 4 7 10 13
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, c, n;
GOVERNMENT POSTGRADUATE COLLEGE 71

JHANG
Department of Computer Science

for(a=1;a<=3; a++){
n=1;
for(b=1;b<=5;b++){
cout<<n<<" ";
n=n+a;}
cout<<endl; }
getch();}

PROGRAM-138
Write a program in C++ Language to show following output using loop
structure. 1 2 3 4 5
28 56 84 112 140
55 110 165 220 275
82 164 246 328 410

SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, c, n;
for(a=1;a<=100; a+=27){
for(b=1;b<=5;b++){
c=a*b;
cout<<c<<" "; }
cout<<endl; }
getch();}

PROGRAM-139
Write a program in C++ Language to show following output.
1 2 4 6
2 2 4 6
3 2 4 6
4 2 4 6
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b;
for(a=1;a<=4; a++){
cout<<a<<" ";
for(b=2;b<=6;b+=2){
GOVERNMENT POSTGRADUATE COLLEGE 72

JHANG
Department of Computer Science

cout<<" "<<b<<" "; }


cout<<endl; }
getch();}
PROGRAM-140
Write a program in C++ Language to show following output using loop
structure.
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39 41
43 45 47 49 51 53 55
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr();
int a, b, c;
c=1;
for(a=1;a<=7; a++){
for(b=1;b<=a; b++){
cout<<c<<" ";
c=c+2;}
cout<<endl; }
getch();}

SYED MUHAMMAD SALEEM RAZA


ASSISTANT PROFESSOR (Comp.Sc)
GOVERNMENT POSTGRADUATE COLLEGE, JHANG

You might also like