nofe
nofe

Reputation: 408

Determining monotonic behavior of a number in C

I want to write a simple program that determines whether the number the user inputs is increasing, strictly increasing, etc. etc.

To solve this I want to break up each digit and see if they are always getting smaller or not, etc. How do I break up the input into separate digits using scanf function?

Upvotes: 0

Views: 96

Answers (2)

Jarosław Gomułka
Jarosław Gomułka

Reputation: 4995

For reading single character use %c.

char digit;
scanf("%c", &digit);

You can also read whole number into char*

char number[100];
scanf("%s", number);

and you can access k-th digit by number[k] - '0'.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182664

One has to wonder why you're not getting the input as integers in the first place (%llu perhaps?) - this would make comparisons so much easier. However, you could get each line from the user via fgets and then parse each character.

Since you mention you want to read large numbers:

int64_t num;
scanf("%"PRId64, &num);

Or maybe:

intmax_t num;
scanf("%jd", &num);

Upvotes: 2

Related Questions