0% found this document useful (0 votes)
55 views43 pages

PSCP Assignment: Name: Kotana Gopi Chand ROLL NO.: 841936

The document contains 17 coding problems involving basic programming concepts like loops, conditional statements, functions etc. Each problem has a short description and the code to solve that problem.

Uploaded by

Pratheek Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views43 pages

PSCP Assignment: Name: Kotana Gopi Chand ROLL NO.: 841936

The document contains 17 coding problems involving basic programming concepts like loops, conditional statements, functions etc. Each problem has a short description and the code to solve that problem.

Uploaded by

Pratheek Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

PSCP ASSIGNMENT

NAME : KOTANA GOPI CHAND


ROLL NO. : 841936
1) Enter 4-digit number through keyboard. Write a program to obtain the sum of 1st and last digits of

this number.

CODE:

#include<iostream>
using namespace std;
int main()
{
int a,b,c,sum;
cout<<"enter any four digit number"<<endl;
cin>>a;
b=a%10;
c=a/1000;
sum=b+c;
cout<<"required sum="<<sum;
return 0;
}

=====================================================================================
2) Enter a year through keyboard. Write a program to determine whether the year is leap year or not.

Use logical operators && and ||.

CODE:

#include<iostream>
using namespace std;
int main()
{
int year,a,b,c;
cout<<"enter any year"<<endl;
cin>>year;
a=year%4;
b=year%100;
c=year%400;
if(c==0||b!=0&&a==0)
cout<<"leap year";
else
cout<<"ordinary year";
return 0;
}

=====================================================================================

3) Write a program that will take three numbers from keyboard and find the maximum of these

numbers. Then check whether the maximum number is even or odd.

CODE:

#include<iostream>
using namespace std;
int main()
{
int a,b,c,max,d;
cout<<"enter any three numbers"<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
max=a;
cout<<"MAX="<<max;
}
else
{
max=c;
cout<<"MAX="<<max;
}
}
else
if(b>c)
{
max=b;
cout<<"MAX="<<max;
}
else
{
max=c;
cout<<"MAX="<<max;
}
d=max%2;
if(d==0)
cout<<endl<<"even number";
else
cout<<endl<<"odd number";
return 0;
}
=================================================================================

4) Find out the sum of squares of first n numbers.

CODE:

#include<iostream>
using namespace std;
int main()
{
int n,sum=0,i;
float average;
cout<<"enter the value of n:";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
cout<<"SUM="<<sum;
return 0;
}

=====================================================================================
5) Find out the average of n numbers.

CODE:

#include<iostream>
using namespace std;
int main()
{
int n,sum=0,i;
float average;
cout<<"enter the value of n:"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<endl<<"SUM="<<sum;
average=(float)sum/n;
cout<<endl<<"AVERAG="<<average;
return 0;
}

=====================================================================================

6) The mark price and discount are entered through keyboard. Sometimes seller gets profit of x % or

some time loss of y % depends on discount. Write a program to determine whether the seller has

made profit or incurred loss. Also determine how much profit he made or loss incurred. Enter the

cost price also through key board.


CODE:

#include<iostream>
using namespace std;
int main()
{
int mp,d,sp,a,b,c;
cout<<"enter the marked price:";
cin>>mp;
cout<<endl<<"enter discount percentage:";
cin>>d;
cout<<endl<<"enter selling price:";
cin>>sp;
a=(d*sp)/100;
b=sp-a;
c=b-mp;
if(c>0)
cout<<"PROFIT="<<c;
else if(c<0)
cout<<"LOSS="<<-c;
else
cout<<"NEUTRAL";
return 0;
}

=====================================================================================

7) Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each
digit of the number is equal to the number itself, then the number is called an Armstrong number. For
example 153= (1*1*1) + (5*5*5) + (3*3*3).

CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int sum,num;
for(int i=0;i<=5;i++)
{
for(int j=0;j<=10;j++)
{
for(int k=0;k<=10;k++)
{
num=i*100+j*10+k;
sum=((i*i*i)+(j*j*j)+(k*k*k));
if(num==sum)
cout<<"numbers"<<num<<endl;
}
}
}
return 0;
}

====================================================================================

8) There are 9000 people in a town whose population increases by 15% each year. Write a program

