Download Quick Currency Converter


Number to words/Number to figures C/C++

Description: Remember those lessons from school where you were given numbers and you had to write them in figures/words? Specifically, you are given some thing like "4516" , and you write them as "Four Thousand five hundred sixteen". Well, its not just a kids lesson, bankers and managers also need them to generate receipts. This project just makes their job easy; it receives a number (less than a billion) and find out its words/figure representation.

Difficulty: Hard

Language: C++

Compiler/IDE: Visual Studio, dev C++ (others will work as well)



View Source

            							
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;

string units(int n, string s[] );
string tens(int n, string s[], string t[]);
string hundreds(int n, string s[], string t[], string u[]);
string thousands(int n, string s[], string t[], string u[]);
string lakhs(int n, string s[], string t[], string u[]);
string crores(int n, string s[], string t[], string u[]);

int main()
{
	double input;	
	string s[] = {"", "One", "Two", "Three", "Four", "Five" , "Six",
    	"Seven", "Eight", "Nine", "Ten","Eleven", "Twelve", "Thirteen",
     	"Fourteen", "Fifteen", "Sixteen","Seventeen", "Eighteen", "Nineteen"};
     
	string t[]={"Twenty ", "Thirty ", "Fourty ", "Fifty " , "Sixty ",
    	"Seventy ", "Eighty ", "Ninety "};
    
	string u[] ={"Hundred ", "Thousand ", "Lakh ", "Crore "};
	
	cout<<"Write a number\n";
	cin>>input;
        if (input > 999999999 || input <0)
	   cout<<"Input must be less than 1 billion and "
       		 "greater than or equal to zero\n";
        
	else if (input == 0)
		cout<<"Zero\n";
	else
	{	int n = input;
		cout<<crores(n , s , t, u);
	}
	getch();

}

string units(int n, string s[])
{
	
	return s[n];

}

string tens(int n, string s[], string t[])
{
	if (n < 20)
	   return units(n ,s);
	else
	   return t[n/10 - 2] + units(n % 10, s);

}

string hundreds(int n, string s[], string t[], string u[])
{
	if (n < 100)
	   return tens(n , s , t);
	else
	   return units(n / 100 , s) + " "  + u[0] + tens(n % 100, s,t);

}


string thousands(int n, string s[], string t[], string u[])
{
	if (n < 1000)
	   return hundreds(n , s ,t, u);
	else
	   return tens(n/1000,s,t) + " " + u[1] + hundreds(n % 1000, s,t,u);

}
 
string lakhs(int n, string s[], string t[], string u[])
{
	if (n < 100000)
	   return thousands(n,s,t,u);
	else
	   return tens(n/100000,s,t) + " " + u[2] + thousands(n % 100000,s,t,u);

}

string crores(int n, string s[], string t[], string u[])
{
	if ( n<10000000)
	   return lakhs(n,s,t,u);
	else
	   return tens(n/10000000,s,t) + " " + u[3] + lakhs(n % 10000000,s,t,u);

}
       


Notes:This project consists of a set of functions; we will break down each for you. The "units" function is pretty straight forward. It handles numbers less than 20 and directly returns the value from the string "s". The next function is "tens". It handles numbers less than 100. Note that the "tens" function only handles the tens part and to calculate the unit part, it calls the unit function, and here comes the beauty of the program: each function is responsible only for its own part, and uses the help of lower functions.

Now consider the function "hundreds". Again this function is responsible for the hundreds parts (<1000) and then use the help of "tens" function (which in turn use "units"). The other three functions: "thousands" , "lakhs" , "crores" work similarly.

When an input is given, the program calls the "crores" function (the highest in the hierarchy). This function handles the crores part(if it exists) and then makes its way through "lakhs" , "thousands" down to the "units" function.