473,408 Members | 2,405 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,408 software developers and data experts.

How do I "read-in" numbers from the left?

I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.
Feb 20 '06 #1
19 3516
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right most
digit printed out. However, they're all backwards since i am reading in
the right-most digit first. How do i make it so it reads the left-most
first, and then proceeds to print? Or is there a way to flip the right
most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


if you want to read digits from left to right, c provides recursives functions...

Xavier
Feb 20 '06 #2
on 2/20/2006, serrand supposed :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right most
digit printed out. However, they're all backwards since i am reading in
the right-most digit first. How do i make it so it reads the left-most
first, and then proceeds to print? Or is there a way to flip the right most
ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


if you want to read digits from left to right, c provides recursives
functions...

Xavier


Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.
Feb 20 '06 #3
gk245 wrote:
on 2/20/2006, serrand supposed :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just
one number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


if you want to read digits from left to right, c provides recursives
functions...

Xavier

Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.


then you can try something like
....
if ( (int)(n/100000) != 0) printf ("%d", (int)(n/100000));
if ( (int)(n/10000) != 0) ...
....

not very nice... but it works with your req ...
Despite... nothing to do with standard c ...

Xavier
Feb 20 '06 #4
gk245 wrote:
on 2/20/2006, serrand supposed :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just
one number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


if you want to read digits from left to right, c provides recursives
functions...

Xavier

Well, thats the thing. I am not allowed to use those functions, only
printf, scanf, switch, and if-else statments.


it seeems to bee an algorithmic problem...
Find the left most digit then proceed...
if ((int)(n/100...0) != 0)
{
/**/ then extract one by one digit with substractions ...
}
else
better :

k = scanf ("%c%c%c%c%c%c%c%c%c", &n);
k == number of digits... then go on

Xavier
Feb 20 '06 #5

gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..

So, the code snippet could be:

scanf("%s", str);
str[0] = .... /* To acess the first digit */

Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).

Hope that helps.

Feb 21 '06 #6
Jaspreet expressed precisely :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..

So, the code snippet could be:

scanf("%s", str);
str[0] = .... /* To acess the first digit */

Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).

Hope that helps.


Hmm, %s i might be able to use somehow. However, i can't use arrays.
:-(.
Feb 21 '06 #7

gk245 wrote:
Jaspreet expressed precisely :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..

So, the code snippet could be:

scanf("%s", str);
str[0] = .... /* To acess the first digit */

Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).

Hope that helps.


Hmm, %s i might be able to use somehow. However, i can't use arrays.
:-(.


You surely have lots of restrictions in place. If you cannot use
arrays, then I guess Xavier did give a solution in a previous post. You
could input the number character by character and then try and use them
to solve your problem.

Feb 21 '06 #8
"Jaspreet" <js***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).


As people pointed out to me in some other thread, this does not apply just
to ASCII. It is generally true because the Standard ensures it.
Feb 21 '06 #9
"gk245" <to*****@mail.com> wrote in message
news:mn***********************@mail.com...
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right
most digit printed out. However, they're all backwards since i am
reading in the right-most digit first. How do i make it so it reads
the left-most first, and then proceeds to print? Or is there a way to
flip the right most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


I do not know if you are "allowed" this but anyway take a look, printdec()
is recursive:

#include <stdio.h>

void printdec(unsigned int i)
{
if (i/10) printdec(i/10);
printf("%d",i%10);
fflush(stdout);
}

int main(void)
{
unsigned int number;

printf("Enter numbers: ");
scanf ("%d", &number);

printdec(number);
printf("\n");

return 0;
}

It can print non-negative numbers in their decimal representation.
I tried to keep your own code too, even if it is not that safe. You can work
out the rest.
Feb 21 '06 #10
Jaspreet wrote:
It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..

So, the code snippet could be:

scanf("%s", str);
str[0] = .... /* To acess the first digit */

Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).


ASCII is irrelevant to the validity of the
(str[index]-'0') expression.

--
pete
Feb 21 '06 #11
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right most
digit printed out. However, they're all backwards since i am reading in
the right-most digit first. How do i make it so it reads the left-most
first, and then proceeds to print? Or is there a way to flip the right
most ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


from left to right or right to left, without arrays, without any extra call but scanf

int n, m, i;
char c;
n = 0;
m = 0;
i = 1;
while (scanf ("%c", &c), c != '\n')
{
n = 10*n + (c-'0');
m += i*(c-'0');
i *= 10;
}
printf ("N = %d %d\n", n, m);

Xavier
Feb 21 '06 #12
serrand formulated the question :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

Well, i have the above setup in a loop, so i keep getting the right most
digit printed out. However, they're all backwards since i am reading in
the right-most digit first. How do i make it so it reads the left-most
first, and then proceeds to print? Or is there a way to flip the right most
ones around afterwards?

