CJ_Slip13B

Slip No. 13

B) Write a java program that asks the user name, and then greets the user by name. Before outputting the user's name, convert it to upper case letters. For example, if the user's name is Raj, then the program should respond "Hello, RAJ, nice to meet you!".

1. Open Notepad.

2. Type the following code:

    import java.util.Scanner;

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

    // Ask for user's name
    System.out.print("Enter your name: ");
    String name = scanner.nextLine();

    // Convert name to uppercase
    String upperName = name.toUpperCase();

    // Greet the user
    System.out.println("Hello, " + upperName + ", nice to meet you!");

    scanner.close();
    }
3. Save the file with the name GreetUser.java. Make sure to select "All Files" in the "Save as type" dropdown and add the .java extension manually.

4. Open the Command Prompt.

5. Compile the Java program by typing:
javac GreetUser.java

6. Run the compiled Java program by typing:
java GreetUser

}

No comments:

Post a Comment