wap a program in java
Answers
Java Programming
Programming in Java requires knowledge of the Java Language and Solving the problem statement requires some analysis on our part.
Here, we are tasked to write a program which will "Spell Out" each number. So, we will first take user input. In what form should we save it? Here, I saved it in String form, because it is really easy to simply run a loop through all the characters.
For each digit, we can use the Switch Case Java Construct. The same logic is applied here.
Here's a Java Program which accomplishes the task:
import java.util.Scanner; //Importing Scanner
public class Brainly
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //Creating Scanner Object
System.out.print("Enter the number: "); //Asking User for input
String num = sc.nextLine(); //Taking Input as String
for(int i=0;i<num.length();i++) //Setting a Loop to iterate through each character
{
char digit = num. charAt(i); //Storing each character in a char variable
switch(digit) //Switch Case
{
case '0': //Cases of each numbers and their words
System.out.print("ZERO ");
break;
case '1':
System.out.print("ONE ");
break;
case '2':
System.out.print("TWO ");
break;
case '3':
System.out.print("THREE ");
break;
case '4':
System.out.print("FOUR ");
break;
case '5':
System.out.print("FIVE ");
break;
case '6':
System.out.print("SIX ");
break;
case '7':
System.out.print("SEVEN ");
break;
case '8':
System.out.print("EIGHT ");
break;
case '9':
System.out.print("NINE ");
break;
}
}
}
}