Sunday, 5 May 2013

Series


1) * * * * * *                         1 1 1 1 1 1
    * * * * *                            1 1 1 1 1
    * * * *                               1 1 1 1
    * * *                                  1 1 1
    * *                                     1 1
    *                                        1



Assume n is number of * or 1 in first row


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;
int main ()
{
  int i,n,j;
  cout << "Input Value of n:";
  cin>>n; 
  for(i=n;i>0;i--)
  {
    for(j=0;j<i;j++) 
      /*Print 1 or * or as per ur requriment */
      cout<<"*"<<"\t"; 
    }       
    cout<<endl;   //new line
}
 return 0;
}
Input Value of n:3
 
* * *
* *
* 

Fibonacci series

         0 1 1 2 3 5 8 13 21 ..........


PROGRAM                                                 OUTPUT
#include <iostream>
using namespace std;
int main()
{
int a,b,c,n;
a=0; b=1;
cout<<"No. of terms in series:";
cin>>n;
cout<<a<<"\t"<<b;
for(int i=0;i<n;i++){
c=a+b;  a=b;  b=c;
cout<<c;    //print series term
cout<<"\t"; //tab
}
return 0;
}

No. of terms in series :9

0 1 1 2 3 5 8 13 21