Reputation: 478
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?
Upvotes: 1
Views: 314
Reputation: 3800
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);
Upvotes: 6