Home > OS >  I cant find you what is wrong in the Marked loop in this program
I cant find you what is wrong in the Marked loop in this program

Time:03-10

For example. The value of the wanted sum of 2534 is 154 (2^4 5^3 3^2 4^1 = 16 125 9 4 = 154).

int main()
{
    int b, a, s, j, i, ss = 0, t;
    do {
        printf("TYPE A NATURAL NUMBER: ");
        scanf_s("%d", &b);
    } while (b < 1);
    t = b;
    for (i = 1; b != 0; i  ) {
        a = b % 10;
        if (i < 2) {
            ss  = a;
        }
        s = a;
        for (j = 1; j >= 2; j--) {
            s *= a;
        }
        if (i >= 2) {
            ss  = s;
        }
        b /= 10;
    }
    printf("value of the wanted sum  %d is %d", t, ss);
    return 0;
}

CodePudding user response:

You can use this:

for (i = 1; b != 0; i  ) {
    a = b % 10;
    s = a;
    for (j = i; j > 1; j--) {
        s *= a;
    }
    ss  = s; 
    b /= 10;
}

The main problem is your inner loop. for (j = 1;j>=2..) will never execute as j starts from 1 and is never bigger than 2.

  •  Tags:  
  • c
  • Related