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::::: -
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.
11 1826
can you first reverse the number and then call your function to print the values.
raghuram
Banfa 9,065
Expert Mod 8TB
This is not platform independent use
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).
This is C, right?
Just use itoa().
Banfa 9,065
Expert Mod 8TB
Just use itoa().
You could do if it exists but itoa() is not part of the standard library.
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.
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 -
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!!!!
Banfa 9,065
Expert Mod 8TB
digit is 1 power of 10 too much because it is initialised to 10 instead of 1
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?
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!!!
This is C. itoa() is still valid in C, right?
yah but in my assign i cant use anything but getchar and putchar!!!
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 -
#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...
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
| |