Skip to content

Instantly share code, notes, and snippets.

@madan712
Last active September 18, 2023 04:02
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save madan712/be79a395424bf773be87 to your computer and use it in GitHub Desktop.
Save madan712/be79a395424bf773be87 to your computer and use it in GitHub Desktop.
Java program to convert numbers to words
import java.text.NumberFormat;
public class NumberToWordsConverter {
public static final String[] units = { "", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen" };
public static final String[] tens = {
"", // 0
"", // 1
"Twenty", // 2
"Thirty", // 3
"Forty", // 4
"Fifty", // 5
"Sixty", // 6
"Seventy", // 7
"Eighty", // 8
"Ninety" // 9
};
public static String convert(final int n) {
if (n < 0) {
return "Minus " + convert(-n);
}
if (n < 20) {
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
}
if (n < 100000) {
return convert(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + convert(n % 1000);
}
if (n < 10000000) {
return convert(n / 100000) + " Lakh" + ((n % 100000 != 0) ? " " : "") + convert(n % 100000);
}
return convert(n / 10000000) + " Crore" + ((n % 10000000 != 0) ? " " : "") + convert(n % 10000000);
}
public static void main(final String[] args) {
int n;
n = 5;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 16;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 50;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 78;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 456;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 1000;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 99999;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 199099;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
n = 10005000;
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");
}
}
Copy link

ghost commented Sep 2, 2017

import java.text.NumberFormat;
import java.util.Scanner;

public class NumberToWordsConverter {

public static final String[] units = { "", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
"Eighteen", "Nineteen" };

public static final String[] tens = { 
		"", 		// 0
		"",		// 1
		"Twenty", 	// 2
		"Thirty", 	// 3
		"Forty", 	// 4
		"Fifty", 	// 5
		"Sixty", 	// 6
		"Seventy",	// 7
		"Eighty", 	// 8
		"Ninety" 	// 9
};

public static String convert(final int n) {
	if (n < 0) {
		return "Minus " + convert(-n);
	}

	if (n < 20) {
		return units[n];
	}

	if (n < 100) {
		return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
	}

	if (n < 1000) {
		return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
	}

	if (n < 100000) {
		return convert(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + convert(n % 1000);
	}

	if (n < 10000000) {
		return convert(n / 100000) + " Lakh" + ((n % 100000 != 0) ? " " : "") + convert(n % 100000);
	}

	return convert(n / 10000000) + " Crore" + ((n % 10000000 != 0) ? " " : "") + convert(n % 10000000);
}

public static void main(final String[] args) {

	int n;
	Scanner s=new Scanner(System.in);
    System.out.println("Enter a number to convert into word format");
	n =s.nextInt();
	System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'");

}

}

Note: This code is valid for dynamic conversion of numbers.

@TechWithHazem
Copy link

Thank you sir for your work.

@bhavikgevariya
Copy link

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int m(long long int a,int b){
a=a/(pow(10,b-1));
return a%10;
}
void no1(int a){
switch(a){
case 1:printf("One ");break;
case 2:printf("Two ");break;
case 3:printf("Three ");break;
case 4:printf("Four ");break;
case 5:printf("Five ");break;
case 6:printf("Six ");break;
case 7:printf("Seven ");break;
case 8:printf("Eight ");break;
case 9:printf("Nine ");break;
}
}
void no2(int a,int b){
int aaa=0;
switch(a){
case 1: switch(b){
case 0:printf("Ten ");break;
case 1:printf("Eleven ");break;
case 2:printf("Twelve ");break;
case 3:printf("Thirteen ");break;
case 4:printf("Fourteen ");break;
case 5:printf("Fifteen ");break;
case 6:printf("Sixteen ");break;
case 7:printf("Seventeen ");break;
case 8:printf("Eighteen ");break;
case 9:printf("Nineteen ");break;
}aaa=1;break;
case 2:printf("Twenty ");break;
case 3:printf("Thirty ");break;
case 4:printf("Forty ");break;
case 5:printf("Fifty ");break;
case 6:printf("Sixty ");break;
case 7:printf("Seventy ");break;
case 8:printf("Eighty ");break;
case 9:printf("Ninety ");break;
}
if(aaa==0)
no1(b);
}
int main() {
int t;
long long int a;
scanf("%d",&t);
while(t-->0){
scanf("%lld",&a);
if(a==0)
printf("Zero\n");
else{
no1(m(a,15));
if(m(a,15)!=0)printf("Hundred ");
no2(m(a,14),m(a,13));
if(m(a,15)+m(a,14)+m(a,13)!=0)printf("Trillion ");

        no1(m(a,12));
        if(m(a,12)!=0)printf("Hundred ");
        no2(m(a,11),m(a,10));
        if(m(a,12)+m(a,11)+m(a,10)!=0)printf("Billian ");
    
    
        no1(m(a,9));
        if(m(a,9)!=0)printf("Hundred ");
        no2(m(a,8),m(a,7));
        if(m(a,9)+m(a,8)+m(a,7)!=0)printf("Millian ");
    
            
        no1(m(a,6));
        if(m(a,6)!=0)printf("Hundred ");
        no2(m(a,5),m(a,4));
        if(m(a,6)+m(a,5)+m(a,4)!=0)printf("Thousand ");
    
    
        no1(m(a,3));
        if(m(a,3)!=0)printf("Hundred ");
        no2(m(a,2),m(a,1));
        printf("\n");
    }
}
return 0;

}

@mohammedhussain109
Copy link

How do I Write a Java Program That prompts the user to enter a password and displays it back in the console window. The displayed password should be the entered password halved with the full password total number of characters added at the end.
example1:
enter your password: abcd
your password is : ab4

example2:
enter your password: abcde
your password is :ab5

Note: always assume that the user will enter a password with at least two characters. Additionally, you don’t need to consider the cases if the password length is an odd number (e.g: if the password length is 5 the program should only display the first two characters along with the full length).

@kushalcodes
Copy link

Thank you sir!

@ashprog
Copy link

ashprog commented Nov 23, 2019

@saecmca
Copy link

saecmca commented Dec 26, 2019

Hi, Any one have number to word convert in Romanian Java

@Abdulhay-dev
Copy link

Abdulhay-dev commented Dec 3, 2020

hi , all Best

@Ismail-Ai404
Copy link

Check this one out (by method): https://tinyurl.com/mc25ea8

@gabbriel222
Copy link

How about hundred thousands?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment