Write a program in java to print a city's name.
Answers
CODE :-
import java.util.*;
class City_name
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the city name I will print it");
String s=sc.nextLine();
System.out.println("The city name is :"+s);
}
}
OUTPUT
Enter the city name I will print it
Kolkata
Kolkata
___________________________________________________________________
Following are the program that is given below
Explanation:
import java.util.*; // import package
public class Main // main class
{
public static void main(String[] args) // main method
{
Scanner sc1=new Scanner(System.in); // create the object of scanner class
System.out.println("Enter the city :");
String s1=sc1.nextLine(); // Read the city
System.out.println("city is :"+s1); // display city
}
}
Output:
Enter the city :
kolk
city is :kolk
Following are the description of program
- Create the instance of scanner class for input i.e "sc1".
- Read the city in the 's1" variable by using the scanner class object .
- Finally display the city by using system .println.
Learn More :
- brainly.in/question/10342288