displays the annual population and determines the number of years it will take for the population to

surpass 50000.

CODE:
#include<iostream>
using namespace std;
int main()
{
float n=0,a=9000,b;
b=a+(15*a)/100;
cout<<"annual population ="<<b<<endl;
while(a<=50000)
{
a=a+((a*15)/100);
n++;
cout<<"population value="<<a<<endl;
}
cout<<endl<<"number of years taken to surpass 50000 is "<<n;
return 0;
}

===============================================================================

9) Write a program to print the sum of digits of any positive number

CODE:

#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"enter any number"<<endl;
cin>>n;
while(n>0)
{
sum=sum+n%10;
n=n/10;
}
cout<<"sum of digits ="<<sum;
return 0;
}

========================================================================

10) Write a program to receive Cartesian coordinates (x,y) of a points and convert them

into polar co-ordinates(r,θ).

CODE:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float x,y,r;
double angle;
double A,p=3.1415;
cout<<"enter coordinates in cartesian system "<<endl;
cin>>x>>y;
r=sqrt(x*x+y*y);
A=(180/p)*atan(y/x);
if(x>=0&&y>=0)
angle=A;
else if(x<=0&&y>=0)
angle=180-A;
else if(x<=0&&y<=0)
angle=180+A;
else
angle=360-A;
cout<<"polar coordinates are"<<"("<<r<<","<<angle<<")";
return 0;
}

========================================================================
11) Write a program to receive values of latitude(L1,L2) and longitude (G1,G2), in

degrees, oftwo places on the earth and output the distance(D) between then in nautical

miles.

CODE:

#include<iostream>

#include<cmath>

using namespace std;

#define pi 3.14159265359

double convertDtR(double);

int main(){

//formulae

//a=sin(dellon/2)^2+cos(lat1)*cos(lat2)*sin(dellon/2)^2;

//c=2*atan2(sqrt(a),sqrt(1-a))=sininv

//d=R*c

double lat[2],lon[2];

cout<<"Enter Latitude and Longitude of first place: ";

cout<<"\n=>";cin >>lat[0]>>lon[0];

cout<<"Enter Latitude and Longitude of second place: ";

cout<<"\n=>";cin >>lat[1]>>lon[1];

for(int i=0;i<=1;i++){

lat[i]=convertDtR(lat[i]);

lon[i]=convertDtR(lon[i]);

double dellat=lat[1]-lat[0];
double dellon=lon[1]-lon[2];

double D=sin(dellat/2)*sin(dellat/2)+cos(lat[0])*cos(lat[1])*sin(dellon/2)*sin(dellon/2);

D=2*asin(sqrt(D));

double r=3440.1;//nautical miles

D=D*r;

cout<<"The distance between them is "<<D<<" nautical miles"<<endl;

return 0;

double convertDtR(double degree){

return ((degree/180)*pi);

=============================================================================

12) Wind chill factor is the felt air temperature on exposed skin due to wind. The wind chill

temperature is always lower than the air temperature. Write a program to calculate the wind

chill factor.

CODE:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double t,v,w;
cout<<"enter temperature in farenheit:"<<endl;
cin>>t;
cout<<"enter velocity of air in miles/hour:"<<endl;
cin>>v;
w=33-(((10*sqrt(v)-v+10.5)*(33-t))/23.1);
cout<<"windchill factor is="<<w;
return 0;
}
=====================================================================

13) If the value of an angle is input through the keyboard, write a program to print all its

Trigonometric ratios.

CODE:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double angle,A,p=3.1416;
cout<<"enter angle in degrees"<<endl;
cin>>angle;
A=(p/180)*angle;
cout<<"sin"<<A<<"="<<sin(A)<<endl;
cout<<"cos"<<A<<"="<<cos(A)<<endl;
cout<<"tan"<<A<<"="<<tan(A)<<endl;
cout<<"cosec"<<A<<"="<<1/sin(A)<<endl;
cout<<"sec"<<A<<"="<<1/cos(A)<<endl;
cout<<"cot"<<A<<"="<<1/tan(A);
return 0;
}

========================================================================

14) Find the gcd and lcm of given two numbers


CODE:

