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.
To find out if a number
nis divisible by ten, you use:That’s the modulo operator which returns the remainder when
nis 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 dividenby ten.So you print statement will become something like:
Making that change gives you the output:
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
jto be 1999 or change theforstatement to use<instead of<=.