0% found this document useful (0 votes)
255 views3 pages

Project Codes

This Java program contains code to convert numeric numbers to their word equivalents. It includes arrays to store the values for single digits, teens, and tens. The main method gets user input for a number, then uses if/else statements and modulo/division operations to separate the input into hundreds, tens, and ones digits before printing out the corresponding word values based on the arrays. The program handles numbers from 1 to 999.

Uploaded by

Jay Telan II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views3 pages

Project Codes

This Java program contains code to convert numeric numbers to their word equivalents. It includes arrays to store the values for single digits, teens, and tens. The main method gets user input for a number, then uses if/else statements and modulo/division operations to separate the input into hundreds, tens, and ones digits before printing out the corresponding word values based on the arrays. The program handles numbers from 1 to 999.

Uploaded by

Jay Telan II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROJECT CODES:

package numberToWord;
import java.util.Scanner;

public class NumberToWord {


//array containing single digits
public static final String[] digits =
{
"",
" One",
" Two",
" Three",
" Four",
" Five",
" Six",
" Seven",
" Eight",
" Nine",

};

//array containing teens


public static final String[] teens =
{
" Eleven",
" Twelve",
" Thirteen",
" Fourteen",
" Fifteen",
" Sixteen",
" Seventeen",
" Eighteen",
" Nineteen"
};

//array containing tens numbers


public static final String[] tens =
{
"Ten",
"Twenty",
"Therty",
"Fourty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety",
};

//string for hundreds numbers


public static final String hundred = "hundred";

public static int tmpDigit, //temp number to store digits


tmpTeen, //temp to store teens numbers
tmpTen, //temp number to store tens
number, //number to be converted to words
result; //result number
//deciding whether a num is teen or not
public static boolean teen = false;

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//getting user input


System.out.print("Please enter a number: ");
number = in.nextInt(); //number to be converted
//input validation
while(number <= 0)
{
System.out.println("Number should be bigger than cero!");
System.out.print("Please enter a number:");
number = in.nextInt();
}
//converting single digits number
if(number > 0 && number < 10)
{
result = number % 10;
System.out.println(digits[result]);
}
//converting teen numbers
if(number > 10 && number < 20)
{
result = number % 10;
System.out.println(teens[result-1]);
}
//converting from 20 to 100 numbers
if(number > 19 && number < 100)
{
result = number / 10; //finding tens
System.out.print(tens[result-1]);
number %= 10; //finding single digits
System.out.println(digits[number]);
}
//converting from 101 to 999
if(number > 100 && number < 1000)
{
result = number / 100; //getting hundreds
//getting tens, and finding tens, and teens numbers
tmpTen = number % 100;
if(tmpTen > 1 && tmpTen < 20)//if teen go get it
{
tmpTeen = tmpTen % 10;
teen = true;
}
else //if not a teen split number
tmpTen /= 10;
//finding digits
tmpDigit = number % 10;

if(!teen)//if final result doesn't include teen numbers print the following
System.out.println(digits[result]+" "+hundred+" "+tens[tmpTen-1]+" "+digits[tmpDigit]);
else
System.out.println(digits[result]+" "+hundred+" "+teens[tmpTeen-1]);
}

}
}

You might also like