Search code examples
arrayscfor-loop

Getting undesirable output from a variable inside the for loop


I wanted to try and make an armstrong number check program in C using arrays and for loops but have been facing problem with what input is getting saved in the variable input_num below;

In my code:

#include <stdio.h>
#include <math.h>
int main(){
int x, j, num[50];
    printf("Enter the size of your number: ");
    scanf("%d", &x);
    for (j = 0; j < x; j++) {
      printf("Write the %d th digit of your num: ", j + 1);
      scanf("%d", &(num[j]));
    }
    int input_num;
    for (j = 0; j < x; j++) {
      input_num = printf("%d", num[j]);
    }
    printf("%d \n", input_num);
    int power[50];
    for (j = 0; j < x; j++) {
      power[j] = pow(num[j], x);
      printf("%d\n", power[j]);
    }
    int sum = 0;
    for (j = 0; j < x; j++) {
      sum = sum + power[j];
    }
    if (input_num == sum) {
      printf("Its an armstrong number!!!\n");
    } else {
      printf("not an armstrong no. :( \n");
    }
 }

I tried to convert the elements of the array num (eg. {1,2,3}) into a single number "123" by saving the output of the for loop in the variable input_num but whenever I entered initially that the size of my number is 3 digits, and entered "123" as the number, the number inside input_num will be saved as "1231", basically an extra 1 is getting added to the unit place and I have been unable to figure out that how can I get rid of it. What am I doing wrong?


Solution

  • I tried out your code as is and did get the same behavior and output as you noted due to the apparent eclectic usage of the output from the "printf" statements where your program is attempting to construct the input number.

    First off, when I attempt to assist folks in solving issues, I look for any compiler errors and warnings when building a program, and indeed, the compiler did issue a warning about how the "input_num" variable was being initialized.

    /home/craig/C_Programs/Console/Arm/main.c|25|warning: ‘input_num’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
    

    And running your program with the input of a number that would be an Armstrong number yielded the following output at the terminal.

    craig@Vera:~/C_Programs/Console/Arm/bin/Release$ ./Arm 
    Enter the size of your number: 3
    Write the 1 th digit of your num: 1
    Write the 2 th digit of your num: 5
    Write the 3 th digit of your num: 3
    1531 
    1
    125
    27
    not an armstrong no. :( 
    

    As noted in the good comments, your understanding of the output from the "printf" statement would not build the value you are expecting, so you should probably delve more into some "C" tutorials, either online or in publications to get a better understanding.

    Even though there are multiple ways of refactoring your program to provide a proper answer as to whether or not an entered number is an Armstrong number, I refactored the block of code to store the desired entry number from:

    int input_num;
    for (j = 0; j < x; j++) {
      input_num = printf("%d", num[j]);
    }
    

    to:

    int input_num = 0;          /* Define and initialize the variable   */
    for (j = 0; j < x; j++) {
      input_num += num[j] * pow(10, (x - j - 1));   /* Generate the integer from the digits */
    }
    

    This refactoring derives the desired value and then allows for the testing as an Armstrong number. Following, are a couple of tests at the terminal with this minimal refactoring.

    craig@Vera:~/C_Programs/Console/Arm/bin/Release$ ./Arm 
    Enter the size of your number: 3
    Write the 1 th digit of your num: 1
    Write the 2 th digit of your num: 5
    Write the 3 th digit of your num: 3
    153 
    1
    125
    27
    Its an armstrong number!!!
    
    craig@Vera:~/C_Programs/Console/Arm/bin/Release$ ./Arm 
    Enter the size of your number: 4
    Write the 1 th digit of your num: 1
    Write the 2 th digit of your num: 6
    Write the 3 th digit of your num: 3
    Write the 4 th digit of your num: 4
    1634 
    1
    1296
    81
    256
    Its an armstrong number!!!
    

    The main takeaway from this is again, it probably would be beneficial to delve into some "C" tutorials to get a better comfort level with how functions work along with any result output that they produce, and also pay attention to any compiler warnings you may receive when building a program or project. Warnings often times are clues that you may receive results that you were not expecting.