#include<iostream>
using namespace std;
int main()
{
int lcm,a,b;
cout <<"Enter any two positive numbers :";
cin >>a >> b;
int temp1=a,temp2=b;
if(a<=0||b<=0)
{
cout <<"Invalid Input";
return 0;
}
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
lcm=(temp1*temp2)/a;
cout <<"The GCD is "<< a<<endl;
cout <<"The LCM is "<<lcm<<endl;
return 0;
}

=====================================================================================
15) Write a program to find the sum of first n terms of series:

1 + X + (X*X)/2! + (X*X*X)/3! + (X*X*X*X)/4! +.............

CODE:

#include<iostream>
using namespace std;
int main()
{
float x,sum;
int n;
cout<<"enter value of x"<<endl;
cin>>x;
cout<<"enter no. of terms n"<<endl;
cin>>n;
float mul=1,f=1,i;
for(i=1;i<=n;i++)
{
mul=mul*x;
f=f*i;
sum=1+(mul/f);
}
cout<<"required sum ="<<sum;
return 0;
}

=====================================================================================

16) Write a program to accept a number and find sum of its individual digits repeatedly till the result

is asingle digit. For example, if the given number is 4687 the output should be 7.
CODE:

#include<iostream>
using namespace std;
int main()
{
long a;
int s,c,n,b,sum=0;
cout<<"enter any number"<<endl;
cin>>a;
while(a>0)
{
b=a%10;
sum=sum+b;
a=a/10;
if(sum>=10)
{
while(sum>0)
{
c=sum%10;
s=s+c;
sum=sum/10;
}
cout<<"required sum="<<s;
}
else
cout<<"required sum="<<sum;
}
return 0;
}

=====================================================================================

17) Write a program to get following output


ABCDEFGFEDCBA

ABCDEF FEDCBA

ABCDE EDCBA

ABCD DCBA

ABC CBA

AB BA

A A

CODE:

#include<iostream>
using namespace std;
int main(){
int row=7;
for(int i=1;i<=(2*row-1);i++)
cout <<(char)('A'+i-1);
cout <<endl;
for(int i=2;i<=row;i++){
//left part
for(int j=1;j<=(row-i+1);j++){
cout <<(char)('A'+j-1);
}
//space
for(int spc=1;spc<=(2*(i-1)-1);spc++){
cout <<" ";
}
//right part
for(int j=row-i+1;j>=1;j--){
cout <<(char)('A'+j-1);
}
cout <<endl;
}
return 0;
}
------------------------------

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 3 3 1

1 2 1

1 1

CODE:

#include<iostream>
using namespace std;
//pascals triangle
int main()
{
int row=9,temp=1;
for(int i=0;i<(row+1)/2;i++){
for(int j=1;j<=(row+1)/2-i;j++)
cout<<" ";
cout<<temp<<" ";
for(int k=0;k<i;k++){
temp=(temp*(i-k))/(k+1);
cout<<temp<<" ";
}
cout<<endl;
}
for(int i=(row-3)/2;i>=0;i--){
for(int j=0;j<=(row-1)/2-i;j++)
cout<<" ";
cout<<temp<<" ";
for(int k=0;k<i;k++){
temp=(temp*(i-k))/(k+1);
cout<<temp<<" ";
}
cout<<endl;
}
return 0;
}

-------------------------------------------------

* * *

* * * * *

* * * * * * *

* * * * *

* * *
*

CODE:

#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter number of rows:"<<endl;
cin>>n;
for(int i=1;i<=n;i++){
for(int spc=i;spc<n;spc++){
cout <<" ";
}
for(int j=1;j<=(2*i-1);j++){
cout<<"*";
}
cout <<endl;
}
for(int i=(n-1);i>=1;i--){
for(int spc=i;spc<n;spc++){
cout <<" ";
}
for(int j=1;j<=(2*i-1);j++){
cout<<"*";
}
cout <<endl;
}
return 0;
}
=====================================================================================

18) An equation of the form ax^2+bx+c=0 is known as quadratic equation. The values of x that satisfy

the equation are known as the roots of the equation. Write a program to find out the roots of the

quadratic equation

