New microsoft office word document (2)

3
/* Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89... */ #include<stdio.h> #include<conio.h> void main() { clrscr(); unsigned i,n=25,a=1,fib; for(i=0;i<n;i++) { printf("%u ",fib*a); a++; } getch(); } fib(unsigned n) { if(n==0) return 0; if(n==1) return 1; else return fib(n-1)+fib(n-2); } Out Put /* A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number: (1) Without using recursion (2) Using recursion */ #include<stdio.h>

Transcript of New microsoft office word document (2)

Page 1: New microsoft office word document (2)

/* Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:1 1 2 3 5 8 13 21 34 55 89... */

#include<stdio.h>#include<conio.h>void main(){clrscr();unsigned i,n=25,a=1,fib;for(i=0;i<n;i++) {printf("%u ",fib*a);a++;}getch();}fib(unsigned n) {if(n==0)return 0;if(n==1)return 1;elsereturn fib(n-1)+fib(n-2);}

Out Put

/* A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number:(1) Without using recursion(2) Using recursion */

#include<stdio.h>#include<conio.h>void main(){clrscr();long int n,s=0,b,sum;printf("Enter any 5-digit number: ");scanf("%ld",&n);

Page 2: New microsoft office word document (2)

printf("\n\nChoose : 1: sum of digits without recursively\n\n");printf("Or Choose : 2: sum of digits recursively\n\n\n");printf("Your choice is = ");b=getche();switch(b) {case '1':while(n!=0) {s=s+(n%10);n=n/10;}printf("\n\n\nThe sum of digits is = %d\n",s);break;case '2':s=sum+n;printf("\n\n\nThe Sum of digits is = %d\n",s);break;}getch();}sum(long n) {static s=0;if(n==0)return s;else {s=s+n%10;n=sum(n/10);return n;}}

Out Put