Thursday, December 26, 2013

Assignment 2.2: flow of control Set 2 SOLUTION

flow of control
[SET – 2]

1
Write a program to print number from 1 to 10. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int i=1;

 while(i<=10)
 {
  cout<<i<<"\n";
  i++;
 }

 getch();
 return 0;
}
2
Write a program to calculate the sum of first 10 natural number.
#include<iostream.h>
#include<conio.h>

int main()
{
 int i=1,sum=0;

 while(i<=10)
 {
  sum+=i;
  i++;
 }

 cout<<"Sum :"<<sum;

 getch();
 return 0;
}


3
Write a program to find the factorial value of any number entered through the keyboard. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,fact=1;
 cout<<"Enter any number : ";
 cin>>n;

 while(n>=1)
 {
  fact*=n;
  n--;
 }

 cout<<"Factorial :"<<fact;

 getch();
 return 0;
}



4
Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,p,r=1;
 cout<<"Enter the base number and exponent ";
 cin>>n>>p;

 for(int i=1;i<=p;i++)
  r=r*n;

 cout<<"Result :"<<r;

 getch();
 return 0;
}
5
Write a program to reveres any given integer number. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,t,r,rev=0;
 cout<<"Enter any number : ";
 cin>>n;
 t=n;

 while(t>0)
 {
  r=t%10;
  t=t/10;
  rev=rev*10+r;
 }

 cout<<"Reverse of number "<<n<<" is "<<rev;

 getch();
 return 0;
}
6
Write a program to sum of digits of given integer number. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,t,r,sum=0;
 cout<<"Enter any number : ";
 cin>>n;
 t=n;

 while(t>0)
 {
  r=t%10;
  sum+=r;
  t=t/10;
 }

 cout<<"Sum of digits of number "<<n<<" is "<<sum;

 getch();
 return 0;
}

7
Write a program to check given number is prime or not. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,flag=0;
 cout<<"Enter any number : ";
 cin>>n;

 for(int i=2;i<n;i++)
 {
  if(n%i==0)
  {
   flag=1;
   break;
  }
 }

 if(flag==0 && n>1)
  cout<<"Number is prime";
 else
  cout<<"Number is not prime";

 getch();
 return 0;
}


8
Write a program to calculate HCF of Two given number. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int dividend, divisor, rem, hcf;
 cout<<"Enter two numbers : ";
 cin>>dividend>>divisor;

 while(rem!=0)
 {
  rem=dividend%divisor;
  if(rem==0)
   hcf=divisor;
  else
  {
   dividend=divisor;
   divisor=rem;
  }
 }

 cout<<"HCF is : "<<hcf;

 getch();
 return 0;
}

9
Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.
#include<iostream.h>
#include<conio.h>

int main()
{
 int n, sum_p=0, sum_n=0, sum_z=0;
 char choice;

 do
 {
  cout<<"Enter number ";
  cin>>n;

  if(n>0)
   sum_p++;
  else if(n<0)
   sum_n++;
  else
   sum_z++;

  cout<<"Do you want to Continue(y/n)? ";
  cin>>choice;

 }while(choice=='y' || choice=='Y');


 cout<<"Positive Number :"<<sum_p<<"\nNegative Number :"<<sum_n<<"\nZero Number :"<<sum_z;

 getch();
 return 0;
}

10
Write a program to enter the numbers till the user wants and at the end it should display the maximum and minimum number entered.
#include<iostream.h>
#include<conio.h>

int main()
{
 int n, max=0, min=32767;
 char choice;

 do
 {
  cout<<"Enter number : ";
  cin>>n;

  if(n>max)
   max=n;
  if(n<min)
   min=n;

  cout<<"Do you want to Continue(y/n)? ";
  cin>>choice;

 }while(choice=='y' || choice=='Y');


 cout<<"Maximum Number :"<<max<<"\nMinimum Number :"<<min;

 getch();
 return 0;
}


11
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 )
#include<iostream.h>
#include<conio.h>

int main()
{
 int n,digit1,digit2,digit3;

 for(int i=1;i<=500;i++)
 {
  digit1=i/100;
  digit2=i/10 - digit1*10;
  digit3=i%10;

  if(digit1*digit1*digit1 + digit2*digit2*digit2 + digit3*digit3*digit3 == i)
   cout<<i<<endl;
 }

 getch();
 return 0;
}

12
Write a program to print Fibonacci series of n terms where n is input by user : 0 1 1 2 3 5 8 13 24 ..... 
#include<iostream.h>
#include<conio.h>

int main()
{
 int f=0,s=1,t,n;
 
 cout<<"Enter Number of terms of Series : ";
 cin>>n;
 
 cout<<f<<" "<<s<<" ";
 
 for(int i=3;i<=n;i++)
 {
  t=f+s;
  cout<<t<<" ";
  f=s;
  s=t;
 }
 
 getch();
 return 0;
}

13
Write a program to calculate the sum of following series where n is input by user. 
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n #include<iostream.h>
#include<conio.h>

int main()
{
 int i,n;
 float sum=0;

 cout<<"Enter the value of n ";
 cin>>n;

 for(i=1;i<=n;i++)
  sum += 1.0/i;

 cout<<"Sum : "<<sum;

 getch();
 return 0;
}


14
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user. 
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,n,sign=-1;
 float sum=0;
 cout<<"Enter the value of n ";
 cin>>n;

 for(i=1;i<=n;i++)
 {
  sign *= -1;
  sum += sign*1.0/i;
 }
 cout<<"log 2 : "<<sum;

 getch();
 return 0;
}

No comments:

Post a Comment