6.) Write a program using switch case statement for following option.
// program using switch case statement for following option:
// a) factorial of a number.
// b) prime or not
// c) Odd or Even
// d) exit
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
char ch;
int num, i;
while (1)
{
int fact =1;
printf("Enter the choice where a)
calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit :\n");
scanf(" %c", &ch);
switch (ch)
{
case 'a':
printf("Enter the number :\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
printf("Factorial of %d! is %d\n", num, fact);
break;
case 'b':
printf("Enter the number:\n");
scanf("%d", &num);
i = 2;
while (i < num)
{
if (num % i == 0)
{
printf("%d is not a prime\n", num);
break;
}
i += 1;
}
if (i == num)
{
printf("%d is a prime\n", num);
}
break;
case 'c':
printf("Enter the number:\n");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("%d is an Even Number\n", num);
}
else
{
printf("%d is an Odd Number\n", num);
}
break;
case 'd':
exit(0);
default:
printf("Invalid choice!\a\n");
}
}
return 0;
}
Output:-
Enter the choice where
a) calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit :
a
Enter the number :
5
Factorial of 5! is 120
Enter the choice where
a) calculate the factorial of a number,
b) check the number is prime or not ,
c) odd or even ,d) exit :
d // Exit .
******* Thank you ******
Comments
Post a Comment