Thursday, December 26, 2013

Assignment 2.3: flow of control Set 3 SOLUTION

Flow of Control
[SET – 3]

1
Write a program to print following : solution

i)
********************
**********
**********
ii)
***
***
****
*****
iii)
        *      **
    ***
  ****
*****


iv)
        *      ***
    *****
  *******
*********
v)
        1
      222
    33333
  4444444
555555555
vi)
        1
      212
    32123
  4321234
543212345

//Solution of (i)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j;
 for(i=1;i<=4;i++)
 {
  for(j=1;j<=10;j++)
   cout<<'*';
  cout<<endl;
 }

 getch();
 return 0;
}


//Solution of (ii)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j;
 for(i=1;i<=5;i++)
 {
  for(j=1;j<=i;j++)
   cout<<'*';
  cout<<endl;
 }

 getch();
 return 0;
}


//Solution of (iii)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<=i;k++)
   cout<<'*';
  cout<<endl;
 }

 getch();
 return 0;
}


//Solution of (iv)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<2*i;k++)
   cout<<'*';
  cout<<endl;
 }

 getch();
 return 0;
}


//Solution of (v)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j,k;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=1;k<2*i;k++)
   cout<<i;
  cout<<endl;
 }

 getch();
 return 0;
}


//Solution of (vi)
#include<iostream.h>
#include<conio.h>

int main()
{
 int i,j,k,l;
 for(i=1;i<=5;i++)
 {
  for(j=5;j>i;j--)
   cout<<' ';
  for(k=i;k>=1;k--)
   cout<<k;
  for(l=2;l<=i;l++)
   cout<<l;
  cout<<endl;
 }

 getch();
 return 0;
}

2
Write a program to compute sinx for given x. The user should supply x and a positive integer n. We compute the sine of x using the series and the computation should use all terms in the series up through the term involving xn
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........  
#include<iostream.h>
#include<conio.h>

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

 for(i=1;i<=n;i+=2)
 {
  fact=1;
  for(j=1;j<=i;j++)
  {
   p=p*x;
   fact=fact*j;
  }
  sign=-1*sign;
  sum+=sign*p/fact;
 }
 cout<<"sin "<<x<<"="<<sum;

 getch();
 return 0;
}
3
Write a program to compute the cosine of x. The user should supply x and a positive integer n. We compute the cosine of x using the series and the computation should use all terms in the series up through the term involving xn
cos x = 1 - x2/2! + x4/4! - x6/6! .....  
#include<iostream.h>
#include<conio.h>

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

 for(i=2;i<=n;i+=2)
 {
  fact=1;
  for(j=1;j<=i;j++)
  {
   p=p*x;
   fact=fact*j;
  }
  sum+=sign*p/fact;
  sign=-1*sign;
 }
 cout<<"cos "<<x<<"="<<1+sum;

 getch();
 return 0;
}

No comments:

Post a Comment