This post is related to MENU driven C program using do..while loop.
#include<stdio.h>
int main()
{
int main()
{
int choice, n1=5, n2=10;
printf("n1=5, n2=10 \n");
do
{
printf("=== Menu ===\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("The addition = %d \n", n1 + n2);
break;
printf("n1=5, n2=10 \n");
do
{
printf("=== Menu ===\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("The addition = %d \n", n1 + n2);
break;
case 2: printf("The subtraction = %d \n", n1 - n2);
break;
break;
case 3: printf("Goodbye\n");
break;
break;
default:printf("Wrong Choice. Enter again\n");
break;
break;
}
} while (choice != 3);
return 0;
}
Output of program
n1=5, n2=10
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:1
The addition = 15
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:2
The subtraction = -5
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:3
Goodbye
} while (choice != 3);
return 0;
}
Output of program
n1=5, n2=10
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:1
The addition = 15
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:2
The subtraction = -5
=== Menu ===
1. Addition
2. Subtraction
3. Exit
Enter your choice:3
Goodbye
No comments:
Post a Comment