C++

Status
Not open for further replies.

siriusb

Cyborg Agent
The current best free c++ compiler would be MinGW compiler. You can get it along with Dev C++ by Bloodshed soft editor, which is free too.
But I use MS VC++ or TC++ depending upon the need. MS VC++ can do C++ very well, but for dos graphics programming I use TC++.
 

comm

Right off the assembly line
c programming

hey guys, i have problem with "c" progmming. the problem is- enter any 4 digit no. and display that no. in words. ex- 1234. result- one two three four.

plz hep me , it's urgent. :roll: :roll: :roll: :roll: :roll: :roll: :roll: :roll:
 

siriusb

Cyborg Agent
comm, if u have a question not related to this post, then post it as a new thread.
BTW, I guess you can do it by doing a itoa() on the number variable to get a char array of the numbers. Loop through this array and with a if...then construct, you can output the desired string.
 
OP
himtuna

himtuna

Journeyman
In c++ (don't know about c )you first extract all the digits then use switch case to get the desired output.
Extract 4digits like this
int a,b,c,d,n;
cin>>n;
a=n/1000;
b=(n%1000)/100;
c=(n%100)/10;
d=n%10;
switch(a)
case 1: cout<<"one";
...
....do it for b,c,d.
 

hafees

In the zone
do u want it to read "one two three four" or "one thousand two hundred and thirty four". If it is the first then it is very easy.
 
OP
himtuna

himtuna

Journeyman
Then also it is easy
Like for first switch(a)
case1:
cout<<One thousand;
and for second switch(b)
case n:
cout<< n hundred;
since there are only four digits specified.
 

hafees

In the zone
It is not easy in that case. Also when extracting numbers, numbers can be extracted only from right to left (using the mod % operatror). Try ur logic wth 1234. In ur logic u need to extract all numbers first and should be inserted into an array. One thosand (ok) two hundred (ok), three (what now? you have to check it in detail here). Ok it is not complicated. but require some lines of codes.

Here is my program which can read up to a maximum long int size or a 32 bit number in DOS platform.

Code:
//program to accept amount and to print it in wor
#include<stdio.h>
#include<conio.h>
#include<values.h>
void printsingles(int);
void printtenths(int);
void printthousands(int);
void printlacks(int);

int main()
{
	long int num,left;
	int tens,hundreds,thousands;
	clrscr();
	printf("Enter an Integer Number [Maximum %ld ] ",MAXLONG);
	scanf("%ld",&num);
	if(num<0) //if negative
	{
		printf("Minus");
		left=num* (-1); //convert into +ve
	}
	else
		left=num;
	do
	{
		if(left<20)
		{
			printsingles(left);
			left=0;
		}
		else if(left<100)
		{
			tens=left/10;
			printtenths(tens*10);
			left-=(tens*10);
		}

		else if(left<1000)
		{
			hundreds=left/100;
			printsingles(hundreds);
			printf(" Hundred");
			left-=(hundreds*100);
			if(left>0) printf(" and");
		}
		else if (left<10000)
		{
			thousands=left/1000;
			printsingles(thousands);
			printf(" Thousand");
			left-=(thousands*1000);
			if(left>0) printf(" and");
		}
		else if (left<100000L)
		{
			thousands=left/1000;
			printthousands(thousands);
			printf(" Thousand");

			left-=(long)(thousands*1000L);
			if(left>0) printf(" and");
		}
		else if (left<100000000L)
		{
			int lacks;
			lacks=left/100000L;
			printlacks(lacks);
			printf(" Lacks");
			left-=(long)(lacks*100000L);
			if(left>0) printf(" and");
		}
		else
		{
			printf("Unable to convert the number %ld into words",num);
			break;
		}

	}
	while(left);
	getch();
	return 0;
}


void printsingles(int num)
{
	switch(num)
	{
		case 0: printf(" Zero");break;
		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;
		case 10: printf(" Ten");break;
		case 11: printf(" Eleven");break;
		case 12: printf(" Tweleve");break;
		case 13: printf(" Thirteen");break;
		case 14: printf(" Fourteen");break;
		case 15: printf(" Fifteen");break;
		case 16: printf(" Sixteen");break;
		case 17: printf(" Seventeen");break;
		case 18: printf(" Eighteen");break;
		case 19: printf(" Ninteen");
	}

}

void printtenths(int tens)
{
	switch(tens)
	{
	case 20: printf(" Twenty");break;
	case 30: printf(" Thirty");break;
	case 40: printf(" Forty");break;
	case 50: printf(" Fifty");break;
	case 60: printf(" Sixty");break;
	case 70: printf(" Seventy");break;
	case 80: printf(" Eighty");break;
	case 90: printf(" Ninty");break;
	}
}

void printthousands(int t)
{
	int tens;
	if(t<20)
	{
		printsingles(t);

	}
	else if(t<100)
	{
		tens=t/10;
		printtenths(tens*10);
		t-=(tens*10);
		if(t>0) printsingles(t);
	}
}

void printlacks(int t)
{
	int tens;
	if(t<20)
	{
		printsingles(t);

	}
	else if(t<100)
	{
		tens=t/10;
		printtenths(tens*10);
		t-=(tens*10);
		if(t>0) printsingles(t);
	}
}
 
Status
Not open for further replies.
Top Bottom