Thursday, September 4, 2014

write a menu driven program to calculate factorial of a number, prime or not, and even or odd using functions


Problem

Write the program using functions. There are three functions fact (), prime() and even_odd() that contains the logic for calculating factorial of a number, determine that entered number is prime or not and determine that entered number is even or odd, respectively. Prototypes of the three functions are given below:
void fact(void);
void prime(void);
void even_odd(void);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void fact(void);
void prime(void);
void even_odd(void);
main()
{
 int c;

 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);

 switch(c)
 {
  case 1:
   fact();
   break;
  case 2:
   prime();
   break;
  case 3:
   even_odd();
   break;
  case 4:
   exit(0);
  default:
      printf("enter correct choise");
 }
 getch();
}

void fact (void)
{
 int n,b,i;
 printf("enter the first no.:");
 scanf("%d",&n);
 b=1;
 for(i=n;i>0;i--)
  b=b*i;
 printf("factorial is %d",b);
}
void prime(void)
{
 int j;
 printf("enter the first no.:");
 scanf("%d",&j);
 if(j%2==0)
 {
  printf("it is not a prime numbr.");  }
 else
  printf("it is a prime number.");
}
void even_odd(void)
{
 int g;
 printf("enter the first no.:");
 scanf("%d",&g);
 if(g%2==0)
  printf("it is an even number.");

 else
  printf("not an even number");
}

Sample Output


No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact