Converting an int into seperated int in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alenik1989
    New Member
    • Oct 2007
    • 64

    Converting an int into seperated int in C

    I just want to convert an int number such as 5267 or 1000000 into seperated integeres.
    like for number 5267 it be 5 2 6 7 , so i can print them with putchar(). I know, but i have to use only putchat and getchar in my code. ( no printf or scanf).
    I had some thoughts about it but the only thing that i came up is this code which prints the number backward: PLS helpPLS:::::
    [CODE=c]
    int num=5267 /*for example*/
    int d;
    while(1<=num/10)
    {
    d=num%10+48; /*im addind 48 to get the num charah from ASCII*/
    putchar(d);
    num=num/10;
    }
    num=num+48;
    putchar(num);
    }[/CODE]
    this will print the num 5267 as 7625.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    can you first reverse the number and then call your function to print the values.


    raghuram

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by Alenik1989
      Code:
      num=num+48;
      This is not platform independent use
      Code:
      num=num+'0';
      which is.

      Personally I would do the following

      Writie a function that outputs a string (array of char) using putchar

      Make your code write the digits into a string first and then call the function above.

      When you have written the digits into a string reverse the string before printing (again I would make a function to do this, it sounds like a useful generic piece of functionality).

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is C, right?

        Just use itoa().

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by weaknessforcats
          Just use itoa().
          You could do if it exists but itoa() is not part of the standard library.

          Comment

          • Alenik1989
            New Member
            • Oct 2007
            • 64

            #6
            Ya, it would have been so much easier if i could use arrays, string,or itoa().But unfortunetly im not allowed to use even printf !!!! Only getchar and putchar.

            Comment

            • Alenik1989
              New Member
              • Oct 2007
              • 64

              #7
              I wrote this function to seperate an int number , but it doest work right. it adds an extra 0 in front of every number....can anybody help me pls to find the problem...
              my function is
              [CODE=C]
              void ConvertBack(int number)
              {
              int b,d,e;
              int digit=10;
              b=number;
              while(1<=(b/10))
              {
              digit=digit*10;
              b=b/10;
              }
              d=number;
              while (1<=(digit/10))
              {
              e=(d/digit)+48;
              putchar(e);
              d=d%digit;
              digit=digit/10;
              }
              e=(d/digit)+48;
              putchar(e);
              }
              [/CODE]
              For example if the input is the number 5060 the output is 05060!!!!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                digit is 1 power of 10 too much because it is initialised to 10 instead of 1

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by Banfa
                  Quote:
                  Originally Posted by weaknessforcats
                  Just use itoa().
                  You could do if it exists but itoa() is not part of the standard library.
                  This is C. itoa() is still valid in C, right?

                  Comment

                  • Alenik1989
                    New Member
                    • Oct 2007
                    • 64

                    #10
                    Originally posted by Banfa
                    digit is 1 power of 10 too much because it is initialised to 10 instead of 1
                    tanx yeah that was the problem......ta nx very much!!!it was an accident!!!

                    Comment

                    • Alenik1989
                      New Member
                      • Oct 2007
                      • 64

                      #11
                      Originally posted by weaknessforcats
                      This is C. itoa() is still valid in C, right?
                      yah but in my assign i cant use anything but getchar and putchar!!!

                      Comment

                      • Alenik1989
                        New Member
                        • Oct 2007
                        • 64

                        #12
                        My assign was to write a code to do the simple aritmathics.( +, - , / , % , * )
                        using putchar and getchar.
                        I wrote my code, but im completely stuck on error checking.
                        My i/p should be like ^^num^^ oper. sign^^num^^ enter.
                        like
                        12 +13 Enter
                        the error check should be about>>>>
                        ----- 1 2+ 12 no space(s) between an operand
                        -----No negative operands
                        -----No extra aritmathic sign
                        my code is
                        [CODE=c]
                        #include <stdio.h>
                        void spaces(int &space);
                        int convert (int &num);
                        void ConvertBack(int number);
                        int sum (int operand1,int operand2);
                        int differ (int operand1,int operand2);
                        int mult (int operand1,int operand2);
                        int mod (int operand1,int operand2);
                        int devide (int operand1,int operand2);

                        int main(void)
                        {
                        int m='y';
                        while(m!='n')
                        {
                        int ch,n,first=0,se cond=0,flag=1;
                        printf("Enter an epression\n");
                        n=getchar();
                        spaces(n);
                        first=convert(n );
                        spaces(n);

                        if(n=='+'||n=='-'||n=='*'||n==' %'||n=='/')
                        {
                        ch=n;
                        putchar(n);
                        n=getchar();
                        spaces(n);
                        second=convert( n);
                        spaces(n);
                        if(ch=='+')
                        {
                        putchar('=');
                        ConvertBack(sum (first,second)) ;
                        }
                        else if(ch=='-')
                        {
                        putchar('=');
                        ConvertBack(dif fer(first,secon d));
                        }
                        else if(ch=='%')
                        {
                        putchar('=');
                        ConvertBack(mod (first,second)) ;
                        }
                        else if(ch=='*')
                        {
                        putchar('=');
                        ConvertBack(mul t(first,second) );
                        }
                        else
                        {
                        putchar('=');
                        ConvertBack(dev ide(first,secon d));
                        }
                        }
                        printf("\npleas e press n to exit or press Enter to continue\n");
                        m=getchar();
                        }
                        return 0;
                        }


                        void spaces(int &space)
                        {
                        while(space==' ')
                        {
                        putchar(space);
                        space=getchar() ;
                        }
                        }



                        int convert(int &num)
                        {
                        int sum=0;
                        while(num<='9' && '0'<=num)
                        {
                        putchar(num);
                        sum=sum*10 + (num - '0');
                        num=getchar();
                        }
                        return sum;
                        }


                        void ConvertBack(int number)
                        {
                        int b,d,e;
                        int digit=1;
                        b=number;
                        while(1<=(b/10))
                        {
                        digit=digit*10;
                        b=b/10;
                        }
                        d=number;
                        while (1<=(digit/10))
                        {
                        e=(d/digit)+48;
                        putchar(e);
                        d=d%digit;
                        digit=digit/10;
                        }
                        e=(d/digit)+48;
                        putchar(e);
                        putchar('\n');
                        }




                        int sum(int operand1, int operand2)
                        {
                        return(operand1 +operand2);
                        }

                        int differ(int operand1, int operand2)
                        {
                        return(operand1-operand2);
                        }

                        int mult(int operand1, int operand2)
                        {
                        return(operand1 *operand2);
                        }

                        int devide(int operand1, int operand2)
                        {
                        return(operand1/operand2);
                        }

                        int mod(int operand1, int operand2)
                        {
                        return(operand1 %operand2);
                        }
                        [/CODE]
                        ples just give me a hand to start it...

                        Comment

                        Working...