Computer Science, asked by RudyDcosta, 1 year ago

wap a program in java

Attachments:

Answers

Answered by QGP
22

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;

               

           }

       }

       

   }

}

Attachments:
Answered by Oreki
1

\textbf{\large Algorithm}

   \textsf{\textemdash \: Accepting a number.}\\\textsf{\textemdash \: Mapping digits to their n\symbol{97}me.}\\\textsf{\textemdash \: Obtaining each digit of the accepted number.}\\\textsf{\textemdash \: And adding the corresponding n\symbol{97}me to the accumulator.}\\\textsf{\textemdash \: Printing the obtained result.}

\textbf{\large Variable Description}

   \begin{tabular} {l|c|l}\textbf{N\symbol{97}me} &amp; \textbf{Data Type} &amp; \textbf{Usage}\\\textsf{number} &amp; \textsf{int} &amp; \textsf{To store a number.}\\\textsf{DIGIT\_N\symbol{65}ME} &amp; \textsf{String[ ]} &amp; \textsf{To store digit n\symbol{97}mes.}\\\textsf{result} &amp; \textsf{String} &amp; \textsf{To store the final \symbol{111}utput.}\\\end{tabular}    

Attachments:
Similar questions