Object

Write a program that inputs the grade of a student and prints a message accordingly.

Algorithm

  1. Declare character variable grade.
  2. Read grade (and consider it as switch variable).
  3. IF switch variable = ‘A’
    • Write “An Excellent Achievement!”
    • IF switch variable = ‘B’
      • Write “Good! Keep it up.”
    • IF switch variable = ‘C’
      • Write “Fair! You still need to concentrate.”
    • IF switch variable = ‘D’
      • Write “Poor Grade! Better work hard.”
    • ELSE
      • Write “Sorry, you have failed.”
  4. Exit.

Flowchart

C++ Source Code

// program 28
#include<iostream>
using namespace std;

int main()
{
  char grade;
  cout << "Enter your grade: ";
  cin >> grade;

  switch (grade)
  {
  case 'A':
    cout << "An Excellent Achievement!";
    break;
  case 'B':
    cout << "Good! Keep it up.";
    break;
  case 'C':
    cout << "Fair! You still need to concentrate.";
    break;
  case 'D':
    cout << "Poor Grade! Better work hard.";
    break;
  default:
    cout << "Sorry, you have failed.";
  }
  return 0;
}

C Source Code

/*program 28*/
#include<stdio.h>

int main()
{
  char grade;
  printf("Enter your grade: ");
  scanf("%c", &grade);
  switch(grade)
    {
    case 'A':
      printf("An Excellent Achievement!");
      break;
    case 'B':
      printf("Good! Keep it up.");
      break;
    case 'C':
      printf("Fair! You still need to concentrate.");
      break;
    case 'D':
      printf("Poor Grade! Better work hard.");
      break;
    default:
      printf("Sorry, you have failed.");
    }
  return 0;
}

Output

Enter your grade: C
Fair! You still need to concentrate.

Design a site like this with WordPress.com
Get started