Home » Write a menu driven program to check a number is even or odd?

Write a menu driven program to check a number is even or odd?

Write a menu driven program to check

     1. a number is even or odd

       2. prime or not

         3. factorial of the number.

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

void main()

{

int a,d,t,choice,i,j;

long int fact=1;

clrscr();

printf(“\nEnter the number “);

scanf(“%d”,&a);

printf(“\n*********************”);

printf(“\n* 1. Even or Odd    *”);

printf(“\n* 2. Prime or not   *”);

printf(“\n* 3. Factorial      *”);

printf(“\n* 4. Exit           *”);

printf(“\n*********************”);

do

{

printf(“\n\nEnter choice =>”);

scanf(“%d”,&choice);

switch(choice)

{

case 1:if(a%2==0)

printf(“\n%d is EVEN “,a);

else

printf(“\n%d is ODD “,a);

break;

case 2:if(a==2)

printf(“\n%d is the first prime number”,a);

else

{

t=sqrt(a);

d=2;

while(d<=t)

{

if(a%d==0)

break;

d++;

}

if(d>t)

printf(“\n%d is a prime number”,a);

else

printf(“\n%d is not a prime number”,a);

}

break;

case 3:

for( j=1;j<=a;j++)

fact=fact*j;

printf(“\n%d Factorial is %ld”,a,fact);

break;

case 4: exit(0);

default: printf(“\nChoice is wrong!!!”);

}

}

while(choice<=4);

getch();

}

OUTPUT:

4


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s