CODE:

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,b,c;
cout <<"Enter the coefficient of x^2, x and x^0 : ";
cin >>a>>b>>c;
float dis= b*b-4*a*c;
if(dis <0){
cout <<endl;
cout <<"The roots of equation "<<a<<"x^2 + "<<b<<"x + "<<c<<" are: ";
cout <<endl;
cout << (b*-1)/(2*a)<<" + "<<(sqrt(dis*-1)/(2*a))<<"i"<<endl;
cout << (b*-1)/(2*a)<<" - "<<(sqrt(dis*-1)/(2*a))<<"i"<<endl;
}else{
cout <<endl;
cout <<"The roots of equation "<<a<<"x^2 + "<<b<<"x + "<<c<<" are: ";
cout <<endl;
cout << (b*-1+sqrt(dis))/(2*a)<<endl;
cout << (b*-1-sqrt(dis))/(2*a)<<endl;
}
return 0;
}
=====================================================================================

19) Write a program to find out the sum of the following series (up-to 30th term):

CODE:

#include<iostream>
using namespace std;
int main(){
int x;
cout <<"Enter value of x : ";
cin >>x;
if(x==0){
cout <<"The required sum is 0";
return 0;
}
float sum=0.0;
int sign =1,pro=x,fac=1;
for(int i=1;i<=30;i=i+2){
sum=sum+sign*((pro+0.0)/fac);
pro=pro*x*x;
sign*=-1;
fac=fac*(i+1)*(i+2);
}
cout <<"The required sum is "<<sum<<endl;
return 0;
}
=====================================================================================

20) Write a menu driven program which has following options

1-Factorial of a number

2-Prime or not

3-Odd or even

4. Nth Fibonacci number

5-Exit

Once a menu item is selected the appropriate option should be taken and once this option is finished,

the menu should reappear .Unless the user selects the Exit option the program should continue work.

CODE:

#include<iostream>
using namespace std;
int main(){
while(true){
cout<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Please Enter your option :"<<endl;
cout<<"1- Factorial of a number \n";
cout<<"2- Prime or not \n";
cout<<"3- Odd or even \n";
cout<<"4- Nth Fibonacci number \n";
cout<<"5- Exit\n==> ";
int opt;
cin >>opt;
if(opt==5){
return 0;
}
if(opt<1 || opt>5){
continue;
}
switch(opt){
case 1:{
int num;

cout<<"Enter any positive number: ";


cin >> num;
if(num<0){
cout<<"Invalid Input";
continue;
}
int fac=1;
for(int i=2;i<=num;i++)
fac*=i;
cout<<"The Factorial of "<<num<<" is "<<fac;
break;
}
case 2:{
int num;
cout<<"Enter any postive number: ";
cin >> num;
if(num<=0){
cout<<"Invalid input";
continue;
}
if(num ==1){
cout<<"Neither prime nor composite";
continue;
}
int flag=1;
for(int i=2;i<num;i++){
if(num%i==0){
flag=0;
break;
}
}
if(flag==1){
cout<<"Prime number";
}else{
cout<<"Not a Prime number";
}
break;

}
case 3:{
int num;
cout<<"Enter any non negative number: ";
cin >>num;
if(num<0){
cout<<"Invalid Input";
continue;
}
if(num%2==0){
cout<<"An even number";
}else{
cout<<"An odd number";
}
break;
}
case 4:{
int n;
cout<<"Enter value of n: ";
cin >>n;
if(n<=0){
cout<<"Not A Valid Input";
continue;
}
int a=0,b=1;
for(int i=1;i<n;i++){
b=a+b;
a=b-a;
}
cout<<"The "<<n<<"th fibonacci number is "<<a;
break;
}

default:
break;

}
}
=====================================================================================

21) A user enters integers until end of input and wants to find the largest number in the list of integers

entered and the number of times it was entered. For example, if the input is 5, 2, 15, 3, 7, 15, 8, 9, 5,

2, 15, 3, and 7, the largest is 15 and is repeated 3 times. Write an algorithm to compute frequency of

the largest of the integers entered without storing them. Convert the algorithm to a function that

displays the integers read and returns the largest number and its frequency.

CODE:

#include<iostream>

using namespace std;

void check(int &frequency,int &largest,int);

