Program using switch statement to find factorial of natural, number
Answers
Answered by
2
Answer:
include<stdio.h>
int main()
{
int number;
printf("Enter number\n");
scanf("%d",&number);
int choice;
printf("Enter 1 for summation or 2 for factorial\n");
scanf("%d",&choice);
int sum = 0;
int fact = 1;
switch(choice)
{
case 1:
while(number>0)
{
sum = sum+(number%10);
number = number/10;
}
printf("%d\n",sum);
break;
case 2:
while (number>0)
{
fact = fact*number;
number--;
}
printf("Factorial is %d\n",fact);
break;
default:
printf("Error\n");
}
return 0;
}
Similar questions
Computer Science,
22 hours ago
Science,
22 hours ago
Computer Science,
22 hours ago
English,
7 months ago
Computer Science,
7 months ago
English,
7 months ago