C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-19-2009, 12:24 PM   #1
Registered User
 
Join Date: Sep 2008
Posts: 37
Question program that reads a number between 1 and 999 and spells it in english

This is my second program and I'm stuck, i don't know if I'm doing it right, so far i did the hundreds, and tenths, i don't know how to do the Ones, and the 11, 12,......
Also please let me know of those little mistakes too like the brackets and signs.

Write a program that reads a number between 1 and 999 from user and spells out it in English.

For example:
If number is 453, your program prints out “Four hundred fifty three”
If number is 37, your program prints out “Thirty seven”
If number is 214, your program prints out “Two hundred fourteen”
while(1) {
printf(“Enter a number between 1-999 (enter 0 to exit): “);
scanf(“%d”, &number);
if (number==0) break;
if (number > 999 || number < 1) continue;





Code:
#include<stdio.h>
#include<math.h>

int main(void)
{
    while(1) {
      printf("Enter a number between 1-999 (enter 0 to exit): ");
      scanf("%d", &number);
      if (number==0) break;
      if (number > 999 || number < 1) continue
      a=number/100;
      b=number/10;
      
      switch(a){
                case 1: 
                     printf("One Hundred");
                     break;
                case 2:
                     case 1: 
                     printf("Two Hundred");
                     break;
                case 3: 
                     printf("Three Hundred");
                     break;
                case 4: 
                     printf("Four Hundred");
                     break;
                case 5: 
                     printf("Five Hundred");
                     break;
                case 6: 
                     printf("Six Hundred");
                     break;
                case 7: 
                     printf("Seven Hundred");
                     break;
                case 8: 
                     printf("Eight Hundred");
                     break;
                case 9: 
                     printf("Nine Hundred");
                     break;
                     
       switch(b){
                case 1: 
                     printf("Ten");
                     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;
                
                               
                     
                     
                     return 0;
                     
                     }
Cyberman86 is offline   Reply With Quote
Old 02-19-2009, 12:55 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
Same approach as you've got so far would seem to work.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 02-19-2009, 01:13 PM   #3
Registered User
 
Join Date: Feb 2009
Posts: 278
Code:
#include<stdio.h>
#include<math.h>

int main() {
  int hundreds = 0;
  int tens = 0;
  int ones = 0;
  int number; //input
  
  while(1) {
    printf("Enter a number between 1-999 (enter 0 to exit): ");
    scanf("%d", &number);
      
    if (number==0) break;

    hundreds = number / 100;
    tens = (number - (hundreds * 100)) / 10;
    ones = number - (hundreds * 100) - (tens * 10);

    if (hundreds > 0) {
      if      (hundreds == 9) printf("Nine");
      else if (hundreds == 8) printf("Eight");
      else if (hundreds == 7) printf("Seven");
      else if (hundreds == 6) printf("Six");
      else if (hundreds == 5) printf("Five");
      else if (hundreds == 4) printf("Four");
      else if (hundreds == 3) printf("Three");
      else if (hundreds == 2) printf("Two");
      else                    printf("One");

      printf(" Hundred ");
      //if (tens == 0 && ones == 0) printf("\n");
    }

    if (tens != 1) {  // we have a special case (teens)
      if      (tens == 9) printf("Ninety ");
      else if (tens == 8) printf("Eighty ");
      else if (tens == 7) printf("Seventy ");
      else if (tens == 6) printf("Sixty ");
      else if (tens == 5) printf("Fifty ");
      else if (tens == 4) printf("Forty ");
      else if (tens == 3) printf("Thirty ");
      else if (tens == 2) printf("Twenty ");

      if (ones > 0) {
        if      (ones == 9) printf("Nine\n");
        else if (ones == 8) printf("Eight\n");
        else if (ones == 7) printf("Seven\n");
        else if (ones == 6) printf("Six\n");
        else if (ones == 5) printf("Five\n");
        else if (ones == 4) printf("Four\n");
        else if (ones == 3) printf("Three\n");
        else if (ones == 2) printf("Two\n");
        else                printf("One\n");
      } else {
        printf("\n");
      }
    } else {
      if (ones > 0) {
        if      (ones == 9) printf("Nineteen\n");
        else if (ones == 8) printf("Eightteen\n");
        else if (ones == 7) printf("Seventeen\n");
        else if (ones == 6) printf("Sixteen\n");
        else if (ones == 5) printf("Fifteen\n");
        else if (ones == 4) printf("Fourteen\n");
        else if (ones == 3) printf("Thirten\n");
        else if (ones == 2) printf("Twelve\n");
        else                printf("Eleven\n");
      } else {
        printf("Ten\n");
      }
    }
  }

  return 0;
}
Bladactania is offline   Reply With Quote
Old 02-19-2009, 01:14 PM   #4
Registered User
 
Join Date: Feb 2009
Posts: 278
I used if/else ifs but your switches work ok... its just the math part of yours thats iffy. but you were on the right track
Bladactania is offline   Reply With Quote
Old 02-19-2009, 02:04 PM   #5
Registered User
 
Join Date: Oct 2008
Posts: 567
Ok, I was bored so I decided to code it myself. It's not perfect. For instance, the last character it prints is always a space. And there might still be bugs, I haven't tested much. But at least it shows one of the better ways to do it.

Code:
#include <stdio.h>
#include <stdlib.h>

static struct {
	int value;
	const char *name;
} number_scales[] = {
	{ 1000000, "million" },
	{ 1000, "thousand" },
	{ 100, "hundred" }
	};

void print_number(int num)
{
	unsigned int i;

	if(num < 0) {
		printf("minus ");
		num = -num;
	}

	for(i = 0; i < sizeof(number_scales) / sizeof(*number_scales); i++) {
		int scale_value = num / number_scales[i].value;
		if(!scale_value)
			continue;

		print_number(scale_value);
		printf("%s ", number_scales[i].name);
		num %= number_scales[i].value;
		if(num == 0)
			return;
	}

	if(num >= 20) {
		switch(num / 10) {
		case 2: printf("twenty "); break;
		case 3: printf("thirty "); break;
		case 4: printf("fourty "); 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;
		default: break;
		}

		num %= 10;
		if(num == 0)
			return;
	}

	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("twelve "); 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("nineteen "); break;
	default: break;
	}
}


int main(int argc, char **argv)
{
	int num = argc > 1 ? atoi(argv[1]) : 0;
	print_number(num);
	printf("\n");
	return 0;
}

Last edited by EVOEx; 02-19-2009 at 02:07 PM.
EVOEx is offline   Reply With Quote
Old 02-19-2009, 07:06 PM   #6
30 Helens Agree
 
neandrake's Avatar
 
Join Date: Jan 2002
Posts: 607
Now given a number N, print out the Nth number (from 0) which does not contain the letter 'e' in its spelling.
__________________

AIM: Neandrake
EMAIL: nta0 @ yahoo . com

Operating System: Windows XP SP2
Compiler: GCC
IDE: Notepad++


Don't give up your freedom to think - www.cognitiveliberty.org
neandrake is offline   Reply With Quote
Old 02-19-2009, 07:19 PM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Code:
	for(i = 0; i < sizeof(number_scales) / sizeof(*number_scales); i++) {
		int scale_value = num / number_scales[i].value;
		if(!scale_value)
			continue;

		print_number(scale_value);
		printf("%s ", number_scales[i].name);
		num %= number_scales[i].value;
		if(num == 0)
			return;
	}
If you invert the condtion in red, you can avoid the "continue", and wrap all the remaining code in a block. Makes the code easier to read and follow.

Instead of using switch, why not use a table indexed by the tens and units?

The last:
Code:
	default: break;
should probably be a
Code:
printf("We shouldn't get here: num = %d\n", num);
statement.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 05:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22