473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:::::
Expand|Select|Wrap|Line Numbers
  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.
Mar 28 '08 #1
11 1826
gpraghuram
1,275 Expert 1GB
can you first reverse the number and then call your function to print the values.


raghuram
Mar 28 '08 #2
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. num=num+48;
This is not platform independent use
Expand|Select|Wrap|Line Numbers
  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).
Mar 28 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
This is C, right?

Just use itoa().
Mar 28 '08 #4
Banfa
9,065 Expert Mod 8TB
Just use itoa().
You could do if it exists but itoa() is not part of the standard library.
Mar 28 '08 #5
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.
Mar 28 '08 #6
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
Expand|Select|Wrap|Line Numbers
  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. }
  22.  
For example if the input is the number 5060 the output is 05060!!!!
Mar 28 '08 #7
Banfa
9,065 Expert Mod 8TB
digit is 1 power of 10 too much because it is initialised to 10 instead of 1
Mar 29 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
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?
Mar 31 '08 #9
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!!!
Mar 31 '08 #10
This is C. itoa() is still valid in C, right?
yah but in my assign i cant use anything but getchar and putchar!!!
Mar 31 '08 #11
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
Expand|Select|Wrap|Line Numbers
  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. }
  138.  
ples just give me a hand to start it...
Mar 31 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Laughlin, Joseph V | last post by:
class Foo: x = "blah" y = "blah some more" foo = Foo() x_string = "x" y_string = "y" # I want something like this to work: print foo.x_string
2
by: Pmb | last post by:
I'm trying to learn the syntax for initializing objects in a comma separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to...
11
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
3
by: Gary Smith | last post by:
Hi, I've got a field that contains a list of rooms. In most cases, this contains a single ID. However, under some circumstances, the field may contain a list of two IDs which are broken by a...
3
by: Al | last post by:
Hi Is there any utility available to convert VB.net to asp.net? I have application that I to make it browser based. Before I strart to re write all the Windows based UI I like to know if I can...
1
by: Hennie7863 | last post by:
Hi i want to create a table as follows : if exists (select * from dbo.sysobjects where id = object_id(N'') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table GO Create table...
2
nehashri
by: nehashri | last post by:
i hv a database in access wid Asp as front end. ven given a word in search command(of the front end) it shud go to a table in which each field has words serated by comma. each word shud b checked...
4
nehashri
by: nehashri | last post by:
i want to knw how Search for text within single file in Ms Access, i have my data seperated by commas in the columns....... how do i query through the file for single word. like for example i want...
0
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.