So first we need to calculate the number of digits before the decimal point.
Code -
n is the number
if (n < 1){
cout << "0000" << n << endl;
}
else{
len = 0;
x = n
while (x >= 1){
x /= 10;
len += 1;
}
for (int i = 0; i < 5-len; i++){
cout << "0";
}
cout << n << endl;
}
printf allows some formatting options.
example :-
printf(“leading zeros %05d”, 123);
this will print 5 digits with leading zeroes . Similar way for floating point %05.4f