TechTorch

Location:HOME > Technology > content

Technology

How to Write a Program to Print ‘Hello’ When You Enter ‘Hi’

February 28, 2025Technology1538
How to Write a Program to Print ‘Hello’ When You Enter ‘Hi’ This tutor

How to Write a Program to Print ‘Hello’ When You Enter ‘Hi’

This tutorial provides examples in several popular programming languages, demonstrating how to create a simple program that prints 'Hello' when the user inputs 'Hi'. You can use this guide to learn and implement in various languages like Python, JavaScript, Java, and C. Let's dive into the details and see how it works.

Python Example

Python is a widely used language for its simplicity and readability. Here's a simple program that checks if the user input is 'Hi' and prints 'Hello' accordingly.

user_input  input()
if user_input.lower()  'hi':
    print('Hello')

JavaScript Example

JavaScript is commonly used in web development. Below is an example of how to implement the same functionality using a simple prompt and console.log.

let userInput  prompt('Enter something:');
if (()  'hi') {
    console.log('Hello');
}

Java Example

In Java, you can use a Scanner to get user input and then check for the condition. Here's the implementation:

import ;
public class HelloPrinter {
    public static void main(String[] args) {
        Scanner scanner  new Scanner();
        String userInput  ();
        if (userInput.equalsIgnoreCase("hi")) {
            ("Hello");
        }
    }
}

C Example

If you prefer a lower-level language, C provides a powerful way to implement this. You can use the strcmp function from the standard string library. Below is a simple C program to illustrate this:

#include 
#include 
#include 
int main() {
    char str[100];
    printf("Enter something: ");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "
")]  0; // Remove newline character
    if (strcasecmp(str, "hi")  0) {
        printf("Hello
");
    } else {
        printf("Not a match
");
    }
    return 0;
}

How it Works

Each program follows a similar logic:

The program prompts the user to enter input. The input is then processed and compared with the target word ('Hi'). If the input matches the target word, the program prints 'Hello'. Otherwise, it may print a message indicating that the input did not match.

You can run these snippets in their respective environments (Python, JavaScript console, Java compiler, or C compiler) to see the output.

Conclusion

These examples show how to handle user input and process it using conditional statements. By understanding these basic concepts, you can create more complex programs that interact with the user in meaningful ways.