Write program in java Ruya’s teacher gave her an assignment to practice the spelling of "RAINBOW". Ruya’s mother helped her in
doing this by spelling each letter as Ruya wrote it in her notebook. If Ruya writes wrong spelling her mother should say "The spelling is wrong"
Sample Input 1:
Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W
Sample Output 1:
RAINBOW
Sample Input 2:
Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W
Sample Output 2:
The spelling is wrong
Answers
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner Sc = new Scanner(System.in);
String S = "RAINBOW";
char ch[] = new char[7];
System.out.print("Enter the first letter: ");
ch[0] = Sc.next().charAt(0);
System.out.print("Enter the second letter: ");
ch[1] = Sc.next().charAt(0);
System.out.print("Enter the third letter: ");
ch[2] = Sc.next().charAt(0);
System.out.print("Enter the fourth letter: ");
ch[3] = Sc.next().charAt(0);
System.out.print("Enter the fifth letter: ");
ch[4] = Sc.next().charAt(0);
System.out.print("Enter the sixth letter: ");
ch[5] = Sc.next().charAt(0);
System.out.print("Enter the seventh letter: ");
ch[6] = Sc.next().charAt(0);
int flag = 0;
for(int i = 0; i < 7; i++)
{
if(S.charAt(i) != ch[i])
{
flag = 1;
break;
}
}
if(flag == 0)
{
System.out.print("RAINBOW");
}
else
{
System.out.print("The spelling is wrong");
}
}
}
Output 1:
Enter the first letter: R
Enter the second letter: A
Enter the third letter: I
Enter the fourth letter: N
Enter the fifth letter: B
Enter the sixth letter: O
Enter the seventh letter: W
RAINBOW
Output 2:
Enter the first letter: R
Enter the second letter: E
Enter the third letter: I
Enter the fourth letter: N
Enter the fifth letter: B
Enter the sixth letter: O
Enter the seventh letter: W
The spelling is wrong