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. 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
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.
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
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
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.
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.
:-(.
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.
"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.
"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.
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
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
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
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.
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;
}
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.
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
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>
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.
^^
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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"...
|
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.
|
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?...
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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...
|
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: 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...
|
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...
|
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...
|
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...
| |