Factorial is the continuous multiplication of the number from 1,2,…upto the number. Using recursion concept, this can be achieved easily.
Program
:”factrec.c”
#include<stdio.h>
#include<conio.h>
int factorial(int
n)
{
if
(n<=1)
return(1);
else
return(n*factorial(n-1));
}
void main()
{
int
n,fact=0;
printf(“
Enter the number”);
scanf(“%d”,&n);
fact= factorial(n);
printf(“The
factorial of %d is : %d”,n,fact);
getch();
}
Output:
Enter the
number 5
The
factorial of 5 is 120
Now,let us
try some interesting number series called Fibonacci series….
First,get
the number of elements for the series as ‘n’.
Using a
loop, repeat the process until the number reaches to n.
Add the
last two number and print the number.
Once the
loop is finished, close the program.
Now, the
program begins….
“Fibonacci.c”
#include <stdio.h>
int fibonacci(int x)
{
if (x==0) { return(0); }
else if(x==1) { return( 1);}
return fibonacci(x-1)+fibonacci(x-2);
}
void main()
{
int n,x;
printf("Enter the number of elements");
scanf("%d",&n);
for(x=0;x<n;x++)
{
printf("%d \t \n" ,fibonacci(x);
}
getch();
}
the output is given below....
Enter the number of elements 7
0
1
1
2
3
5
8
No comments:
Post a Comment