Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-05 10:17:06
forward
1002 people have browsed it

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 );
}
Copy after login

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
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template