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 )
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); }
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).
|
|
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
|
|
March 28th, 2008 09:53 AM
# 3
|
Re: Converting an int into seperated int in C
Quote:
Originally Posted by Alenik1989
|
This is not platform independent use
Code: ( text )
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).
|
|
March 28th, 2008 03:07 PM
# 4
|
Re: Converting an int into seperated int in C
This is C, right?
Just use itoa().
|
|
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.
|
|
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.
|
|
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 )
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); }
For example if the input is the number 5060 the output is 05060!!!!
|
|
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
|
|
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?
|
|
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!!!
|
|
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!!!
|
|
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 )
#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,second=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(differ(first,second)); } else if(ch=='%') { putchar('='); ConvertBack(mod(first,second)); } else if(ch=='*') { putchar('='); ConvertBack(mult(first,second)); } else { putchar('='); ConvertBack(devide(first,second)); } } printf("\nplease 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); }
ples just give me a hand to start it...
 |
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
|