Home > Software design >  I can't see the error in my code. The code is running but it is stuck in a loop
I can't see the error in my code. The code is running but it is stuck in a loop

Time:01-25

I was writing code that finds the sum of the digits of the product of all n-digit numbers. I can't see the error in my code. The code is running but it is stuck in a loop. This is my code :

import java.util.Scanner;
public class SummarynDigitNumbersDigitMultiplyDigit {
    public static void main (String[] args) {
        Scanner s = new Scanner(System.in);
        double n = s.nextInt();
        double Digit;
        double Summary = 0;
        double MultipliedDigit = 1;
        double start = Math.pow(10,n-1), finish=Math.pow(10,n);
        for ( double i = start; i<finish; i  ){
            while (i>0) {
                Digit = i % 10;
                MultipliedDigit *= Digit;
                i = i / 10;
            }
            Summary =MultipliedDigit;
        }
        System.out.println(Summary );
    }
}

If I write 1 to n Summary = 45 .

CodePudding user response:

In your inner (while) loop, you set i = i/10, effectively making sure that i will never reach the termination condition of the for loop

  • Related