0

I want to extend the functionality of a part of a program that I'm working now..

Right now my code prints this on screen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
1999

But I'm looking to do this: Putting the tens in text, without number. Also, numbering tens.

1
2
3
4
5
6
7
8
9
ten 1
11
12
13
14
15
16
17
18
19
ten 2
21
22
...
1999

But with the functions that I know of C, I can't figure how to do. Could do this with many ifs in the for, but do not want a code so extensive.

The code of the first output is this:

#include<stdio.h>

int main(void) {

    int i, j=2000;

        for(i=1;i<=j;i++)
        {
            printf("%d\n", i);

        }

    return 0;
}

Very simple, and I want to keep that.

IN SIMPLE WORDS: All numbers ending in 0, should be print "ten x", instead the number...

Thank you.

8
  • What about 100? Do you want to turn that into ten ten? Commented Apr 2, 2012 at 0:43
  • No. Keeping the line of "ten". I mean, the 100 has to be "ten 10". Same thing for the 1000. Commented Apr 2, 2012 at 0:46
  • @suddnely_me: Wait, if you want only the tens place, then that means it would cycle between 0-9, not suddenly jump from 9 to 10. Commented Apr 2, 2012 at 0:46
  • Do you know about the '%' mod operator? It gives the remainder part instead of division, so 19 / 10 = 1, and 19 % 10 = 9. So any number % 10 will == 0 whenever it is a multiple of 10. That could be tested with an if. Commented Apr 2, 2012 at 0:47
  • @user1178392 A little curious...Why do you need this for? Commented Apr 2, 2012 at 0:50

3 Answers 3

3

To find out if a number n is divisible by ten, you use:

if ((n % 10) == 0) ...

That's the modulo operator which returns the remainder when n is divided by ten - numbers divisible by ten have a remainder of zero when you do that, all other numbers have a remainder of one through nine (at least in the non-negative space which is where you're working - it may be different for negative numbers but I'm not going to check since it's not relevant here).

To find which number you need to output with your "ten" string, simply divide n by ten.

So you print statement will become something like:

if ((i % 10) == 0)
    printf ("ten %d\n", i / 10);
else
    printf ("%d\n", i);

Making that change gives you the output:

1
2
3
4
5
6
7
8
9
ten 1
11
12
:
1988
1989
ten 199
1991
1992
1993
1994
1995
1996
1997
1998
1999
ten 200

which appears to be what you're after.


And just one other point, your output appears to stop at 1999 rather than 2000, despite the code. If that's what you really want, either change j to be 1999 or change the for statement to use < instead of <=.

Sign up to request clarification or add additional context in comments.

5 Comments

@suddnely_me, your answer is a block of code with no explanation. I prefer to provide more than that. I'm also not a big fan of expressions like !i%10, especially for beginners, when there are more readable alternatives with no downside (unless your compiler is truly brain-dead).
Nice! Just missed to replace the ten number by the text and no adding instead, but it helps a lot!
@paxdiablo you'd prefer 0 == (i % 10) for beginners then? :)
Well, yes, except the other way around (0 on the RHS). From teaching experience, students find it easier to understand equalities that the auto-conversion of boolean values to integers.
@paxdiablo the second output showed in my answer was the wanted.
1
    #include <stdio.h>
    int main(void)
    {
      int i, j=2000;
      for( i = 1; i <= j; i++ )
      {
        if ( !i%10 ) // if i / 10 is an integer...
        {
          printf ("ten %d\n", i/10);
        } else
        {
          printf("%d\n", i);
        }
      }
      return 0;
    }

5 Comments

Fails on i >= 100. Should give "0" not "10".
@Makoto can't see this condition.
1250 % 10 = 0, satisfying the condition. printf("ten %d\n", i/10) will give 125, not 5.
Check my revision to my reply above.
@Makoto yes, and from my point of view it will be exact truth - 1250 is the 125th ten met.
0

First, you may want to check IF the number is divisible by 10. Look here how to do that: Check if a double is evenly divisible by another double in C?

you could then check how many times it could be divided by 10 and print them out

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.