Saturday, March 12, 2016

Program to display MENU using do..while

This post is related to MENU driven C program using do..while loop.

#include<stdio.h>
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;
    case 2: printf("The subtraction = %d \n", n1 - n2);
            break;
    case 3: printf("Goodbye\n");
            break;
    default:printf("Wrong Choice. Enter again\n");
            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




No comments:

Post a Comment