google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.

Monday, March 3, 2014

Program in C++ for Fibonacci Series using recursion function

#include<iostream.h>
fibonacci(int);            //function prototype
void main()
{
int n,i,f;
cout<<"Enter the total elements in the series : ";
cin>>n;
cout<<"\nThe Fibonacci series is:\n";
for(i=0;i<n;i++)
    {
        f=fibonacci(i); //function call
        cout<<f<<"  ";
    }
}

fibonacci(int n)        //function definition
{
    int f;
    if(n==0)
    return 0;
    else if(n==1)
    return 1;
    else
    f=fibonacci(n-1)+fibonacci(n-2);  /*Two recursion function calling itself with different arguments.*/
    return f;
}

-----------------OUTPUT---------------

Enter the total elements in the series :  7
The Fibonacci series is:
0    1    1    2    3    5    8

Note: -When a function call itself within its body is known as recursion function .The function calling has different arguments.
Fibonacci Series means addition of previous two numbers example 0+1=1 , 1+1=2 , 1+2=3 ,2+3=5 , 3+5 =8 , and so on .So the Fibonacci Series is 0,1,1,2,3,5,8,13,21,34


Click here for more programs on C++

Saturday, March 1, 2014

Using power function display armstrong number

#include<iostream.h>
#include<math.h>
int main()
{
int sum,n,temp,rem;
cout<<"The Armstrong number between the range 1 to 500 are : \n"; 
for(int x=1;x<=500;x++)
{
n=x;
sum=0;
temp=n;
do
{
rem=n%10;
n=n/10;
int p;
p=pow(rem,3);
sum=sum+p;
}while(n!=0);
if(sum==temp)
cout<<x<<"\t";
}
cout<<"\t";
return 0;
}

Output:
The Armstrong number between the range 1 to 500 are : 
1      153     370     371     407
 

अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...