Skip to content

← Back to Overview

User Input and Display

Concept Explanation

Programs often need to interact with users. Taking user input allows a program to receive data from the user, making it dynamic and responsive. Displaying output is equally important for providing feedback, results, or prompts to the user. This concept is fundamental to almost all interactive applications.

Algorithm

  1. Start.
  2. Prompt the user to enter a value (e.g., name, number).
  3. Read the input provided by the user.
  4. Display the entered value back to the user.
  5. End.

Implementations

import java.util.Scanner; // Import the Scanner class

public class UserInputDisplay {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // --- Taking String Input ---
        System.out.print("Enter your name: ");
        String name = scanner.nextLine(); // Reads the entire line of input
        System.out.println("Hello, " + name + "!");

        // --- Taking Integer Input ---
        System.out.print("Enter your favorite number: ");
        int number = scanner.nextInt(); // Reads the next integer input
        System.out.println("Your favorite number is: " + number);

        scanner.close(); // It's good practice to close the scanner
    }
}
# --- Taking String Input ---
name = input("Enter your name: ")  # Prompts user and reads input as a string
print("Hello,", name + "!")

# --- Taking Integer Input ---
# input() always returns a string, so we need to convert it to an integer
number_str = input("Enter your favorite number: ")
number = int(number_str) # Convert the string to an integer
print("Your favorite number is:", number)
#include <stdio.h> // Required for printf and scanf

int main() {
    char name[50]; // Declare a character array (string) to store the name
    int number;    // Declare an integer variable to store the number

    // --- Taking String Input ---
    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin); // Reads entire line including spaces, safer
    printf("Hello, %s", name); // name already includes a newline from fgets

    // --- Taking Integer Input ---
    printf("Enter your favorite number: ");
    scanf("%d", &number); // Reads an integer
    printf("Your favorite number is: %d\n", number); // Added \n for consistent output

    return 0; // Indicate successful execution
}
-- SQL*Plus/SQL Developer (Substitution Variable for name)
ACCEPT p_name CHAR PROMPT 'Enter your name: '
SELECT 'Hello, ' || '&p_name' || '!' AS Greeting FROM DUAL;

-- PL/SQL (Anonymous Block with User Input for a value)
SET SERVEROUTPUT ON;
DECLARE
  p_input VARCHAR2(100);
BEGIN
  p_input := '&Enter_a_value';
  DBMS_OUTPUT.PUT_LINE('You entered: ' || p_input);
END;
/

Explanation

  • Java: Uses the Scanner class (java.util.Scanner) to read various types of input. nextLine() for strings and nextInt() for integers. scanner.close() is important for resource management.
  • Python: Uses the built-in input() function, which always reads input as a string. Explicit conversion functions like int() or float() are used to convert string input to numerical types when needed.
  • C: Employs fgets() for safe string input (reading an entire line) and scanf() for formatted input like integers. Proper format specifiers (%s, %d) and the address-of operator (&) are crucial with scanf(). \n added to printf for consistent output.
  • Oracle:
    • SQL: ACCEPT command (specific to SQL*Plus/SQL Developer) can prompt for user input into a substitution variable (&p_name), which can then be used in SQL queries.
    • PL/SQL: An anonymous PL/SQL block can also use substitution variables (&Enter_a_value). DBMS_OUTPUT.PUT_LINE is used to display output, which requires SET SERVEROUTPUT ON; to be enabled in the client environment.

Flowchart

graph TD
    A[Start] --> B[Prompt User for Input]
    B --> C[Read Input]
    C --> D[Display Input]
    D --> E[End]

Practice Problems

  • Modify the program to take two numbers as input and display their sum.
  • Take a single word as input and display it in uppercase.
  • Ask the user for their age and print a message like "You are [age] years old."

"The capacity to learn is a gift; the ability to learn is a skill; the willingness to learn is a choice." - Brian Herbert