Menu driven program to perform operations on an integer.

C File =>


/* Write a menu driven program to perform the following operations on an 
integer. Write separate functions. 
1. Check if is even or odd 
2. Check if it is prime 
3. Exit */


#include<stdio.h>
#include<stdlib.h>
int main() 
{
      int n,ch,i;
 
      void chk_even(int n)
     {
        if(n%2==0)
           printf("\n Number is EVEN..");
       else
          printf("\n Number is ODD...");
     }
 
     
     void chk_prime(int n)
     {
        int flag=1;
        for(i=2;i<n;i++)
       {
           if(n%i==0)
          {
              flag=0;
              break;
           }
        }
 
       if(flag==0)
             printf("\n Number is NOT prime..");
      else
            printf("\n Number is PRIME..");
      }
 
     printf("\n Enter an integer : ");
     scanf("%d",&n);
     
     printf("\n Menu driven operations:\n 1. Check if is even or odd\n
 2. Check if it is prime\n 3. Exit");
 
     printf("\n Enter your choice (1-3) : ");
     scanf("%d",&ch);
 
     switch(ch)
    {
       case 1: chk_even(n);
       break;
       case 2:chk_prime(n);
       break;
       case 3:exit(0);
       break;
       default : printf("\n Invalid choice..");
     }

     return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.