This document contains code to calculate the Fibonacci sequence up to 25 numbers recursively and code to calculate the sum of digits of a 5-digit number entered by the user either recursively or non-recursively. It includes functions to calculate the Fibonacci sequence recursively and to calculate the sum of digits of a number recursively and non-recursively. The code prompts the user to enter a 5-digit number and chooses whether to calculate the sum recursively or non-recursively.
1. /* 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>
#include<conio.h>
void main()
{
clrscr();
long int n,s=0,b,sum;
printf("Enter any 5-digit number: ");
scanf("%ld",&n);
2. printf("nnChoose : 1: sum of digits without recursivelynn");
printf("Or Choose : 2: sum of digits recursivelynnn");
printf("Your choice is = ");
b=getche();
switch(b) {
case '1':
while(n!=0) {
s=s+(n%10);
n=n/10;
}
printf("nnnThe sum of digits is = %dn",s);
break;
case '2':
s=sum+n;
printf("nnnThe Sum of digits is = %dn",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