Series Printing coding 3

CODE NO:- 15

Q)Write a program to display the sequence 1,2,2,4,8,32,256.....

#LOGIC
If you observe this series carefully you will find this is somewhat same as the Fibonacci series with minute change instead of "+" between the two previous terms we are having "*". 

NOTE: Considering starting index as 0 (zero).

For example: we got 2 by multiplying 1 and 2 i.e we got 2nd term by multiplying 1st term and 0th term.

#Program (In C++):

In  C++ language we use  ' // '  for Single line comment and /*---*/ for multi line comment

#include<iostream> // header file for i/p and o/p
using namespace std;
int main()
{
int n,re,a=1,b=2,i;
cout<<"\n enter the value of n (no. of terms):";
cin>>n;
cout<< a <<" "<< b ;
for(i=2;i<n;i++) // here i=2 because first two terms we had already printed (a,b)
{
re=a*b;
cout<<" "<<re ;
a=b;
b=re;
}
return 0;
}

We got this output by using DEV C++ (IDE)



Link: For other series printing problems

Comments

Post a Comment

Popular posts from this blog

WHY PROGRAMMING?

Series code 2

Pattern Printing coding 7