Home > database >  Issues with using nested loops to find strong numbers between two user input numbers
Issues with using nested loops to find strong numbers between two user input numbers

Time:10-18

So I am new to C, and have been doing some practice, and nested loops tend to be tougher for me. This new problem about finding a strong number between two user input numbers is confusing me. Essentially, a strong number is a number whose individual digit's factorials add up to the number. e.g. 145 = 1! 4! 5! = 1 24 120.

My issue is that after I input the two numbers, absolutely nothing happens. I am trying to print all strong numbers in between a range.

I have been trying to break the code down by pieces, and added comments to actually make it easier to understand for myself and others.

I am using the following function to find the strong numbers, the int main() just has the user input statements:

void strongno(int num1, int num2)
{
    // num1 and num2 are from user input in the in main() function
    int tot = 0; // to store the total number from each digits factorials
    for(int i = num1; i < num2; i  )
    //getting each individual number in the range, say i = 145
    {
        while(i)
        {
            int rem, fact = 1;

            rem = i % 10;// I am using this to get each digit alone, so first we get 5
            for(int j = 1; j <= rem; j  )
            {
                fact = fact * j;
                //the fact will give us the factorial of 5 = 120
            }
            tot = tot   fact; //tot will add all of the factorials of each remainder
            // while the number is not 0, so for now it is 120
            i = i / 10; // this will give us 14, and the cycle will repeat
        }
        // Once we have our total, we will compare the actual number i
        // to our calculated total. If it matches, it is a strong number
        if(tot == i)
        {
            printf("Strong Number: %d\n", i);
        }
        else
        {
            continue;
        }
    }

}

I feel that the main issue may be with where I am actually placing the if statement or something, however I have tried arranging it and to no avail. The loops make sense to me, how they interact and all. I am trying not to look at the solution for this question, as that never helps, and would prefer guidance with the code I have, to work on a proper solution. Anything is appreciated.

CodePudding user response:

For starters it is better to declare the function with parameters of the unsigned type unsigned int instead of the signed type int

void strongno( unsigned int num1, unsigned int num2);

Otherwise you will have problems with negative numbers.

Using this while loop

while(i)

does not make a sense because it changes the variable i used in the preceding for loop. You need to use some intermediate variable that will assigned with the value of i.

For example

for( ; num1 < num2; num1  )
//getting each individual number in the range, say i = 145
{
    unsigned int i = num1;
    //...

Also the variable tot should be declared and initialized where it is used that is within the outer for loop.

for( ; num1 < num2; num1  )
//getting each individual number in the range, say i = 145
{
    unsigned int i = num1;
    unsigned int tot = 0; // to store the total number from each digits factorials
    //...

And this else statement

    else
    {
        continue;
    }

is just redundant. Remove it.

  • Related