Object

Write a program to input three numbers and print the greater one.

Algorithm

  1. Declare integer variables a, b and c.
  2. Read a, b and c.
  3. IF (a > b and a > c), then
    • Write “a is greater”.
    • ELSE IF (b > a and b > c), then
      • Write “b is greater”.
    • ELSE
      • Write “c is greater”.
    • [End of IF-ELSE structure.]
  4. Exit.

Flowchart

C++ Source Code

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

int main()
{
  int a, b, c;
  cout << "Enter the first number: ";
  cin >> a;

  cout << "Enter the second number: ";
  cin >> b;
  
  cout << "Enter the third number: ";
  cin >> c;

  if (a > b && a > c)
    cout << "The greatest number is " << a;
  else if (b > a && b > c)
    cout << "The greatest number is " << b;
  else
    cout << "The greatest number is " << c;

  return 0;
}

C Source Code

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

int main()
{
  int a, b, c;
  printf("Enter the first number: ");
  scanf("%d", &a);
  printf("Enter the second number: ");
  scanf("%d", &b);
  printf("Enter the third number: ");
  scanf("%d", &c);

  if (a>b && a>c)
     printf("The greatest number is %d", a);
  else if (b>a && b>c)
     printf("The greatest number is %d", b);
  else
     printf("The greatest number is %d", c);
  return 0;
}

Output

Enter the first number: 7
Enter the second number: 9
Enter the third number: 3
The greatest number is 9

Design a site like this with WordPress.com
Get started