The intent of this page is to collection the best program and help programmers to find good blog posts to read. Some of these blogs may not be written by Java developers, but Java developers should find it useful or interesting. Reading those blogs should be fun and often bring some fresh ideas

Saturday, March 7, 2020

PROGRAM CONVERT A NUMBER TO A STRING

import java.util.*;
class Numtostring{
private static final String[] centuarys={"","Thousent ","Million ","Billion ","Trillion "};
private static final String[] tenth={"","Ten ","Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
private static final String[] numbers={"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ",
"Eleven ","Twelve ","Thirteen ","Fourty ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
private String converting(int number){
String item;
if(number%100<20){
item=numbers[number%100];
number/=100;
}
else{
item=numbers[number%10];
number/=10;
item=tenth[number%10]+item;
number/=10;
}
if(number==0)
return item;
return numbers[number]+"Hundred "+item;
}
public String convert(int number){
if(number==0){
return "zero";
}
String negative="";
if(number<0){
number=-number;
negative="Negativenumber";
}
String item="";
int value=0;
do{
int n=number%1000;
if(n!=0){
String s=converting(n);
    item=s+centuarys[value]+item;
}
value++;
number/=1000;
}while(number>0);
return(negative+item).trim();
}
public static void main(String[] args){
Numtostring obj = new Numtostring();
Scanner sc = new Scanner(System.in);
int no=sc.nextInt();
System.out.println(obj.convert(no)+" ");
}
}
INPUT:12345

OUTPUT:Twelve Thousent Three Hundred Fourty Five

No comments:

Post a Comment

If you have any doubt . Please let me know

String Operation

String Operation Using collection Framework Sorting Order  public static void sortAccending(String s) { char[] arr = s.toCharArray(); ...

Adbox