Question #40439

Make use of switch statement. The outline of this program is given below:

/* A menu driven program */

void main

{

int choice ;

int Number;

while (1)

{

printf ( "\n1. Factorial" ) ;

printf ( "\n2. Odd/Even" ) ;

printf ( "\n3. Exit" ) ;\

printf("\n Enter Number");

scanf("%d",&Number);

printf ( "\nYour choice? " ) ;

scanf ( "%d", &choice ) ;

switch ( choice )

{

case 1 :

/* logic for factorial of a Number */

break ;

case 2 :

/* logic for odd/even */

break ;

case 3 :

exit() ;

}

}

}

/* A menu driven program */

void main

{

int choice ;

int Number;

while (1)

{

printf ( "\n1. Factorial" ) ;

printf ( "\n2. Odd/Even" ) ;

printf ( "\n3. Exit" ) ;\

printf("\n Enter Number");

scanf("%d",&Number);

printf ( "\nYour choice? " ) ;

scanf ( "%d", &choice ) ;

switch ( choice )

{

case 1 :

/* logic for factorial of a Number */

break ;

case 2 :

/* logic for odd/even */

break ;

case 3 :

exit() ;

}

}

}

Expert's answer

#include <stdio.h>

#include <stdbool.h>

int factorial(long long int n) {

if ( n < 0 ) {

return -1;

}

if ( n < 2 ) {

return 1;

} else {

return n * factorial(n-1);

}

}

bool even(int n) {

if ( n % 2 == 0 ) {

return true;

}

return false;

}

int main() {

int choice ;

int Number;

while (1) {

printf ( "\n1. Factorial" ) ;

printf ( "\n2. Odd/Even" ) ;

printf ( "\n3. Exit" ) ;\

printf("\nEnter Number\n");

scanf("%d",&Number);

printf ( "\nYour choice?\n" ) ;

scanf ( "%d", &choice ) ;

switch ( choice ) {

case 1 :

printf(" Factorial of the number %d is %d", Number, factorial(Number));

break ;

case 2 :

if ( even(Number) == true ) {

printf("The given number is even\n");

} else {

printf("The number is odd\n");

}

break ;

case 3 :

return 0 ;

default:

printf("Please do the choice correctly\n");

break;

}

}

return 0;

}

#include <stdbool.h>

int factorial(long long int n) {

if ( n < 0 ) {

return -1;

}

if ( n < 2 ) {

return 1;

} else {

return n * factorial(n-1);

}

}

bool even(int n) {

if ( n % 2 == 0 ) {

return true;

}

return false;

}

int main() {

int choice ;

int Number;

while (1) {

printf ( "\n1. Factorial" ) ;

printf ( "\n2. Odd/Even" ) ;

printf ( "\n3. Exit" ) ;\

printf("\nEnter Number\n");

scanf("%d",&Number);

printf ( "\nYour choice?\n" ) ;

scanf ( "%d", &choice ) ;

switch ( choice ) {

case 1 :

printf(" Factorial of the number %d is %d", Number, factorial(Number));

break ;

case 2 :

if ( even(Number) == true ) {

printf("The given number is even\n");

} else {

printf("The number is odd\n");

}

break ;

case 3 :

return 0 ;

default:

printf("Please do the choice correctly\n");

break;

}

}

return 0;

}

## Comments

## Leave a comment