0% found this document useful (0 votes)
13 views9 pages

Menu driven in c

Uploaded by

deepakptna4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views9 pages

Menu driven in c

Uploaded by

deepakptna4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

C PROGRAM

1 .// Menu driven program in C

#include <stdio.h>

// Driver code
int main() {
// Choice variable
int choice = -1;

// Menu display
printf("MENU:\n1. Coffee\n2. Tea\n3. Exit");

// Infinite Loop for choice input


while(1){
printf("\nEnter your choice:");
C PROGRAM
scanf("%d", &choice);

// If-else ladder
if(choice == 1){
printf("> Enjoy your Coffee!\n");
}
else if (choice == 2){
printf("> Enjoy your Tea!\n");
}
else if (choice == 3){
printf("BYE!!!\n");
// Termination of the Loop using break statement
break;
}
else{
printf("> Invalid Input\n");
}
}
return 0;
}

OUTPUT:

MENU:
1. Coffee
2. Tea
3. Exit
Enter your choice:1
> Enjoy your Coffee!

Enter your choice:2


> Enjoy your Tea!

Enter your choice:4


> Invalid Input

Enter your choice:3


BYE!!!

2. // Menu Driven Program for a calculator in C

#include <stdio.h>
C PROGRAM

// Driver code
int main() {
// Choice variable
int choice = -1;

// Variables to store the numbers


int a, b;

// Menu display
printf("MENU:\n1. Addition\n2. Subtraction\n3. Multiplication\n4.
Division\n5. Exit");

// Infinite Loop for choice input


while(1){
printf("\nEnter the operation you wish to perform:");
scanf("%d", &choice);

// If-else ladder
if(choice == 1){
// Number Input
printf("Enter First number :");
scanf("%d", &a);
printf("Enter Second number:");
scanf("%d", &b);
printf("Result: %d + %d = %d\n", a, b, (a+b));
}
else if (choice == 2){
// Number Input
printf("Enter First number :");
scanf("%d", &a);
printf("Enter Second number:");
scanf("%d", &b);
printf("Result: %d - %d = %d\n", a, b, (a-b));
}
else if (choice == 3){
// Number Input
printf("Enter First number :");
scanf("%d", &a);
printf("Enter Second number:");
scanf("%d", &b);
printf("Result: %d * %d = %d\n", a, b, (a*b));
}
else if (choice == 4){
C PROGRAM
// Number Input
printf("Enter First number :");
scanf("%d", &a);
printf("Enter Second number:");
scanf("%d", &b);
printf("Result: %d / %d = %d\n", a, b, (a/b));
}
else if (choice == 5){
printf("BYE!!!\n");
// Termination of the Loop using break statement
break;
}
else{
printf("> Invalid Input\n");
}
}
return 0;
}

OUTPUT:

MENU:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter the operation you wish to perform:1
Enter First number :5
Enter Second number:10
Result: 5 + 10 = 15

Enter the operation you wish to perform:2


Enter First number :10
Enter Second number:6
Result: 10 - 6 = 4

Enter the operation you wish to perform:3


Enter First number :4
Enter Second number:4
Result: 4 * 4 = 16
C PROGRAM

Enter the operation you wish to perform:4


Enter First number :10
Enter Second number:2
Result: 10 / 2 = 5

Enter the operation you wish to perform:6


> Invalid Input

Enter the operation you wish to perform:5


BYE!!!

3. // Menu Driven Program using switch case in C

#include <stdio.h>

// Driver code
int main() {
// Switch variable
int choice = -1;

// Menu display
printf("MENU:\n1. Coffee\n2. Tea\n3. Exit");

// Flag variable for termination of loop


int flag = 1;

// Infinite Loop for choice input


while(flag){
printf("\nEnter your choice:");
scanf("%d", &choice);

// Switch statements
switch(choice){
case 1:
printf("> Enjoy your Coffee!\n");
break;
case 2:
C PROGRAM
printf("> Enjoy your Tea!\n");
break;
case 3:
printf("BYE!!!\n");
flag = 0; // To terminate the loop
break;
default:
printf("> Invalid Input\n");
break;
}
}
return 0;
}

OUTPUT:

MENU:
1. Coffee
2. Tea
3. Exit
Enter your choice:1
> Enjoy your Coffee!

Enter your choice:2


> Enjoy your Tea!

Enter your choice:4


> Invalid Input

Enter your choice:3


BYE!!!
Tttttyy

4. // Menu Driven Program using Functions in C

#include <stdio.h>

// Declaring the functions


C PROGRAM
void odd_even();
void prime();

// Function to check whether a number is odd or even.


void odd_even(){
// Numeric Input
int num;
printf("\nEnter a number:");
scanf("%d", &num);

// Odd-Even checker
if(num % 2 == 0){
printf("Number %d is EVEN\n", num);
}
else{
printf("Number %d is ODD\n", num);
}
}

// Function to check whether a number is prime or not.


void prime(){
// Numeric Input
int num;
printf("\nEnter a number:");
scanf("%d", &num);

// Flag variable for prime numbers


int flag = 1;

// Prime checker
for(int i=2; i*i <= num; i++){
if(num % i == 0){
flag = 0;
break;
}
}

// Output
if(flag){
printf("Number %d is PRIME\n", num);
C PROGRAM
}
else{
printf("Number %d is NOT PRIME\n", num);
}
}

// Driver code
int main() {
// Choice variable
int choice = -1;

// Menu display
printf("MENU:\n1. Check_Odd/Even\n2. Check_Prime\n3. Exit");

// Flag variable for termination of loop


int flag = 1;

// Infinite Loop for choice input


while(flag){
printf("\nEnter your choice:");
scanf("%d", &choice);

// Switch statements
switch(choice){
case 1:
odd_even();
break;
case 2:
prime();
break;
case 3:
printf("BYE!!!\n");
flag = 0; // To terminate the loop
break;
default:
printf("> Invalid Input\n");
break;
}
}
return 0;
C PROGRAM
}

OUTPUT:

MENU:
1. Check_Odd/Even
2. Check_Prime
3. Exit
Enter your choice:1
Enter a number:20
Number 20 is EVEN

Enter your choice:2


Enter a number:5
Number 5 is PRIME

Enter your choice:3


BYE!!!

You might also like