int main(){

int num;

char chk;

int frequency=0,largest;

cout<<"Enter a number: ";


cin >> num;

largest=num;frequency=1;

cout<<"Do you want to continue(Y/N) :";

cin >>chk;

if(chk=='N'||chk=='n'){

cout<<"The largest number is "<<largest<<endl;

cout<<"And the frequency is "<<frequency;

return 0;

do{

cout<<"Enter a number: ";

cin >> num;

check(frequency,largest,num);

cout<<"Do you want to continue(Y/N) :";

cin >>chk;

if(chk=='N'||chk=='n'){

break;

}while(true);

cout<<endl;

cout<"----------------------------------------";

cout<<"The largest number is "<<largest<<endl;

cout<<"And the frequency is "<<frequency;

return 0;
}

void check(int &frequency,int &largest,int num){

if(num==largest){

frequency+=1;

}else if(num >largest){

largest=num;

frequency=1;

=====================================================================================

22) Write a program that reads an integer -n (decimal number system) and convert this decimal

number to Binary, Octal, and Hexadecimal form.

CODE:

#include<iostream>

using namespace std;

void binary(int);

void octal(int);

void hexa(int);

int main(){

int dec;

cout<<"Enter number in decimal system: ";

cin >>dec;

binary(dec);
octal(dec);

hexa(dec);

return 0;

void binary(int n){

long long int rev=0;

int temp=0;

while(n){

rev=rev*10+n%2;

n=n/2;

temp++;

long long int bin=0;

while(temp){

bin=bin*10+rev%10;

rev=rev/10;

temp--;

cout<<"The value in binary is "<< bin;

cout<<endl;

return;

void octal(int n){

long long int rev=0;

int temp=0;
while(n){

rev=rev*10+n%8;

n=n/8;

temp++;

long long int oct=0;

while(temp){

oct=oct*10+rev%10;

rev=rev/10;

temp--;

cout<<"The value in octal is "<< oct;

cout<<endl;

return;

void hexa(int n){

char arr[100];

int index=0;

while(n){

int dig = n%16;

if(dig<=9){

arr[index]=(char)('1'+dig-1);

index++;

}else{
switch(dig){

case 10:

arr[index]='A';

index++;

break;

case 11:

arr[index]='B';

index++;

break;

case 12:

arr[index]='C';

index++;

break;

case 13:

arr[index]='D';

index++;

break;

case 14:

arr[index]='E';

index++;

break;

case 15:

arr[index]='F';

index++;
break;

default:

break;

n=n/16;

cout<<"The value in binary is ";

for(int i=index-1;i>=0;i--){

cout<<arr[i];

cout<<endl;

return;

=====================================================================================

23) Given 3-angles. write a program to check whether they form a triangle or not (A+B+C =180). If yes
check whether triangle is scalen, equilateral, isoceless or right angled triangle.

CODE:

#include<iostream>

using namespace std;

int main(){

int a,b,c;

cout<<"Enter three angles of any triangle: ";

cin >>a>>b>>c;

if(a+b+c==180){

if(a==b&&a==c){

cout<<"An equilateral triangle can be formed";

}else if(a==b || a==c || b==c){

cout<<"An Isosceles triangle can be formed";

}else{

cout<<"A Scalene triangle can be formed";

}else{

cout<<"A triangle cant be formed";

return 0;

=====================================================================================

24) The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1 and Fi+2=Fi+Fi+1 i=0,1,....n Write a

program to find the Fibonacci value of the given number.


CODE:

#include<iostream>

using namespace std;

int main(){

int n;

cout<<"Enter any Fibonnaci number: ";

cin >>n;

int a=0,b=1;

int index=0;

while(a<n){

b=a+b;

a=b-a;

index++;

if(a==n){

cout<<"The Fibonacci value of given number is: "<<index;

}else{

cout<<"It is not a fibonacci number";

return 0;

=====================================================================================

25) Write a program to print all the ASCII values and their equivalent characters using a while loop.

CODE:
#include<iostream>

using namespace std;

int main(){

int i=0;

cout <<"ASCII VALUE\t"<<"CHARACTER"<<endl;

cout<<"----------------------------------------------------------";

cout<<endl;

//printable ascii characters from 32 to 126

for(int i=32;i<=126;i++){

cout<<i<<"\t\t "<<(char)i;

cout<<endl;

i++;

=====================================================================================

26) A way to calculate the value of π is based on the use of a series defines as

follows(N- number of terms). Write a program to find π value (up to n terms and

display the result by correcting it to three decimal places):

CODE:

#include<iostream>

#include <iomanip>

using namespace std;

int main(){

int n;

cout<<"Enter value of n: ";


cin >>n;

float pi=0;

int sign=1;

for(int i=0;i<=n;i++){

pi+=(sign+0.0)/(2*i+1.0);

sign*=-1;

pi=pi*4;

cout<<setprecision(3)<<fixed;

cout<<pi<<endl;

=====================================================================================

27) Write a program that accepts a year written as a four-digit numeral and outputs the year written

in Roman numerals. Important Roman numerals are V –5 , X-10 , L-50 , C-100, D-500 and M-1,000.

CODE:

#include<iostream>

using namespace std;

int main()

int n,a[4],i;

cout<<"enter a four digit year"<<endl;

cin>>n;

for (i=0;i<4;i++){

a[i]=n%10;
n=n/10;

for (i=1;i<=a[3];i++)

cout<<"M";

if (a[2]<5){

for (i=1;i<=a[2];i++)

cout<<"C";

}else if (a[2]==5)

cout<<"D";

else if (a[2]>5){

cout<<"D";

for (i=1;i<=(a[2]-5);i++)

cout<<"C";}

if (a[1]<5){

for (i=1;i<=a[1];i++)

cout<<"X";

}else if (a[1]==5)

cout<<"L";

else if (a[1]>5){

cout<<"L";

for (i=1;i<=(a[1]-5);i++)

cout<<"X";

if (a[0]<4){

for (i=1;i<=a[0];i++)
cout<<"I";

else if (a[0]==4)

cout<<"IV";

else if (a[0]==5)

cout<<"V";

else if (a[0]>5&&a[0]<9){

cout<<"V";

for (i=1;i<=(a[0]-5);i++)

cout<<"I";

}else if (a[0]==9)

cout<<"IX";

return 0;

=====================================================================================

28) Write a function power(a,b) , to calculate the value of a raised to b.

CODE:

#include<iostream>

using namespace std;

long long int pow(int,int);

int main(){

int base,exponent;

cout<<"Enter the value of base: ";

cin >> base;


cout<<"Enter the exponent : ";

cin>>exponent;

long long int result = pow(base,exponent);

cout<<endl;

cout<<base<<"^"<<exponent<<" = "<<result;

long long int pow(int b,int e){

long long int pro=1;

for(int i=1;i<=e;i++){

pro*=b;

return pro;

=====================================================================================

29) A positive integer is entered through the keyboard, write a function to find the binary equivalent

this number:

CODE:

#include<iostream>

using namespace std;

long long int binary(int);

int main(){

int dec;

cout<<"Enter any decimal number: ";

cin >>dec;

long long int bin=binary(dec);


cout<<"The binary equivalent of the number is: "<<bin;

return 0;

long long int binary(int dec){

long long int rev=0,org=0;

int dig=0;

while(dec){

rev=rev*10+dec%2;

dec/=2;

dig++;

while(dig){

org=org*10+rev%10;

rev/=10;

dig--;

return org;

=====================================================================================

30) Write a program for towers of Hanoi, using recursive function

CODE:

#include<iostream>
using namespace std;

void tower(int,char,char,char);

int main(){

int n;

cout<<"Enter number of discks to be moved: ";

cin>>n;

char from, to,left;

cout<<"Enter the initial place of the disks: ";

cin>>from;

cout<<"Enter the final destination of the disk: ";

cin>>to;

cout<<"Enter the disk left out: ";

cin>> left;

cout<<"The disks are to be moved from position "<<from<<" to "<<to;

cout<<" using position "<<left;

cout<<endl;

cout<<"Follow The following instruction: "<<endl;

cout<<"----------------------------------------"<<endl;

tower(n,from,to,left);

void tower(int n,char a,char c,char b){

if(n==1){

cout<< "Move disk "<<n<<" from "<<a<<" to "<<c;

cout<<endl;
return;

tower(n-1,a,b,c);

cout<<"Move "<<"disk "<<n<<" from "<<a<<" to "<<c<<endl;

tower(n-1,b,c,a);

You might also like