-3

I have to print out 2 digit numbers (00, 01, 02, 03,... ,10, 11,..99) i.e. from 00 to 99 using only one integer and function putchar().

In ASCII table, these are signs from 0x30 (0) to 0x39(9).

Also i may only use stdio.h library.

Output example:

00  
01  
02  
03  
...(all the way to 99)
99  

Which operation would you suggest to make this possible?
I already made working solution but I had to use two integers (one from first part of 2 digit number, and other for second). But this is not allowed.

cse
  • 4,066
  • 2
  • 20
  • 37
Totenkoph93
  • 43
  • 1
  • 9
  • What a ridiculous requirement... – DeiDei May 03 '18 at 12:51
  • Reading [the help pages](http://stackoverflow.com/help), and [how to ask good questions](http://stackoverflow.com/help/how-to-ask) is a good start. And learning how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). I also recommend [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 03 '18 at 12:52
  • im not fan of it either but it is required to do it this way – Totenkoph93 May 03 '18 at 12:54
  • It might as well be impossible with only `putchar`. – DeiDei May 03 '18 at 12:54
  • Check if that integer is < 10 and then append a zero in front of it? Or is that also not allowed? – Neijwiert May 03 '18 at 12:55
  • 2
    Welcome to Stack Overflow. This is not a homework completion service. Your instructor gave you the assignment, not us, and you're going to need to do your own work. If you can't get started, ask your teacher for help; they're being paid to teach you. Good luck. – Ken White May 03 '18 at 12:57
  • As for your assignment, it's a simple `for` loop, with some basic arithmetic (modulo, division, addition). – Some programmer dude May 03 '18 at 12:58
  • 2
    Hint: call `putchar` twice, once for for each digit, using % 10 and / 10 to get each one. – dbush May 03 '18 at 12:59
  • how i see it, the only reasonable approach would be to use `putchar() two times`: one for first part of digit and second time for second part.. For example: `int x = 0x30` `putchar(x)` `putchar(x+1)` will produce `01`...(increment x each time 0x39)... – Totenkoph93 May 03 '18 at 13:02
  • 2
    Hint: Try doing e.g. `5 + '0'`. What is the result? If you print that as a character (using e.g. `putchar`), what do you print? – Some programmer dude May 03 '18 at 13:05
  • @Totenkoph93 You can do `int main(void) { int i=0; while(i<100) { putchar('0'+i/10); putchar('0'+i%10); putchar('\n'); i++; } return 0; }`. See it [working here](https://ideone.com/R0odZr). – cse May 03 '18 at 13:09
  • thank you very much for proposed solutions and sorry for posting a trivial question as this one, i will do my best to improve. – Totenkoph93 May 03 '18 at 13:13

2 Answers2

4
for(int i = 0; i < 100 ; i++){

    putchar((i/10)+'0');
    putchar((i%10)+'0');
    printf("\n");
}
Martin Chekurov
  • 733
  • 4
  • 15
0

This should work. And you're defining only one integer.

void foo()
{
    int a = 0;
    while(a <= 99)
    {
        if(a < 10)
        {
            putchar((char)i/10 + '0');
            putchar((char)a%10 + '0');
            putchar('\n');
        }
        a++;
    }
}
Petar Velev
  • 2,305
  • 12
  • 24