Write a program in java to store 5 words in a write a program
Answers
Answer:
Explanation:
import java.util.*;
class Prahul
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i;
String words[] = new String[5];
System.out.println("Enter 5 words");
for(i=0 ; i<5; i++)
{ words[i]=sc.next();
}
for(i=0 ; i<5; i++)
{ System.out.print(words[i] + "\b"); //displays 5 words given by user
}
}
}
Program:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words;
words = new String[5];
for(int i=0;i<=4;i++){
if(i<4){
System.out.println("Enter a Word");
}
else{
System.out.println("Enter last Word");
}
words[i] = sc.nextLine();
}
System.out.println(Arrays.toString(words));
}
}