Programming Exercise Looping Structure P-7

Programming Exercise Looping Structure P-6

Conditional Structure Programming Exercise P-6

Q.28 Write a program which reads an integer value from keyboard and checks whether the input value is Armstrong Number or Not.
// Note: The number will be Armstrong if sum of cubes of its all digits is equal to the number itself e.g. 153 = (1*1*1) + (5*5*5) + (3*3*3)

#include<iostream.h>
#include<conio.h>
main()
{
long n, r, num, sum = 0;
clrscr();
cout<<" Enter a number ? ";
cin>>n;
num = n;
while(n!=0)
{
r = n%10;
sum = sum+(r*r*r);
n = n/10;

}
cout<<"Sum of cubes of digits of number is:"<<sum<<endl;
if(sum == num)
cout<<" It is Armstrong Number ";
else
cout<<" It is not Armstrong Number ";
getch();
}

Q.29 Write a program that provides three functionalities for checking number’s domain such as it is a Prime, Perfect or Arm Strong Number. Each functionality should be implemented separately and presented by options.

#include<iostream.h>
#include<conio.h>
main()
{
int n, op;
clrscr();
cout<<" Enter a number ? ";
cin>>n;
cout<<"1- Check Prime "<<endl;
cout<<"2- Check Arm Strong number "<<endl;
cout<<"3- Check Perfect number "<<endl;
cout<<"Enter your option [1-3] ?"<<endl;
cin>>op;
switch(op)
{

case 1:
int d = 2, p = 1;

while(d < n)
{
if (n%d ==0)
{ 
p = 0;
break;
}
d++;
}
if(p == 1) 
cout<<n<<" is a prime number";
else
cout<<n<<" is a composite number";
break;


case 2:
int r, num, sum = 0;
num = n;
while(n!=0)
{
r = n%10;
sum = sum+(r*r*r);
n = n/10;

}
if(sum == num)
cout<<" It is Armstrong Number ";
else
cout<<" It is not Armstrong Number ";
break;


case 3:

int i = 1,s = 0;
while(i<=n/2)
{
if(n%i==0)
s = s + i;
i++;
}
if(s==n)
cout<<n<<" is perfect number";
else
cout<<n<<" is not perfect number";
break;

} 
getch();
}

Q.30 Write a program that displays the following output using the while loop:
// 1, 4, 16, 64, 256, 1024

#include<iostream.h>
#include<conio.h>
main()
{
int n = 1;
clrscr();
while(n<=1024)
{
cout<<n<<endl;
n=n*4;
}
getch();
}

Q.31 Write a program that displays the following output using the while loop:
// 1, 5, 25, 125, 625, 3125

#include<iostream.h>
#include<conio.h>
main()
{
int n =1;
clrscr();
while(n<=3125)
{
cout<<n<<endl;
n*=5;
}
getch();
}

Q.32 Write a program that displays the following output using the while loop:
// 1, 6, 36, 216, 1296

#include<iostream.h>
#include<conio.h>
main()
{
int n =1;
clrscr();
while(n<=1296)
{
cout<<n<<endl;
n*=6;
}
getch();
}

Q.33 Write a program that displays the following output using the while loop:
// 1, 7, 49, 343, 2401

#include<iostream.h>
#include<conio.h>
main()
{
int n =1;
clrscr();
while(n<=2401)
{
cout<<n<<endl;
n*=7;
}
getch();
}

Q.34 Write a program that displays the following output using the while loop:
// 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =1024;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/2;
}
getch();
}

Q.35 Write a program that displays the following output using the while loop:
// 1024, 256, 64, 16, 4, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =1024;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/4;
}
getch();
}

Q.36 Write a program that displays the following output using the while loop:
// 729, 243, 81, 27, 9, 3, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =729;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/3;
}
getch();
}

Q.37 Write a program that displays the following output using the while loop:
// 3125, 625, 125, 25, 5, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =3125;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/5;
}
getch();
}

Q.38 Write a program that displays the following output using the while loop:
// 1296, 216, 36, 6, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =1296;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/6;
}
getch();
}

Q.39 Write a program that displays the following output using the while loop:
// 2401, 343, 49, 7, 1

#include<iostream.h>
#include<conio.h>
main()
{
int n =2401;
clrscr();
while(n>=1)
{
cout<<n<<endl;
n =n/7;
}
getch();
}

Q.40 Write a program that calculates the sum of squares of all odd numbers between 1 and 100 using the while loop:

#include<iostream.h>
#include<conio.h>
main()
{
long n =1, sum =0;
clrscr();
while(n<=100)
{
if(n%2==1)
sum = sum + n*n;
n++;
}
cout<<"Sum of square of odd numbers is : "<<sum<<endl;
getch();
}

Leave a Reply