Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting individual digits from a long in C

Tags:

c

I'm doing a homework assignment for my course in C (first programming course). Part of the assignment is to write code so that a user inputs a number up to 9 digits long, and the program needs to determine whether this number is "increasing"/"truly increasing"/"decreasing"/"truly decreasing"/"increasing and decreasing"/"truly decreasing and truly increasing"/"not decreasing and not increasing". (7 options in total)

Since this is our first assignment we're not allowed to use anything besides what was taught in class:

do-while, for, while loops, else-if, if, break,continue scanf, printf ,modulo, and the basic operators

(We can't use any library besides for stdio.h)

That's it. I can't use arrays or getchar or any of that stuff. The only function I can use to receive input from the user is scanf.

So far I've already written the algorithm with a flowchart and everything, but I need to separate the user's input into it's distinct digits.

For example, if the user inputs "1234..." i want to save 1 in a, 2 in b, and so on, and then make comparisons between all the digits to determine for example whether they are all equal (increasing and decreasing) or whether a > b >c ... (decreasing) and so on.

I know how to separate each digit by using the % and / operator, but I can't figure out how to "save" these values in a variable that I can later use for the comparisons.

This is what I have so far:

printf("Enter a positive number : ");

do {
    scanf ("%ld", &number);
    if (number < 0) {
        printf ("invalid input...enter a positive integer: ");
        continue;
    }
    else break;
} while (1);

while (number < 0) {
    a = number % 10;
    number = number - a;
    number = number / 10;
    b = a;
}
like image 626
nofe Avatar asked Apr 06 '12 14:04

nofe


People also ask

How do you extract individual digits from a number?

Extracting digits of a number is very simple. When you divide a number by 10, the remainder is the digit in the unit's place. You got your digit, now if you perform integer division on the number by 10, it will truncate the number by removing the digit you just extracted.


2 Answers

Why not scan them as characters (string)? Then you can access them via an array offset, by subtracting the offset of 48 from the ASCII character code. You can verify that the character is a digit using isdigit from ctype.h.


EDIT

Because of the incredibly absent-minded limitations that your professor put in place:

#include <stdio.h>

int main()
{
  int number;
  printf("Enter a positive number: ");

  do
  {
    scanf ("%ld", &number);
    if (number < 0)
    {
      printf ("invalid input...enter a positive integer: ");
      continue;
    }
    else break;
  } while (1);

  int a = -1;
  int b = -1;
  int c = -1;
  int d = -1;
  int e = -1;
  int f = -1;
  int g = -1;
  int h = -1;
  int i = -1;

  while (number > 0)
  {
    if (a < 0) a = number % 10;
    else if (b < 0) b = number % 10;
    else if (c < 0) c = number % 10;
    else if (d < 0) d = number % 10;
    else if (e < 0) e = number % 10;
    else if (f < 0) f = number % 10;
    else if (g < 0) g = number % 10;
    else if (h < 0) h = number % 10;
    else if (i < 0) i = number % 10;

    number /= 10;
  }

  /* Printing for verification. */

  printf("%i", a);
  printf("%i", b);
  printf("%i", c);
  printf("%i", d);
  printf("%i", e);
  printf("%i", f);
  printf("%i", g);
  printf("%i", h);
  printf("%i", i);

  return 0;
}

The valid numbers at the end will be positive, so those are the ones you validate to meet your different conditions.

like image 106
Evan Mulawski Avatar answered Oct 18 '22 01:10

Evan Mulawski


Since you only need to compare consecutive digits, there is an elegant way to do this without arrays:

int decreasing = 2;
int increasing = 2;

while(number > 9)
{
  int a = number % 10;
  int b = (number / 10) % 10;

  if(a == b)
  {
    decreasing = min(1, decreasing);
    increasing = min(1, increasing);
  }
  else if(a > b)
    decreasing = 0;
  else if(a < b)
    increasing = 0;

  number /= 10;
}

Here, we walk through the number (by dividing by 10) until only one digit remains. We store info about the number up to this point in decreasing and increasing - a 2 means truly increasing/decreasing, a 1 means increasing/decreasing, and a 0 means not increasing/decreasing.

At each step, a is the ones digit and b is the tens. Then, we change increasing and decreasing based on a comparison between a and b.

At the end, it should be easy to turn the values of increasing and decreasing into the final answer you want.

Note: The function min returns the smaller of its 2 arguments. You should be able to write your own, or replace those lines with if statements or conditionals.

like image 45
Aaron Dufour Avatar answered Oct 18 '22 00:10

Aaron Dufour