Computer Science, asked by arpitaagrahari346, 4 months ago

Using a switch statement write a menu driven program to:
Generate and display the first n terms of the Fibonacci series
0,1,1.2.3.5.......
The first two Fibonacci number are 0 and 1, and each subsequent number is the
sum of the previous two numbers.
Find the sum of the digits of an integer that is input by the user
Sample Input
15390
Sample Output
sum of digit 18
For an incorrect choice, and appropriate error message should be displayed.​

Answers

Answered by shailkanch25
3

Answer:

int sum_of_digits(int n)

{

int sum=0,a;

while (n!=0)

{

a=n%10;

sum+=a;

n=n/10

}

return sum;

}

void fibonacci(int n)

{

int a=0,b=1,c;

while(n>0)

{

printf("%d",a);

c=a+b;

a=b;

b=c;

}

}

int main()

{

int c,n;

printf("enter your choice /n1-fibomacci /n2- sum of digits");

scanf("%d",&c);

switch (c)

{

case 1:

printf("enter the value of n");

scanf("%d",&n);

fibonacci(n);

break;

case 2 :

printf(" enter the number");

scanf("%d",&n);

printf( "%d",sum_of_digits(n));

break;

default:

printf("invalid choice")

}

}

Explanation:

code is written in C .

simply changing the syntax would be required if you want some other language....

hope logic is cleared...thank you

Similar questions