Cant solve this C prob plz help

This is a discussion on Cant solve this C prob plz help within the C Programming forums, part of the General Programming Boards category; Hello everyone, Following is the problem as given in the "Programming in C" book in Chapter 6, Pg.94 Code: Write ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    28

    Cant solve this C prob plz help

    Hello everyone,

    Following is the problem as given in the "Programming in C" book in Chapter 6, Pg.94

    Code:
    Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display
    
    nine three two
    
    Remember to display “zero” if the user types in just a 0. (Note: This exercise is a hard one!)
    I was able to reach the basic statements but couldnt go further when logic came into the picture.


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main ()
    
    {
    
    int number ;
    
    clrscr () ;
    
    printf ("Enter number\n\n") ;
    
    scanf ("%d", &number) ;
    I strongly feel that if statements are gonna be used in this case


    Plz help

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if:

    Code:
    char words[][]={
    {"zero"},
    {"one"},
    {"two"},
    };
    and the digits were say: 120

    could the digits match up with the index for the right word of a row in the words[row][] array?

    That is, words[0] equals "zero", words[1] row equals "one", etc.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Main problem is: given an integer, how to peel off digits. A sequence of divide-by-10 to extract the units digit... Or another way would be to convert the integer to a string of digits. Then loop through from the beginning and index into a word array like Adak suggested.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    28
    Quote Originally Posted by nonoob View Post
    Main problem is: given an integer, how to peel off digits. A sequence of divide-by-10 to extract the units digit... Or another way would be to convert the integer to a string of digits. Then loop through from the beginning and index into a word array like Adak suggested.

    Oh, how come the author has asked an Array question when Arrays start from chapter 7!!! Thats strange....Nevertheless I would highly recommend this book for learning C from scratch... Thanks alot for helping
    Last edited by exus69; 12-08-2010 at 08:10 AM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by exus69 View Post
    Oh, how come the author has asked an Array question when Arrays start from chapter 7!!!
    Because there are other ways to do it (e.g. if/else like you mentioned or switch/case). Then chapter 7 comes along and says, "But wait! Look at this (easier) way of doing it!"
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project .... help plz plz plz
    By Hitchhik3R in forum C Programming
    Replies: 13
    Last Post: 10-24-2010, 02:07 PM
  2. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  3. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 07:02 PM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. Plz solve my problem !
    By pankaj8096 in forum C Programming
    Replies: 12
    Last Post: 01-15-2003, 11:59 PM

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