C++ Get Each Digit in Int

Getting each individual digit from a whole integer

You use the modulo operator:

while(score)
{
printf("%d\n", score % 10);
score /= 10;
}

Note that this will give you the digits in reverse order (i.e. least significant digit first). If you want the most significant digit first, you'll have to store the digits in an array, then read them out in reverse order.

How to extract digits from a number in C? Begining from the most significant digit?

Assuming a 32 bit signed number that is positive, then as a simple example:

#include <stdio.h>

int main()
{
int rmdr;
int dvsr;
int quot;
scanf("%d", &rmdr); // read in number
dvsr = 1000000000;
while(0 >= (quot = rmdr / dvsr)){ // skip leading zeroes
rmdr %= dvsr;
dvsr /= 10;
if(dvsr == 1)
break;
}
while(dvsr){ // display number
quot = rmdr / dvsr;
printf("%1d", quot);
rmdr %= dvsr;
dvsr /= 10;
}
printf("\n");
return(0);
}

or a slight optimization:

int main()
{
int rmdr;
int rm10;
int dvsr;
int quot;
scanf("%d", &rmdr); // read in number
rm10 = rmdr/10;
dvsr = 1;
while(dvsr <= rm10) // skip to 1st digit
dvsr *= 10;
while(dvsr){ // display number
quot = rmdr / dvsr;
printf("%1d", quot);
rmdr %= dvsr;
dvsr /= 10;
}
printf("\n");
return(0);
}

Extracting individual digits from a long in C

Why not scan them as characters (string)? Then you can access them via an array offset, by subtracting the offset of 48 from the ASCII character code. You can verify that the character is a digit using isdigit from ctype.h.


EDIT

Because of the incredibly absent-minded limitations that your professor put in place:

#include <stdio.h>

int main()
{
int number;
printf("Enter a positive number: ");

do
{
scanf ("%ld", &number);
if (number < 0)
{
printf ("invalid input...enter a positive integer: ");
continue;
}
else break;
} while (1);

int a = -1;
int b = -1;
int c = -1;
int d = -1;
int e = -1;
int f = -1;
int g = -1;
int h = -1;
int i = -1;

while (number > 0)
{
if (a < 0) a = number % 10;
else if (b < 0) b = number % 10;
else if (c < 0) c = number % 10;
else if (d < 0) d = number % 10;
else if (e < 0) e = number % 10;
else if (f < 0) f = number % 10;
else if (g < 0) g = number % 10;
else if (h < 0) h = number % 10;
else if (i < 0) i = number % 10;

number /= 10;
}

/* Printing for verification. */

printf("%i", a);
printf("%i", b);
printf("%i", c);
printf("%i", d);
printf("%i", e);
printf("%i", f);
printf("%i", g);
printf("%i", h);
printf("%i", i);

return 0;
}

The valid numbers at the end will be positive, so those are the ones you validate to meet your different conditions.

Get a part of an integer in C

Getting a single digit as an int can be done mathematically:

int num = 897;
int dig1 = (num / 1 ) % 10;
int dig2 = (num / 10 ) % 10;
int dig3 = (num / 100 ) % 10;

Division by one on the first line is only for illustration of the concept: you divide by n-th power of ten, starting with 100 = 1.

How do I determine the number of digits of an integer in C?

floor (log10 (abs (x))) + 1

http://en.wikipedia.org/wiki/Logarithm

How to extract each digit from int without using stack or array? (C++)

One easy way is through recursion. In fact, it is quite elegant. You can write something like this:

string barCode(int zip)
{
if (zip <= 0) return "";
return (barCode(zip / 10) + codeForDigit(zip % 10));
}

Basically, you're using the call stack as your stack for storing the previous strings. It's also a minimal way of writing this function.

scanf each digit of integer AND store the whole number in C

I will try to answer your first question. You can simply multiply the entered numbers by multiples of 10. Lets show it with calculation:

5*1 + 4*10 + 3*100 + 2*1000 + 1*10000 = 12345

So, here is the code for a manual use:

...
scanf("%1d%1d%1d%1d%1d",&i,&i2,&i3,&i4,&i5);
i*=10000;
i2*=1000;
i3*=100;
i4*=10;
i5*=1;
...


Related Topics



Leave a reply



Submit