I can only use printf, scanf and switch functions right now.

Thx.


from left to right or right to left, without arrays, without any extra call
but scanf

int n, m, i;
char c;
n = 0;
m = 0;
i = 1;
while (scanf ("%c", &c), c != '\n')
{
n = 10*n + (c-'0');
m += i*(c-'0');
i *= 10;
}
printf ("N = %d %d\n", n, m);

Xavier


wow, thats impressive. 8-o
Feb 22 '06 #13

pete wrote:
Jaspreet wrote:
It seems you could input the number as a string using scanf() even
though using scanf has a bug (but you cannot use fgets). Once you have
the input, start accessing the left most number using index 0, 1, and
so on..

So, the code snippet could be:

scanf("%s", str);
str[0] = .... /* To acess the first digit */

Now if you want that character as a numeral digit you could do
str[index]-'0' (Assuming ASCII).


ASCII is irrelevant to the validity of the
(str[index]-'0') expression.

--
pete


Yes realised after I had posted. Not sure why I wrote "assuming ASCII".
Apologies for that.

Feb 22 '06 #14
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

<snip>

I can only use printf, scanf and switch functions right now.


#include <stdio.h>

int main (void)
{
int num = 0, revnum = 0, digit = 0;

printf ("Enter a number: ");
scanf ("%i", &num);

while (num > 0)
{
digit = num % 10;
revnum = revnum*10 + digit;
num /= 10;
}

printf ("Reversed number is %d\n", revnum);
return 0;
}
Feb 22 '06 #15
Sirius Black has brought this to us :
gk245 wrote:
I have something like this:

printf("Enter numbers: ");
scanf ("%i", &number);

If the user put in a bunch of numbers (like 4568), instead of just one
number, i know that you can extract the right most digit with this:

right_number = number % 10;
/* print the number */
number = number / 10;

<snip>

I can only use printf, scanf and switch functions right now.


#include <stdio.h>

int main (void)
{
int num = 0, revnum = 0, digit = 0;

printf ("Enter a number: ");
scanf ("%i", &num);

while (num > 0)
{
digit = num % 10;
revnum = revnum*10 + digit;
num /= 10;
}

printf ("Reversed number is %d\n", revnum);
return 0;
}


I might be reading this the wrong way, but if revnum is set to zero
from the beginning, then revnum*10 will always equal zero. So the
expression 'revnum = revnum*10 + digit' is just goint to set revnum to
the digit's value..... i don't see why revnum*10 is needed since all
its saying is that 0*10 + digit.

Apologies if i am wrong...

thanks.
Feb 22 '06 #16
gk245 wrote:
Sirius Black has brought this to us :
#include <stdio.h>

I might be reading this the wrong way, but if revnum is set to zero from
the beginning, then revnum*10 will always equal zero.


No, if you check the code right, you'll see that "revnum*10" is used to
deslocate the current number to the left, so the last digit can be setted...

This is a typical university exercise haha, I have mine one here yet,
but I quit the university in the same year =]

#include <stdio.h>

int main(int argc, char *argv[]){
int n, r = 0;

printf("\nentre com o numero: ");
scanf("%d", &n);

for(n = abs(n); n; r = r * 10 + n % 10, n /= 10);

printf("o inverso eh: %d", r);
return 0;
}
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Feb 22 '06 #17
gk245 wrote:
Sirius Black has brought this to us :

#include <stdio.h>

int main (void)
{
int num = 0, revnum = 0, digit = 0;

printf ("Enter a number: ");
scanf ("%i", &num);

while (num > 0)
{
digit = num % 10;
revnum = revnum*10 + digit;
num /= 10;
}

printf ("Reversed number is %d\n", revnum);
return 0;
}


I might be reading this the wrong way, but if revnum is set to zero
from the beginning, then revnum*10 will always equal zero. So the
expression 'revnum = revnum*10 + digit' is just goint to set revnum to
the digit's value..... i don't see why revnum*10 is needed since all
its saying is that 0*10 + digit.

"Walk the code" with pen and paper, marking all changes of variables

code | num | revnum | digit |
------------------------+------+--------+-------+
initialization | 0 | 0 | 0 |
scanf | 4568 | 0 | 0 |
digit = num % 10 | 4568 | 0 | 8 |
revnum = ... | 4568 | 0*10+8 | 8 |
num /= 10 | 456 | 8 | 8 |
(loop)
digit = num % 10 | 456 | 8 | 6 |
revnum = ... | 456 | 8*10+6 | 6 |
num /= 10 | 45 | 86 | 6 |
(repeat ...) ... ... ...
printf | 0 | 8654 | 4 |
------------------------+------+--------+-------+

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Feb 23 '06 #18
Pedro Graca formulated on Wednesday :
gk245 wrote:
Sirius Black has brought this to us :

