Digit Extraction

Can someone help with my digit extraction our Teacher ask us to do like this,
WRITE A PRGORAM THAT INPUT 1ST NUMBER AND DISPLAY THE ONES,TENS,HUNDREDS,THOUSANDS

THIS SHOULD BE THE OUTPUT:

First number:
IF 1 to 9

Display : Ones
IF 1 TO 9

DISPLAY : Tens
IF 10 TO 99

DISPLAY : Hundreds
IF 100 TO 999

DISPLAY : Thousands
IF 1000 TO 9999

IF NEGATIVE "OUT OF RANGE"

how to create a code like that using if/else statement?
i really need your help thank you,
Have you bothered to write any code yourself to solve the problem?

This is not a homework site, we won't do all the work for you. Especially for free.

If you have some code please post it.
Yes i have but only the if statement i need the else and the printf output only. wait sir let me show you my code.

#include <stdio.h>
int main()
{
int n1;

printf("enter any number:");
scanf("%", &n1);

if (unit>=9)
{
printf("ones:");
}
if (tens>=99)
{
printf("tens:");
}
if (hundreds>=999);
{
printf("hundreds:");
}
if (thousands>=9999)
{
printf("thousands:");
}
else
}
Last edited on
For the 'if' or 'if ... else if ... else' statements you can read
http://www.cplusplus.com/doc/tutorial/control/
To be honest, they work as they would in English - try saying what you want out loud.

For the output, C++ tends to use streamed output (cout << etc.), but if you want to use printf then there are both specification and examples at
http://www.cplusplus.com/reference/cstdio/printf/

If you show your coding efforts, incomplete though they may be, people will be more inclined to help.

Your problem statement isn't all that clear, but the standard way of getting successive digits is to use the modulo operator in the form number % 10 to get the last digit and then integer-divide by 10, number /= 10, to remove that digit. Continue until the number is zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main()
{
   int n1;
   printf( "Enter any number: ");
   scanf( "%d", &n1 );                 // Use %d

   int units = n1 % 10;                // Need to declare and set units before using it 
   printf( "Units: %d", units );
   
   n1 /= 10;                           // Lop off this digit and continue
   // tens, hundreds etc.
}
Last edited on
Sir @lastchance thanks for the help, but how do i put the tens,hundreds, and thousands? just like this?

int tens = n1 & 99;
scanf("%d", &n1);

int hundreds = n1 & 999;
scanf("%d", &n1);

int thousands = n1 & 9999;
scanf("%d", &n1);

just like that sir?

You get the tens, hundreds and thousands in the same way that you got the units (remembering to reduce the number by factors of 10 before you find each of them).

I'm afraid that looking at your post
int tens = n1 & 99;
scanf("%d", &n1);

I really don't understand your line of thinking. So ... not like that!


Take a numerical example: let's say n1 is input as 3584.

If you set units to n1 % 10 you will get units=4.
Then doing n1 /= 10; you will reduce n1 to 358.

Now set tens to n1 % 10, to get tens=8.
Doing n1 /= 10; again you will reduce n1 to 35.

Now set hundreds to n1 % 10, to get hundreds=5.
Doing n1 /= 10; again you will reduce n1 to 3.

You are left with the number of thousands.


I don't really see where any if statements come into this, unless you choose not to print particular output or you want to stop as soon as a number becomes 0.
really appreciated but, I think someone is missing sir, like here's the code:

Input:
#include <stdio.h>
int main()
{
int n1,n2,n3,n4;
printf( "Enter any number: ");
scanf( "%d", &n1 );

int units = n1 % 10;
printf( "Units: %d", units );

n1 /= 10;

int tens = n2 % 100;
printf( "tens: %d", tens );

n2 /= 100;


int hundreds = n1 % 1000;
printf( "hundreds: %d", hundreds );

n3 /= 1000;

int thousands = n1 % 10000;
printf( "thousands: %d", thousands );

n4 /= 10000;

return 0;
}

The Output is:

Enter any number: 68376
Units: 6tens: 32hundreds: 837thousands: 6837

is there something wrong? or missing the code?

You only need one number (n1) ... not four of them (n1, n2, n3, n4)!
Sir, i change all the input to n1 then the results are still the same on the output

#include <stdio.h>
int main()
{
int n1;
printf( "Enter any number: ");
scanf( "%d", &n1 );

int units = n1 % 10;
printf( "Units: %d", units );

n1 /= 10;

int tens = n1 % 100;
printf( "tens: %d", tens );

n1 /= 100;

int hundreds = n1 % 1000;
printf( "hundreds: %d", hundreds );

n1 /= 1000;

int thousands = n1 % 10000;
printf( "thousands: %d", thousands );

n1 /= 10000;

return 0;
}

OUTPUT :

Enter any number: 1234
Units: 4tens: 23hundreds: 1thousands: 0

what do i do sir?
All of those integer divisions
AND
all of the modulo operations
should be dividing by 10, NOT 100, 1000, 10000 etc.

Then it works.

BTW, please use code tags in future (select your code, then first item on the format menu)
you be mixing and matching two ideas.
if you divide the input by 10, then mod 100 is obviously wrong.

look.
I put into your program 12345.
you say
units = 12345%10 which is 5 which is correct.
then you say
12345/10 which is 1234.
then you say
tens = 1234 % 100, which is 34. this is not right.
if you divide by 10, its mod by 10: 1234 % 10 is 4, which is what you wanted.

Last edited on
Thank you Sir, @lastchance, i finally submitted to my teacher.
Topic archived. No new replies allowed.