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();
}
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