-2

I am working on a project for university. One of the small things that make me lose points is that my answer is correct but it doesn't have four figure number.

For example say I print my answer which is 17 and on other test files it could change to 6. Is there a way I can ensure that it would always print either

0017 -> testfile_1
0006 -> testfile_2
0123 -> testfile_3

so it always has four digits no matter what the solution is? I made lots of test cases to check if the value is a single number or multiple to work it out but is there a simple way to do this so it automatically adds the zeros?

Jongware
  • 22,200
  • 8
  • 54
  • 100
dave_1234
  • 147
  • 1
  • 3
  • 10

2 Answers2

1

Try to use

printf( "%04d", myValue);

04 will make sure that your myValue will always have, at least, 4 digits.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

You can specify how many numbers printf should print, like so:

printf("%04d",someint);

Read more here

Magisch
  • 7,312
  • 9
  • 36
  • 52