2

I'm working on a project and I want my program to read a strictly 5 digits number with a leading zero.

How can I print the number with the leading zero included?
Plus: how can I provide that my program reads 5 digits including a zero as a leading number?

zx485
  • 28,498
  • 28
  • 50
  • 59
  • 1
    Show us some code – Norhther Aug 18 '18 at 21:35
  • How do you want to input numbers in your program? Console or through a file? Give us a context for your program. – Greg K. Aug 18 '18 at 21:41
  • What type is the number? Integer? Can you not just read is as a string and verify/numberify it after? – Martin James Aug 18 '18 at 21:42
  • You have asked three questions (the first implied). Please don't ask several as if you are in a tutorial. If you use the format specifier `%d` in `scanf` then leading zeros are ignored. If you use the format specifier `%i` then a leading zero indicates an octal value. – Weather Vane Aug 18 '18 at 21:45
  • If you want exactly 5 digits entered then use a string type input. The question is also unclear about "with a leading zero", must there *always* be a leading zero? Please post example inputs and required output. – Weather Vane Aug 18 '18 at 21:49

3 Answers3

3

I assume you want to read in int variable. If so you can try the below solution.

#include<stdio.h>
void main()
{
  int a;

  scanf("%5d", &a);
  printf("%05d",a);

}
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • 2
    This will accept from 1 to 5 digits (plus a sign) on input. Assuming the number isn't negative, it will print the value with leading zeros. – Jonathan Leffler Aug 18 '18 at 22:15
1

use printf family with '%05d" to print number with leading zeros. use sscanf to read this value (leading zeros are ignored).

Consult the following code:

int a = 25;
int b;
char buffer[6];
sprintf( buffer, "%05d", a );
printf( "buffer is <%s>\n", buffer );
sscanf( buffer, "%d", &b );
printf( "b is %d\n", b );

output is:

buffer is <00025>

b is 25

Eliyahu Machluf
  • 1,251
  • 8
  • 17
1

The best way to have input under control is to read in a string and then parse/analyze the string as desired. If, for example, "exactly five digits" means: "exactly 5 digits (not less, not more), no other leading characters other than '0', and no negative numbers", then you could use function strtol, which tells you where number parsing has ended. Therefrom, you can derive how many digits the input actually has:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

    char line[50];
    if (fgets(line,50,stdin)) {
        if (isdigit((unsigned char)line[0])) {
            char* endptr = line;
            long number = strtol(line, &endptr, 10);
            int nrOfDigitsRead = (int)(endptr - line);
            if (nrOfDigitsRead != 5) {
                printf ("invalid number of digits, i.e. %d digits (but should be 5).\n", nrOfDigitsRead);
            } else {
                printf("number: %05lu\n", number);
            }
        }
        else {
            printf ("input does not start with a digit.\n");
        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58