number crunching

This is a discussion on number crunching within the C Programming forums, part of the General Programming Boards category; please help i am really new to c and am having trouble i need to know how to input a ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    Angry number crunching

    please help i am really new to c and am having trouble
    i need to know how to input a number and print it with the numbers reversed like 123 to 321. I have not had any luck.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    What have you tried?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i need to know how to input a number and print it with the numbers reversed like 123 to 321.
    Play around with the modulus operator and division and a loop. Or place the number in a string and print it out in reverse with a loop.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    Use somewhat stack mechanism.


    Create a stack for holding integers.
    Pass all digits in the number to the stack as they are encountered and then pop elements off the stack and print it.

    Got it ?

    If not then the best way is to use strings.

    Accept input as a string say 'num'

    then call strrev(num);

    its simple.

  5. #5
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    I have made this small program that accompalishes that task.
    But you can't input big numbers due to limitation of integer size.

    Try changing all int to long for big numbers.

    #include <stdio.h>

    /* To find length of number eg : 123 length 3 */
    int len(int num)
    {
    int l=1;
    while(num=num/10)
    l++;
    return l;
    }
    /* --------------------------------------------*/



    int power(int num)
    {
    int ans=1;
    while(--num)
    ans*=10;
    return ans;
    }


    int main()
    {
    int n,l,mul,copy,ans=0,rem;
    printf("Enter a number : ");
    scanf("%d",&n);
    copy=n;
    l=len(n);
    mul=power(l);
    do
    {
    rem=copy%10;
    copy=copy/10;
    ans+=mul*rem;
    mul/=10;
    }while(copy);

    printf("\nOriginal Number : %d\nReverce Number : %d\n",n,ans);

    getch();
    }



    In the mean time if somebody else has better code then this (I am sure it would be there ) then post it here.

    As my program uses to many variables. I will also try finding some different logic to make this happen in small amount of code.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please use code tags when posting code.

    Why not code it like Prelude suggested. The code to do it that way can be written in just a few lines.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    I know that code can be written in very short using arrays.

    But I think (THINK) that the way suggested by prelude would be to use modulo and division operator in a loop and printing out the reverse of the number digit by digit as calculation goes on.

    But what if we need reverse of a number in further calculations in our program.

    The code I have written doesn't use arrays or strings. And the output can be used by ohter parts of program also.

    (Focusing on modularity not on getting results)

    I am not a master in C. That's why i mentioned if somebody has made small program then post the code not algorithm.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here are two ways:
    Code:
    #include <stdio.h>
    
    void revprint(int i)
    {
      while (i) 
      {
        printf ("%d", i%10);
        i /= 10;
      }
      putchar ('\n');
    }
    
    int revint(int i)
    {
      int newnum = 0;
      while (i)
      {
        newnum = (newnum*10) + (i%10);
        i /= 10;
      }
      return newnum;
    }
    
    int main(void)
    {
      printf ("By revprint: 123 becomes ");
      revprint(123);
      printf ("By revint: %d becomes %d\n", 123, revint(123));
      return 0;
    }
    
    /*
     Output
    By revprint: 123 becomes 321
    By revint: 123 becomes 321
     *
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    Thanks for the code.

    It requires practice to be at that level. Hope one day i will reach your level.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 07:57 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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