wap a program to create menu by switch if we choose 1. factorial 2. odd even 3. prime or not prime 4. exit.

 




#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 main()
{
   int fact=1,i,a,no,choice,p=0;
   
   while (1)
   {
       printf("\n1 factorial");
       printf("\n2 odd even");
       printf("\n3 prime number or not");
       printf("\n4 exit");


       printf("\n select a opt ");
       scanf(" %d",&choice);
    switch(choice)
    {
        case 1 :
                 printf("enter any no.");
                 scanf(" %d",&no);
                 for(i=1;i<=no;i++)
                 {
                    fact*=i;
                 }
                 printf("your factorial is %d",fact);
                 break;

        case 2 :
                 printf("enter a no.");
                 scanf("%d",&no);
                 if(no%2==0)
                 printf("it is even no.");
                 else
                 printf("no. is odd");
                 break;

        case 3 :
                 printf("select a no.");
                 scanf("%d",&no);
                 for (a=1;a>=no;a++)
                 {
                    if (no%a==0)
                     {
                         p++;
                     }
                 }
                   if(p==2)
                    {
                       printf("%d is not a prime number",no);
                    }
                   else
                   {
                      printf("%d is a prime number",no); 
                   }
                   break;

        case 4 : exit (0) ;
        default : printf("u choose wrong");
     }
   }


}


output :

1 factorial 2 odd even 3 prime number or not 4 exit select a opt 1 enter any no.7 your factorial is 5040
1 factorial 2 odd even 3 prime number or not 4 exit select a opt 2 enter a no.23 no. is odd
1 factorial 2 odd even 3 prime number or not 4 exit select a opt3 select a no.23 23 is a prime number
1 factorial 2 odd even 3 prime number or not 4 exit select a opt 5 u choose wrong
1 factorial 2 odd even 3 prime number or not 4 exit select a opt 4

Comments

Popular Post

Define a class to represent a Bank Account. Include the following members: Data Members: i. Name of the depositor ii. Account number iii. Type of account iv. Balance amount in the account Member Functions: 1. To Input initial values 2. To deposit an amount 3. To withdraw an amount after checking the balance 4. To display name and balance Also write constructor for this class that takes four arguments. It should also handle type of account as savings by default

Write the definition for a class called complex that has floating point data members for storing real and imaginary parts. The class has the following member functions: void set(float, float) to set the specified value in object void disp() to display complex number object complex sum(complex) to sum two complex numbers & return complex number 1. Write the definitions for each of the above member functions. 2. Write main function to create three complex number objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all complex numbers.

Create a base class called shape .Use this class to store two double type values that could be used to compute the area of figures, Derive two specific classes called triangle and rectangle from the base shape .Add to the base class, a member function get_data() to initialise base class data members and another member function display_area() to compute and display the area of figures. Make display_area () as a virtual function and redefine this function in the derived class to suit their requirements. Using these three classes, design a program that will accept dimensions of a triangle or a rectangle interactively and display the area. Area of rectangle = x*y Area of triangle = ½*x*y