Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Count number of digits in array and display them, plus multiply it by given number

stan14892stan14892 usMember Posts: 1

I have to create an array from an entered value. I also need to display array contents, which I have done and that performs to my liking. I am just having some issues trying to have the program count the amount of digits and display them "digits detected" I also need to multiply the original entered value (number) by 11 and display it at the end of my program.

Problems I need help debugging:
1.) Counting and displaying how many digits was entered
2.) Multiplying first entered number by 11.

#include <stdio.h>

int main() {

int number, count, mult = 11;
int numbers[9];

printf("Please enter a number:\n>");
scanf("%d", &number);

for(count = 4; count >= 0; count--) {

    if(number <= 0)
        numbers[count] = 0;

    numbers[count] = number % 10;
    number /= 10;

}

for(count = 0; count < 5; count++)
    printf("Digit Value: %d\t\n", numbers[count]);

while(number!=0)
{
    number/=10;
    ++count;
}
printf("Digits Detected: %d\n", number);
return 0;
}

Comments

  • B00B00 Member Posts: 97 ✭✭✭
    edited March 2016

    Your code is a mess.
    1. You only take 1 number, what's the array for?
    2. Why negative numbers set to 0?
    3. Your array is size of 9 but your loops only loop through 5 elements.
    4. You overwrite your numbers array with digits and zeroes because there is no stop at last digit.
    5. While loop does not make sense.
    6. At the end of all this you lost user input long ago and have nothing to multiply.

    Should go something like this (pseudocode):

    SIZE = 10
    numbers[SIZE]
    multiplier = 11
    
    foreach 0..SIZE as i:
      print "enter number: "
      read numbers[i]
    
    foreach 0..SIZE as i:
      /* digits will be in reverse */
      temp = numbers[i]
      c = 0
      print temp + " digits: "
      if temp < 0:
        temp * -1 /* removing sign from digits */
      while temp % 10:
        print temp % 10
        temp /= 10
        c++
      print "\n"
      print "total: " + c
      print numbers[i] + " x " multiplier + " = " + numbers[i] * multiplier + "\n"
    
Sign In or Register to comment.