#include <stdio.h>

int main (void)
{
int num = 0, revnum = 0, digit = 0;

printf ("Enter a number: ");
scanf ("%i", &num);

while (num > 0)
{
digit = num % 10;
revnum = revnum*10 + digit;
num /= 10;
}

printf ("Reversed number is %d\n", revnum);
return 0;
}


I might be reading this the wrong way, but if revnum is set to zero
from the beginning, then revnum*10 will always equal zero. So the
expression 'revnum = revnum*10 + digit' is just goint to set revnum to
the digit's value..... i don't see why revnum*10 is needed since all
its saying is that 0*10 + digit.

"Walk the code" with pen and paper, marking all changes of variables

code | num | revnum | digit |
------------------------+------+--------+-------+
initialization | 0 | 0 | 0 |
scanf | 4568 | 0 | 0 |
digit = num % 10 | 4568 | 0 | 8 |
revnum = ... | 4568 | 0*10+8 | 8 |
num /= 10 | 456 | 8 | 8 |
(loop)
digit = num % 10 | 456 | 8 | 6 |
revnum = ... | 456 | 8*10+6 | 6 |
num /= 10 | 45 | 86 | 6 |
(repeat ...) ... ... ...
printf | 0 | 8654 | 4 |
------------------------+------+--------+-------+


doh...i forgot that it was adding the digit, was just looking at revnum
= revnum * 10.

Thanks, i like the table approach. Will use it quite a bit, i am sure.

^^
Feb 23 '06 #19
On Tue, 21 Feb 2006 05:38:56 +0200, "stathis gotsis"
<st***********@hotmail.com> wrote:
I do not know if you are "allowed" this but anyway take a look, printdec()
is recursive:

#include <stdio.h>

void printdec(unsigned int i)
{
if (i/10) printdec(i/10);
printf("%d",i%10);
fflush(stdout);
}
If you have printf (with %u or probably %d) you don't need your
recursive logic. But all you need is putchar ('0' + i%10).

Doing the fflush() for each digit is probably wasteful.
int main(void)
{
unsigned int number;

printf("Enter numbers: ");
scanf ("%d", &number);
#if stdclc
Prompt not ending with newline (and perhaps even with) is not strictly
guaranteed to appear if you don't fflush(stdout).

%d is technically incorrect for unsigned int, use %u.
Or use an int variable; it will converted on the call next.
#endif
printdec(number);
printf("\n");
This could also be a putchar ('\n').

return 0;
}

It can print non-negative numbers in their decimal representation.
I tried to keep your own code too, even if it is not that safe. You can work
out the rest.


- David.Thompson1 at worldnet.att.net
Mar 3 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rich | last post by:
Hi, I'm having problems with changing the Read Only properties. Running Winxp and i cannot get the "read only" to clear off a folder. The folder is one that i want to change the data in and it...
3
by: Justin To via AccessMonster.com | last post by:
I just distributed a FE mde file throught my department, and while stress testing on the performance of the new release, 3 users got the following error: The instruction at ... referenced memory...
0
by: Kennet Andersson | last post by:
Hi there! I want to "read" the remtecontrol used with Terratec Cinergy 600 TV card: http://productsen.terratec.net/modules.php?op=modload&name=News&file=article&sid=142&menu=401 Maby it can...
1
by: John Grandy | last post by:
How to remove "Read Only" status on a file on a remote server? My goal is to delete the file, but System.File.Delete(\\machine1\c$\folder1\file1.txt) is giving me an "Access is denied"...
3
by: Johannes Zellner | last post by:
Hi, can I make an object read-only, so that x = new_value fails (and x keeps it's orginal value)? This would be especially of interest for me for an object created by a c extension.
2
by: James | last post by:
Hello, Whenever I try to delete a Read Only file using FileInfoObject.Delete I get "Access to the path "File name and path" is Denied" error. This does not happen with other files. Any ideas?...
3
by: ano | last post by:
Hi, Anyone knows how to get "xmlns" value from XML file? For example, how to check that this xml file has a xmlns or not? Or how to read the xmlns value? <bookstore...
1
by: suresh810in | last post by:
hi, I got thsi error --------------------------- VB6.EXE - Application Error --------------------------- The instruction at "0x0055a8ea" referenced memory at "0x608af85c". The memory could not...
0
by: ansuhua | last post by:
Hi, I want to read a long string(342425198510303710) from Excel,but what i get is "#####", why? code : string m_czxm = ((Excel.Range)ActiveSheet.Cells.get_Item(2,...
3
kickingthehabbit
by: kickingthehabbit | last post by:
Hi All new to all of this I just downloaded reactor server. everything works fine except Mysql - SQL import files. I am trying to import file createdb.sql I open DB and try and import with...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.