1

I need to print some leading 0's before a number dynamically at run-time.
Here's what i did :

#include <stdio.h>

int main()
{
    int leading_zero, n;
    scanf("%d %d",&leading_zero, &n);
    for(int i=0; i<leading_zero; i++)
      printf("0");
    printf("%d\n",n);
return 0;
}

Is there any way to do this without loop?

I searched over internet and i found something like this -> printf("%05d\n",n)
which will print static number of leading 0's

Is there any way to do this at run-time?

Ali Akber
  • 3,670
  • 3
  • 26
  • 40
Milon Sarker
  • 478
  • 8
  • 25

1 Answers1

6

If you want to print 0 before a number dynamically at runtime you can do one of these :

printf("%0*d\n", leading_zero, n);

or

printf("%.*d\n", leading_zero, n);
Ali Akber
  • 3,670
  • 3
  • 26
  • 40