Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Converting an int into seperated int in C

Question posted by: Alenik1989 (Member) on March 28th, 2008 04:41 AM
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: ( text )
  1. int num=5267 /*for example*/
  2. int d;
  3. while(1<=num/10)
  4. {
  5. d=num%10+48; /*im addind 48 to get the num charah from ASCII*/
  6. putchar(d);
  7. num=num/10;
  8. }
  9. num=num+48;
  10. putchar(num);
  11. }

this will print the num 5267 as 7625.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
gpraghuram's Avatar
gpraghuram
Expert
1,086 Posts
March 28th, 2008
05:56 AM
#2

Re: Converting an int into seperated int in C
can you first reverse the number and then call your function to print the values.


raghuram

Reply
Banfa's Avatar
Banfa
AdministratorVoR
4,860 Posts
March 28th, 2008
09:53 AM
#3

Re: Converting an int into seperated int in C
Quote:
Originally Posted by Alenik1989
Code: ( text )
  1. num=num+48;
This is not platform independent use
Code: ( text )
  1. 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).

Reply
weaknessforcats's Avatar
weaknessforcats
Moderator
4,588 Posts
March 28th, 2008
03:07 PM
#4

Re: Converting an int into seperated int in C
This is C, right?

Just use itoa().

Reply
Banfa's Avatar
Banfa
AdministratorVoR
4,860 Posts
March 28th, 2008
04:07 PM
#5

Re: Converting an int into seperated int in C
Quote:
Originally Posted by weaknessforcats
Just use itoa().
You could do if it exists but itoa() is not part of the standard library.

Reply
Alenik1989's Avatar
Alenik1989
Member
64 Posts
March 28th, 2008
06:21 PM
#6

Re: Converting an int into seperated int in C
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.

Reply
Alenik1989's Avatar
Alenik1989
Member
64 Posts
March 28th, 2008
09:29 PM
#7

Re: Converting an int into seperated int in C
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: ( text )
  1. void ConvertBack(int number)
  2. {
  3.    int b,d,e;
  4.    int digit=10;
  5.    b=number;
  6.    while(1<=(b/10))
  7.    {
  8.          digit=digit*10;
  9.          b=b/10;
  10.     }
  11.     d=number;
  12.     while (1<=(digit/10))
  13.     {
  14.          e=(d/digit)+48;
  15.          putchar(e);
  16.          d=d%digit;
  17.         digit=digit/10;
  18.     }
  19.     e=(d/digit)+48;
  20.     putchar(e);
  21. }

For example if the input is the number 5060 the output is 05060!!!!

Reply
Banfa's Avatar
Banfa
AdministratorVoR
4,860 Posts
March 29th, 2008
12:19 AM
#8

Re: Converting an int into seperated int in C
digit is 1 power of 10 too much because it is initialised to 10 instead of 1

Reply
weaknessforcats's Avatar
weaknessforcats
Moderator
4,588 Posts
March 31st, 2008
12:12 AM
#9

Re: Converting an int into seperated int in C
Quote:
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?

Reply
Alenik1989's Avatar
Alenik1989
Member
64 Posts
March 31st, 2008
02:55 AM
#10

Re: Converting an int into seperated int in C
Quote:
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......tanx very much!!!it was an accident!!!

Reply
Alenik1989's Avatar
Alenik1989
Member
64 Posts
March 31st, 2008
02:56 AM
#11

Re: Converting an int into seperated int in C
Quote:
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!!!

Reply
Alenik1989's Avatar
Alenik1989
Member
64 Posts
March 31st, 2008
05:12 AM
#12

Re: Converting an int into seperated int in C
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: ( text )
  1. #include <stdio.h>
  2. void spaces(int &space);
  3. int convert (int &num);
  4. void ConvertBack(int number);
  5. int sum (int operand1,int operand2);
  6. int differ (int operand1,int operand2);
  7. int mult (int operand1,int operand2);
  8. int mod (int operand1,int operand2);
  9. int devide (int operand1,int operand2);
  10.  
  11. int main(void)
  12. {
  13.     int m='y';
  14.     while(m!='n')
  15.     {
  16.     int ch,n,first=0,second=0,flag=1;
  17.     printf("Enter an epression\n");
  18.     n=getchar();
  19.     spaces(n);
  20.     first=convert(n);
  21.     spaces(n);
  22.    
  23.     if(n=='+'||n=='-'||n=='*'||n=='%'||n=='/')
  24.     {
  25.         ch=n;
  26.         putchar(n);
  27.         n=getchar();
  28.         spaces(n);
  29.         second=convert(n);
  30.         spaces(n);
  31.     if(ch=='+')
  32.     {
  33.         putchar('=');
  34.         ConvertBack(sum(first,second));
  35.     }
  36.     else if(ch=='-')
  37.     {
  38.         putchar('=');
  39.         ConvertBack(differ(first,second));
  40.     }
  41.     else if(ch=='%')
  42.     {
  43.         putchar('=');
  44.         ConvertBack(mod(first,second));
  45.     }
  46.     else if(ch=='*')
  47.     {
  48.         putchar('=');
  49.         ConvertBack(mult(first,second));
  50.     }
  51.     else
  52.     {
  53.         putchar('=');
  54.         ConvertBack(devide(first,second));
  55.     }
  56.     }
  57.     printf("\nplease press n to exit or press Enter to continue\n");
  58.     m=getchar();
  59.     }
  60.     return 0;
  61. }
  62.  
  63.  
  64. void spaces(int &space)
  65. {
  66.     while(space==' ')
  67.         {
  68.         putchar(space);
  69.         space=getchar();
  70.         }   
  71. }
  72.  
  73.  
  74.  
  75. int convert(int &num)
  76. {
  77.     int sum=0;
  78.     while(num<='9' && '0'<=num)
  79.     {
  80.         putchar(num);
  81.         sum=sum*10 + (num - '0');
  82.         num=getchar();
  83.     }
  84.     return sum;
  85. }
  86.  
  87.  
  88. void ConvertBack(int number)
  89. {
  90.     int b,d,e;
  91.     int digit=1;
  92.     b=number;
  93.     while(1<=(b/10))
  94.     {
  95.         digit=digit*10;
  96.         b=b/10;
  97.     }
  98.     d=number;
  99.     while (1<=(digit/10))
  100.     {
  101.         e=(d/digit)+48;
  102.         putchar(e);
  103.         d=d%digit;
  104.         digit=digit/10;
  105.     }
  106.     e=(d/digit)+48;
  107.     putchar(e);
  108.     putchar('\n');
  109. }
  110.  
  111.  
  112.  
  113.  
  114. int sum(int operand1, int operand2)
  115. {
  116. return(operand1+operand2);
  117. }
  118.  
  119. int differ(int operand1, int operand2)
  120. {
  121. return(operand1-operand2);
  122. }
  123.  
  124. int mult(int operand1, int operand2)
  125. {
  126. return(operand1*operand2);
  127. }
  128.  
  129. int devide(int operand1, int operand2)
  130. {
  131. return(operand1/operand2);
  132. }
  133.  
  134. int mod(int operand1, int operand2)
  135. {
  136. return(operand1%operand2);
  137. }

ples just give me a hand to start it...

Reply
Reply
Not the answer you were looking for? Post your question . . .
180,430 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top C / C++ Forum Contributors