C# Program to Accept the Height of a Person and Categorize as Taller, Dwarf & Average​

A C# height categorization program is useful for classifying user input into meaningful groups, making data analysis and decision-making more efficient. Categorizing data based on input values allows programs to provide better insights, automate processes, and enhance user interaction.

In this blog, we will create a C# height categorization program that accepts a person’s height and classifies it into predefined groups: Dwarf, Average, Taller, and Abnormal height. This exercise will help beginners understand how to work with conditional statements (if-else) and user input handling in C#.

Understanding Height Categories

To categorize a person’s height, we define the following classification in our C# height categorization program:

  • Dwarf: Less than 150 cm
  • Average: Between 150 cm and 165 cm
  • Taller: Between 165 cm and 195 cm
  • Abnormal height: Greater than 195 cm

Rationale Behind These Classifications

These height categories are based on general population height distribution and can vary based on region, genetics, and demographics. The Dwarf category represents individuals who are significantly shorter than average, while the Average category covers a common height range. The Taller category includes those who are above average, and anything beyond 195 cm is considered abnormal height due to its rarity in most populations.

This classification helps in various real-world applications, such as:

  • Medical analysis (growth monitoring).
  • Sports classification (eligibility criteria for different sports).
  • Fashion and apparel (size recommendations).

Now, let’s move forward and implement this logic in C#!

Writing the C# PrograC# height categorization program

Now that we have defined our height categories, let’s implement them in C#. Below is the complete program:

using System;

class Program
{
    public static void Main()
    {
        float height;
        Console.WriteLine("Enter the Height (in centimeters):");
        height = float.Parse(Console.ReadLine());

        if (height < 150.0)
            Console.WriteLine("Dwarf");
        else if (height <= 165.0)
            Console.WriteLine("Average Height");
        else if (height <= 195.0)
            Console.WriteLine("Taller");
        else
            Console.WriteLine("Abnormal height");
    }
}

Code Explanation

  1. Reading User Input
    • The program prompts the user to enter their height in centimeters using Console.WriteLine().
    • The input is received as a string from Console.ReadLine() and converted into a float using float.Parse().
  2. Using Conditional Statements for Classification
    • The if-else structure is used to compare the height value and categorize it accordingly:
      • If the height is less than 150 cm, it prints “Dwarf”.
      • If the height is between 150 cm and 165 cm, it prints “Average Height”.
      • If the height is between 165 cm and 195 cm, it prints “Taller”.
      • If the height is greater than 195 cm, it prints “Abnormal height”.
  3. Displaying the Result
    • After classification, the appropriate category is displayed using Console.WriteLine().

C# height classification Program Output

Here are some sample inputs and outputs of C# height categorization program:

Input (Height in cm)Output
145Dwarf
160Average Height
180Taller
200Abnormal height

Example Runs

Example 1

Input:

Enter the Height (in centimeters):  
140

Output:

Dwarf

Example 2

Input:

Enter the Height (in centimeters):  
170

Output:

Taller

Example 3

Input:

Enter the Height (in centimeters):  
200

Output:

Abnormal height

This program effectively categorizes a person’s height based on their input, providing a simple yet practical example of conditional statements in C#.

Enhancing the C# Height Categorization Program with User-Friendly Features

To make the program more interactive and robust, we can add the following improvements:

  1. Input Validation
    • Currently, if the user enters a non-numeric value, the program will crash. We can use float.TryParse() to validate the input and handle errors gracefully.
using System; class Program { public static void Main() { Console.WriteLine("Enter the Height (in centimeters):"); if (float.TryParse(Console.ReadLine(), out float height)) { if (height < 150.0) Console.WriteLine("Dwarf"); else if (height <= 165.0) Console.WriteLine("Average Height"); else if (height <= 195.0) Console.WriteLine("Taller"); else Console.WriteLine("Abnormal height"); } else { Console.WriteLine("Invalid input! Please enter a numeric value."); } } }

2. Loop for Multiple Entries

  • Instead of exiting after one input, we can allow the user to enter multiple heights until they choose to exit.

    using System; class Program { public static void Main() { while (true) { Console.WriteLine("Enter the Height (in centimeters) or type 'exit' to quit:"); string input = Console.ReadLine(); if (input.ToLower() == "exit") break; if (float.TryParse(input, out float height)) { if (height < 150.0) Console.WriteLine("Dwarf"); else if (height <= 165.0) Console.WriteLine("Average Height"); else if (height <= 195.0) Console.WriteLine("Taller"); else Console.WriteLine("Abnormal height"); } else { Console.WriteLine("Invalid input! Please enter a numeric value."); } } } }

    Benefits of These Enhancements

    Prevents crashes when users enter invalid input.
    Improves user experience by allowing multiple inputs in a single execution.
    Provides better feedback, helping users enter valid data.

    By implementing these features, the program becomes more user-friendly and practical. Let me know if you’d like more modifications!

    Conclusion

    In this blog, we explored a C# height categorization program, learning how to use conditional statements and user input. Try modifying height ranges or adding new categories to enhance the program. For more informative and latest programming resources, visit Newtum and keep improving your coding skills!

    About The Author

    Leave a Reply