# Programming Exercise Looping Structure P-8

Programming Exercise Looping Structure P-7

Conditional Structure Programming Exercise P-6

#### Q.41 Write a program that asks the user to enter a number, an operator, and another number. Then it shows the result and asks whether the user wants to do another calculation or not. Use the do-while loop and switch structures in the program.

```#include<iostream.h>
#include<conio.h>
main()
{
long int a, b;
char op, ch;
do
{
clrscr();
cout<<"Enter a number, an operator, and another number ?";
cin>>a>>op>>b;
switch(op)
{
case '+':
cout<<a + b;
break;

case '-':
cout<<a - b;
break;

case '*':
cout<<a * b;
break;
case '/':
cout<<a / b;
break;
case '%':
cout<<a % b;
break;
default:
cout<<"Invalid input";
}

cout<<endl<<"Do you want to do another calculation or not [y/n]? ";
cin>>ch;
}while(ch=='y' || ch=='Y');
cout<<"OK";
getch();
}```

#### Q.42 A telephone connection may be in any of the two states, i.e. working or dead. Write a program that reads the current state of the telephone line. The user should enter ‘w’ for working state and ‘d’ for dead state. Any other input should be considered as invalid. Use the do-while loop to force the user to enter a valid input value. (Hint: Input value for variable ‘s’ (i.e. state) in the body of loop and apply condition s!=’w’ && s!=’d’)

```#include<iostream.h>
#include<conio.h>
main()
{
char s;
do
{
clrscr();
cout<<"Enter the current state of the telephone line ?";
cin>>s;
switch(s)
{
case 'w':
cout<<"Telephone is in Working State \n";
break;

case 'd':
cout<<"Telephone is in dead State \n";
break;
default:
cout<<"Invalid input";
getch();
}

}while(s!='w' && s!='d');
cout<<"OK";
getch();
} ```

#### Q.43 Write a program that displays the following series and counts the prime numbers in the series:// 1, 5, 9, 13, ……, n

```#include<iostream.h>
#include<conio.h>
main()
{
int d, p, c, n, prime_no = 0;
c = 1;
clrscr();
cout<<"Enter value of n ?";
cin>>n;
do
{
d = 2;
p = 1;
do
{
if (c%d ==0 || c == 1)
{
p = 0;
break;
}
d++;
}while(d < c);
// count prime number
if(p == 1)
prime_no = prime_no + 1;

cout<<c<<endl;
c = c + 4;
}while(c<=n);
cout<<"Total Prime numbers in this series are :"<<prime_no;
getch();
}```

#### Q.44 Write a program to print all odd numbers up to N, where N is a positive integer value entered by user.

```#include<iostream.h>
#include<conio.h>
main()
{
int N, c = 0;
clrscr();
cout<<"Enter value for N ?";
cin>>N;
do
{
if (c%2 == 1)
cout<<c<<endl;
c = c + 1;

}while(c<=N);
getch();
}```

#### Q.45 Write a program to calculate the sum of all odd numbers between 1 and 100 using the do-while loop.

```#include<iostream.h>
#include<conio.h>
main()
{
int s = 0, c = 0;
clrscr();

do
{
if (c%2 == 1)
s = s + 1;
c = c + 1;

}while(c<=100);
cout<<"Sum of odd numbers between 1 and 100 :"<<s;
getch();
}```

#### Q.46 Write a program to print all even numbers <=N using the do-while loop. // If N =15, then even numbers are: 0 2 4 6 8 10 12 14

```#include<iostream.h>
#include<conio.h>
main()
{
int N, c = 0;
clrscr();
cout<<"Enter value of N ? ";
cin>>N;
do
{
if (c%2 == 0)
cout<<c<<endl;
c = c + 1;

}while(c<=N);
getch();
}```

#### Q.47 Write a program to show first N even numbers using the do-while loop, where N is user defined positive integer input.

```#include<iostream.h>
#include<conio.h>
main()
{
int N, c = 1;
clrscr();
cout<<"Enter value of N ? ";
cin>>N;
do
{
if (c%2 == 0)
cout<<c<<endl;
c = c + 1;

}while(c<=N);
getch();
}```

#### Q.48 Write a program which reads N integer values from the keyboard during program execution/runtime and displays the average of N values. Where N is user defined positive integer.

```#include<iostream.h>
#include<conio.h>
main()
{
int N, val, s = 0, c = 1;
clrscr();
cout<<"Enter value of N ? ";
cin>>N;
do
{
cout<<"Enter an integer value ? ";
cin>>val;
s = s + val;
c = c + 1;
}while(c<=N);
cout<<"Average of " <<N<< " values : "<<s/N;
getch();
}```

#### Q.49 Write a program which reads two integer values from keyboard during program execution/runtime and displays the GCD/HCF and LCM of these two values. For example, if a=50, b=35 then// GCD=5 and LCM=(first x second)/GCD// Therefore, LCM=(50 * 35)/5.0 LCM=350

```#include<iostream.h>
#include<conio.h>
main()
{
int i, x, y, GCD, LCM, g = 1;
clrscr();
cout<<"Enter first value ? ";
cin>>x;
cout<<"Enter second value ? ";
cin>>y;
i = 2 ;
do
{

if(x%i== 0 && y%i== 0)
g = i;
GCD = g;
i++;

}while(i<=x);
LCM = (x * y)/GCD;
cout<<"GCD of two values : "<<GCD<<endl;
cout<<"LCM of two values : "<<LCM<<endl;
getch();
}```