Oct 5, 2013

Program: Display Fibonacci Series IN C++

Program : DISPLAY FIBONACCI SERIES IN C++

Check out Program

#include<iostream.h>
#include<conio.h>
// Display Fibonacci Series 
// In Fibonacci Series first two numbers in the Fibonacci series are 0 and 1, and each subsequent number is the sum of the previous two.
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; // n store the integer value that series will shown upto n terms
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
/* First we Print first Two Term of Series then using for loop we generate rest of series upto n term. Let Begin with loop first it begin from 0 till the n-2 term because we already print first two term of series, then inside loop we initialize c = a + b, c contain value sum of a + b which become the third term of series. In Fibonacci series sum of previous two term is the next term. After getting value of next term and store in variable c. Then swapping of variable take place, now we don't require first term for next term(i.e. 4th term) so we replace value of a with b and replace value of b with c. Now 1st contain 2nd and 2nd contain 3rd, so they can generate 4th and so on. This process going on until the nth term of series. It will Print output after every assigning value of c.
*/
getch(); //http://cplspls.blogspot.com
return 0; 
}

OUTPUT:




Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
int a=0,b=1, c,n; 
cout << "Enter the value of n ";
cin >> n; 
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
getch(); //http://cplspls.blogspot.com
return 0; 
}

Oct 4, 2013

Program: Find Factorial of a Given Number

Program : FIND FACTORIAL OF A GIVEN NUMBER

Program to find factorial of a number using For Loop
Check out Program

#include<iostream.h>
#include<conio.h>
// Find factorial of a Given Number
int main()
{
long double factorial = 1; /* factorial has long double type which take 8 bytes in memory and it can store number within range of 1.7E -308 to 1.7E + 308 */
int n;
cout <<"Enter a Number : "<<endl; // endl for new line
cin >> n; // Take Integer value from user and store in variable n 

for(int i =n; i>0; i--)
     factorial *= i;
/* You Know Factorial is product of all positive number integers less than or equal to n. In above loop we initialize i equal to value of n that is taken from user, then set condition that i > 0 because we do multiplication here if i = 0 then it will give result 0. Then in loop statement is factorial * = i , it means that factorial = factorial * i , Now we already assign factorial value as 1, if we don't assign value 1 it will give error or junk value, because we are doing multiply if there is no number to multiply with it give junk value. In this we use decrement of value of i from n to 1, so it will multiply number like this n * (n-1) * (n-2) * ... * 2 * 1 . In this loop i didn't put braces because any loop or condition if you didn't put braces then it will automatic consider next line or till the termination(;) it will consider that part inside loop. So next line to loop inside the loop after that loop scope is finished.
*/
cout << n << "! = " << factorial <<endl; 
getch(); //http://cplspls.blogspot.com
return 0;
}

OUTPUT:






Above Program Without Comments
#include<iostream.h>
#include<conio.h>
int main()
{
long double factorial = 1; 
int n;
cout <<"Enter a Number : "<<endl; 
cin >> n; 

for(int i =n; i>0; i--)
     factorial *= i;

cout << n << "! = " << factorial <<endl; 
getch(); 
return 0;
}