• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Convert number to words java

May 19, 2015 by Dhaval Dave

Write a code to Convert number to words . example  ip 15  op : fifteen

ip : 1234  op : one thousand and thirty four

Algorithm : When we detect a number at any place how to convert to a word. Obviously we need to store it.
See usage of Data Structure in java and Data Structure in c++ .
See usage of static string in java.

Step 1 ) Create array of ones[20] = {“zero”, “one”, “two”, …, “nineteen”};
Step 2) Create array of tens[10] = {“”, “ten”, “twenty”, …, “ninety”};
Step 3) Divide number by 1000, 100, 20 and convert it.
Step 4) Print it.

Convert number to words C++ solution

#include<iostream>
#include<string>
using namespace std;

string units [20] = {"","One ","Two ","Three ","Four ", "Five ","Six ","SEven ","Eight ","Nine ","Ten ","Eleven ", "Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
string tens[] = {"","","Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninty "};
string digits[] = {"","","","Hundred ","Thousand ","Thousand ","Lakh ","Lakh ","Crore "};

string getOneDigit(int val, int index) { 
 if(val < 10) {
  string res = units[val];
  if(val != 0) {
    res = res + digits[index];
  }
  return res;
  }
  return "";
}

string getTensDigit(int val, int index) {
 if(val < 10) {
 string res = tens[val] + digits[index];
 return res;
 } 
 return "";
}

string getTwoDigit(int val, int index) {
 string res = ""; int temp = val;
 if(val < 20) {
  res = units[val] + res;
 }
 else {
  res = getOneDigit(val%10,0) + res;
  val = val /10;
  res = getTensDigit(val %10, 1) + res;
 }
 if(temp != 0) 
  res = res + digits[index];
  return res;
}

string convertToString(int n) {
 string result = "";
 int i = 1, r;
 while(n) {
 if(i != 3) {
   r = n %100;
   result = getTwoDigit(r,i) + result;
   n = n / 100;
   i = i+2;
 }
 else if(i == 3) {
   r = n %10;
   result = getOneDigit(r, i) + result;
   n = n / 10;
   i++;
 }
 }

 return result;
}
int main() {
 int n, t;
 cin>>t;
 while(t--) {
 cout<<"Entet the number to be converted: ";
 cin>>n;
 cout<<"Result : "<<convertToString(n)<<endl;
 }
 return 0;
}

Optimised Solution by : Rahul Goyal (B.Tech in CSE, Amrita School of Engineering)
See OP : https://ideone.com/FT5Rxv

Convert number to words java solution

Java solution with Scanner to word utility ,  which helps user  to input number  in this program.
See why we have used static string to initialize specialNames, tenseNames.

Check out this Best Java Programming Books 

import java.util.Scanner;
class TestClass {
 private static final String[] specialNames = {
 ""," thousand"," million"," billion"," trillion"," quadrillion",
" quintillion"
 };
 
 private static final String[] tensNames = {
 ""," ten"," twenty"," thirty"," forty"," fifty"," sixty"," seventy",
" eighty"," ninety"
 };
 
 private static final String[] numNames = {
 ""," one"," two"," three"," four"," five"," six"," seven"," eight",
 " nine"," ten"," eleven"," twelve"," thirteen"," fourteen"," fifteen",
 " sixteen"," seventeen"," eighteen"," nineteen"
 };
 
 private String convertLessThanOneThousand(int number) {
 String current;
 if (number % 100 < 20){
 current = numNames[number % 100];
 number /= 100;
 }
 else {
  current = numNames[number % 10];
  number /= 10;
  current = tensNames[number % 10] + current;
  number /= 10;
 }
  if (number == 0) return current;
  return numNames[number] + " hundred and" + current;
 }
 
public String convert(int number) {
if (number == 0) { return "zero"; }
  String prefix = ""; 
  if (number < 0) {
  number = -number;
  prefix = "negative";
 }

 String current = "";
 int place = 0;
 
  do {
  int n = number % 1000;
  if (n != 0){
  String s = convertLessThanOneThousand(n);
  current = s + specialNames[place] + current;
  }
  place++;
  number /= 1000;
 }   while (number > 0);
 
 return (prefix + current).trim();
 }
 
public static void main(String[] args) throws Exception {
  TestClass obj = new TestClass();
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  System.out.println(obj.convert(n));
 }
}

Similar Articles

Filed Under: Flipkart Interview Questions, Hacker Earth Questions, Interview Questions, problem Tagged With: Java, string

Reader Interactions

Comments

  1. Rahul says

    September 19, 2015 at 10:17 pm

    I have a solution which I hope you all will like :

    //Wordify.. convert the number to a string

    #include
    #include
    using namespace std;

    string units [20] = {“”,”One “,”Two “,”Three “,”Four “, “Five “,”Six “,”SEven “,”Eight “,”Nine “,”Ten “,”Eleven “, “Twelve “,”Thirteen “,”Fourteen “,”Fifteen “,”Sixteen “,”Seventeen “,”Eighteen “,”Nineteen “};

    string tens[] = {“”,””,”Twenty “,”Thirty “,”Fourty “,”Fifty “,”Sixty “,”Seventy “,”Eighty “,”Ninty “};

    string digits[] = {“”,””,””,”Hundred “,”Thousand “,”Thousand “,”Lakh “,”Lakh “,”Crore “};

    string getOneDigit(int val, int index) {
    if(val < 10) {
    string res = units[val];
    if(val != 0) {
    res = res + digits[index];
    }
    return res;
    }
    return "";
    }

    string getTensDigit(int val, int index) {
    if(val < 10) {
    string res = tens[val] + digits[index];
    return res;
    }
    return "";
    }

    string getTwoDigit(int val, int index) {
    string res = ""; int temp = val;
    if(val >t;
    while(t–) {
    cout<>n;
    cout<<"Result : "<<convertToString(n)<<endl;
    }
    return 0;
    }

  2. anil bhaskar says

    January 24, 2016 at 4:56 pm

    Very simple solution using recursion.
    Please check out here https://github.com/bhaskey/digitoword

Trackbacks

  1. Urban Ladder Written Test. - GoHired says:
    May 19, 2015 at 7:54 pm

    […] 2) Wordify […]

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

DFS (Depth First Search)

Maximum of all subarrays of size k

Find if a binary tree is height balanced ?

SAP Hiring Off-Campus General Aptitude

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Find Nearest Minimum number in left side in O(n)

Daughter’s Age VeryGood Puzzle

Maximum size of square sub matrix with all 1’s in a binary matrix

Interfaces in C++ (Abstract Classes in C++)

Find min element in Sorted Rotated Array (With Duplicates)

Sort an array according to the order defined by another array

Leetcode: Edit Distance

Find min element in Sorted Rotated Array (Without Duplicates)

Flipkart Set 1 On Campus with Answers

Common Ancestor in a Binary Tree or Binary Search Tree

Facebook Interview Question : Interleave List

Skiing on Mountains Matrix

Printing Longest Common Subsequence

CodeChef Code SGARDEN

Code Chef PRGIFT Solution

Client Server C program

Search element in a matrix with all rows and columns in sorted order

flattens 2 D linked list to a single sorted link list

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Microsoft BING Interview Experience

Level order traversal in Spiral form

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Stickler thief

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Copyright © 2025 · Genesis Framework · WordPress · Log in