Write a program to convert numbers to words.
Example 1:
Input: 1392431860
Output: One Billion Three Hundreds Ninety Two Million Four Hundreds Thirty One Thousands Eight Hundreds Sixty
Approach
Java
public class ConvertNumberToWords {public static void main(String[] args) {int num = 1392431860;String words = convertNumberToWords(num);System.out.println(words);}// Method for convet numner to wordsprivate static String convertNumberToWords(int num) {// if number is negativeif (num < 0)return "Minus " + convertNumberToWords(-num);// if number is zeroif (num == 0)return "";// if number less then 20if (num < 20) {return digits[num];}// if number less then 100if (num <= 99) {return ten[num / 10] + ((num % 10 != 0) ? " " : "")+ digits[num % 10];}// if number less then 1000 (Hundreds condition)if (num <= 999) {return convertNumberToWords(num / 100) + " Hundred"+ ((num % 100 != 0) ? " " : "")+ convertNumberToWords(num % 100);}// Thousanda conditionsif (num <= 999999) {// calling in recurringreturn convertNumberToWords(num / 1000) + " Thousand"+ ((num % 1000 != 0) ? " " : "")+ convertNumberToWords(num % 1000);}// Million conditionif (num <= 999999999) {// calling in recurringreturn convertNumberToWords(num / 1000000) + " Million"+ ((num % 1000000 != 0) ? " " : "")+ convertNumberToWords(num % 1000000);}// Billion condition and recurring callingreturn convertNumberToWords(num / 1000000000) + " Billion"+ ((num % 1000000000 != 0) ? " " : "")+ convertNumberToWords(num % 1000000000);}public static final String[] digits = { "", "One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten", "Eleven", "Twelve", "Thirteen", "Fourteen","Fifteen", "Sixteen", "Seventeen", "Eighteen","Nineteen" };public static final String[] ten = { "", "", "Twenty", "Thirty","Forty", "Fifty", "Sixty", "Seventy", "Eighty","Ninety" };}
C++
#include <bits/stdc++.h>using namespace std;//vector string to hold the each digits//till the 19vector<string> digits = { "", "One", "Two","Three", "Four", "Five", "Six", "Seven","Eight", "Nine","Ten", "Eleven", "Twelve","Thirteen", "Fourteen", "Fifteen", "Sixteen","Seventeen", "Eighteen", "Nineteen" };//vector string to hold the//tens valuevector<string> ten = { "", "", "Twenty","Thirty", "Forty", "Fifty", "Sixty", "Seventy","Eighty","Ninety" };//funtion for convet number to wordsstring convertNumberToWords(int num){// if number is negativeif (num < 0)return "Minus " + convertNumberToWords(-num);// if number is zeroif (num == 0)return "";// if number less then 20if (num < 20) {return digits[num];}// if number less then 100if (num <= 99) {return ten[num / 10] + ((num % 10 != 0) ? " " : "")+ digits[num % 10];}// if number less then 1000 (Hundreds condition)if (num <= 999) {return convertNumberToWords(num / 100) + " Hundred"+ ((num % 100 != 0) ? " " : "")+ convertNumberToWords(num % 100);}// Thousanda conditionsif (num <= 999999) {// calling in recurringreturn convertNumberToWords(num / 1000) + " Thousand"+ ((num % 1000 != 0) ? " " : "")+ convertNumberToWords(num % 1000);}// Million conditionif (num <= 999999999) {// calling in recurringreturn convertNumberToWords(num / 1000000) + " Million"+ ((num % 1000000 != 0) ? " " : "")+ convertNumberToWords(num % 1000000);}// Billion condition and recurring callingreturn convertNumberToWords(num / 1000000000) + " Billion"+ ((num % 1000000000 != 0) ? " " : "")+ convertNumberToWords(num % 1000000000);}int main(){int num = 1392431860;string words = convertNumberToWords(num);cout<<words<<"\n";return 0;}
No comments:
Post a Comment