C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
C program for Fibonacci series using recursion
Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples
Subscribe to:
Post Comments (Atom)
25 comments:
void main()
{
int a,b,c,i,n;
a=0;
b=1;
printf("Enter number of terms of the series to be generated:");
scanf("%d",&n);
if(n==1)
{
printf("\n FIBONACCI SERIES\n");
printf("%d",a);
}
else if(n==2)
{
printf("\n FIBONACCI SERIES\n");
printf("%d\t%d",a,b);
}
else
{
printf("%d\t%d",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
}
getch();
}
OR
void main()
{
int a,b,c,i,n;
clrscr();
a=0;
b=1;
printf("\n enter n for how many times generate series");
scanf("%d",&n);
printf("\n FIBONACCI SERIES\n");
printf("\t%d\t%d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
getch();
}
Find non fibonesis serize using function
#include
#include
Void main( )
{
Int n, i, c, a = 0, b = 1;
printf(“Enter Fibonacci series of n’th term”);
scanf(“%d”, & n);
printf(“%d%d”, a, b);
for ( i = 0; I <= (n-3); i++)
{
c = a + b
a = b
b = c
printf(“%d”,c);
}
getch( );
}
Out put:
Enter the Fibonacci series of n’th term : 7
0112358
//in c++
//this is short code of Fibonacci function in recursion..
int fib(int n){
if(n==0){
return 0;
}else if(n==1){
return 1;
}else{
return fib(n-1)+fib(n-2);
}
}
Void main( )
{
cout<<fib(n)<<endl;
}
How can I tell if a given source code has used recursion or not, when there is a function declared?
This is really helpful.
For Dynamic Programming this seems 2 be d brief one...
#include
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n>0){ // there is error it should be if(n>2)since we have already printed two digit 0 1
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
plz..
Check the logic is wrong
There is a better solution for fibo recursive:
Notes:
Call it with n > 0 and 0, 1 as initial fibo elements.
Print a 0 before the call.
The code:
void fibo_recursive(int n, int i, int j)
{
if (n > 0)
{
printf("%d, ", j);
int temp = j;
j = j + i;
i = temp;
fibo_recursive(--n, i, j);
}
}
its not working
Make it n-2 in for statement
thankssssss
#include
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n==0)
return;
if(n==1)
printf("%d",first);
else
printf("%d",first);
sum=first+second;
first=second;
second=sum;
printFibonacci(n-1);
}
#include
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n==0)
return;
if(n==1)
printf("%d",first);
else
printf("%d",first);
sum=first+second;
first=second;
second=sum;
printFibonacci(n-1);
}
no the logic seems to work perfectly
#include
int fib(int n)
{
if (n==1)
return 1;
else if (n==0)
return 0;
else
return (fib(n-1)+fib(n-2));
}
int main()
{
int number,i=0;
printf("Enter Number for fib series : ");
scanf("%d",&number);
while(i<number)
{
printf("%d ",fib(i));
i++;
}
return 0 ;
}
#include
void main()
{
int i,n;
int fab(int);
printf("Enter numbers of terms of Fabonnacci series :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d, ",fab(i));
}
printf("\n");
}
int fab(int n)
{
if(n-2<=0)
return(1);
else
return(fab(n-1)+fab(n-2));
}
what is the meaning of long int or static long in recursion program
what is program & why using program
write a program 1 2 2 4 8 32 series like that display
the declaration of variables first,second and sum should be made global to get correct output
I don't like to print 0 and 1 manually....so try this...
void printFibonacci(int n){
static long int first=-1,second=1,sum;
if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
int fibo(int n)
{
if(n<=1)
return n;
else
return (fibo(n-1)+fibo(n-2));
}
main()
{
n=6;
for(i=0;i<n;i++)
printf("%d",fibo(i));
}
Post a Comment