easy
1 views

Character – Male or Female

Determine if a given character represents male ('m') or female ('f') and print the corresponding gender.

Understand the Problem

Problem Statement

Write a program that accepts a single character as input and determines the gender it represents.

  • If the character is 'm', print 'male'
  • If the character is 'f', print 'female'

The program should handle both lowercase characters as specified in the problem statement.

Constraints

  • Input will be a single character (either 'm' or 'f')
  • Input will be provided via standard input (stdin)
  • Output must be exactly 'male' or 'female' (lowercase)
  • Program must terminate after processing one character

Examples

Example 1
Input
m
Output
male
Explanation

When the input character is 'm', the program correctly identifies it as representing male and prints 'male'.

Example 2
Input
f
Output
female
Explanation

When the input character is 'f', the program correctly identifies it as representing female and prints 'female'.

Solution

#include <stdio.h>

int main() {
    char ch;
    scanf("%c", &ch);
    
    if (ch == 'm') {
        printf("male");
    } else {
        printf("female");
    }
    
    return 0;
}
Time:O(1) - Constant time operation regardless of input
Space:O(1) - Uses only one character variable for storage
Approach:

The C solution:

  1. Declares a character variable ch to store input
  2. Uses scanf("%c", &ch) to read a single character from stdin
  3. Uses an if-else statement to check if ch == 'm'
  4. If true, prints 'male' with printf
  5. Otherwise, prints 'female'
  6. Returns 0 to indicate successful execution

Visual Explanation

Loading diagram...