Home  >  Article  >  Backend Development  >  C++ program to read a person's height and print out whether the person is tall, short or average height

C++ program to read a person's height and print out whether the person is tall, short or average height

WBOY
WBOYforward
2023-09-05 10:17:06997browse

C++ program to read a persons height and print out whether the person is tall, short or average height

A person's height determines whether he/she is tall, dwarf, or average height. Height ranges vary in different parts of the world. We are considering Indian standards. In this article, we'll show you how to write a simple program in C to determine whether a person is tall, short, or of average height.

Let's first define the height ranges and corresponding classifications, then we can use them in our algorithms and implementations.

Height (cm) type
150 – 170 average
170 – 195 high
Less than 150 dwarf
Anything else Height Abnormality

Now let us look at its algorithm and implementation.

algorithm

  • Read height h.
  • If h is between 150 and 170, then.
    • This person is of medium height.
  • Otherwise when h is between 170 and 195, then.
    • This man is very tall.
  • Otherwise when h is lower than 150, then.
    • This man is a dwarf.
  • For some other cases,
    • This person has abnormal height
    • End if.

Example

#include <iostream>
using namespace std;

void solve( int h ) {
   if (h >= 150 && h <= 170 ) {
      cout << "The person is of average height" << endl;
   }
   else if (h >= 170 && h <= 195 ) {
      cout << "The person is tall" << endl;
   }
   else if (h < 150 ) {
      cout << "The person is dwarf" << endl;
   }
   else {
      cout << "The person has abnormal height" << endl;
   }
}

int main()
{
   cout << "Height of person A: 172" << endl;
   solve( 172 );
   cout << "Height of person B: 130" << endl;
   solve( 130 );
   cout << "Height of person C: 198" << endl;
   solve( 198 );
   cout << "Height of person D: 160" << endl;
   solve( 160 );
}

Output

Height of person A: 172
The person is tall
Height of person B: 130
The person is dwarf
Height of person C: 198
The person has abnormal height
Height of person D: 160
The person is of average height

in conclusion

Using height for classification is a simple problem, we just use decision making under certain conditions. In our implementation, four categories are shown, namely tall, short, average and abnormal height. Height ranges are also defined in the table above. With a simple conditional check if-else solution, the program can classify people based on a given height value.

The above is the detailed content of C++ program to read a person's height and print out whether the person is tall, short or average height. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete