-3
import java.util.*;
public class Composite_Magic
{
    public static void main()
    {
        int i,j,m,n,fact=0,sum=0,temp=0;
        boolean k=false;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 2 numbers as upper and lower bound and all composite numbers between them will be displayed");
        m=sc.nextInt();
        n=sc.nextInt();
        sc.close();
        if(m<n){
            for(i=m;i<=n;i++)
            {
                for(j=1;j<=i;j++)
                {
                    if(i%j==0)
                        fact++;
                }                    
                sum=i;
                while(k==false)
                {
                    temp=sum;
                    while(temp>0)
                    {
                        sum=sum+(temp%10);
                        temp=temp/10;
                    }
                    if(sum/10==0)
                        k=true;
                }
                if(sum==1 && fact>2) 
                    System.out.println(i);
            }
        }
        else
            System.out.println("Invalid Input");
    }
}

So I have asked for input only twice but it doesn't stop.

Is this a bug or some error that I'm making?

This is my complete program.

This is the screenshot of the terminal window

Dasyud
  • 23
  • 8

2 Answers2

1

It's just that this loop:

        while(k==false)
        {
            temp=sum;
            while(temp>0)
            {
                sum=sum+(temp%10);
                temp=temp/10;
            }
            if(sum/10==0)
                k=true;
        }

seems to never end.
I don't know what you're trying to do with it, but k does not become true
or it will take a lot of time.
During this time you think that you're prompted to give new numbers but you're not.
You're just typing and pressing enter.
To prove this just type ppp. This should throw InputMismatchException but it does not.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • I just figured out that the problem is with this loop. That loop is used to check if a number is a magic number or not. Can you help me with the logic? – Dasyud Oct 26 '18 at 13:13
  • I have identified the mistake and now I need help fixing it. – Dasyud Oct 26 '18 at 13:21
  • @AdityaSoni this is another issue. The issue of this question is solved. Now you should post another question with another title and with all the relevant info about what you're trying to achieve and where the error is – forpas Oct 26 '18 at 13:23
  • Should I delete the question because it's useless? It already has -2 thanks to my stupidity. – Dasyud Oct 26 '18 at 13:36
  • @AdityaSoni I don't think it's useless but it's your question and you can do whatever you want with it. – forpas Oct 26 '18 at 13:37
0

You have to close the scanner by using sc.close(); but still there is an issue with your loop I have re-implanted the code using my own code this should work now.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter 2 numbers as upper and lower bound and all composite numbers between them will be displayed");
    int m = sc.nextInt();
    int n = sc.nextInt();
    sc.close();
    if (m < n) {
        for (int i = m; i <= n; i++) {
            int f = 0;
            for (int i2 = 1; i2 <= n; i2++) {
                if (n % i2 == 0)
                    f++;
            }
            if (f > 2) {
                int num = i;
                do {
                    num = sumOfDigits(num);
                } while (num > 9);
                if (num == 1) {
                    System.out.println(i);
                }
            }
        }
    } else {
        System.out.println("Invalid Input");
    }
}

public static int sumOfDigits(int n) {
    int s = 0;
    while (n > 0) {
        s += n % 10;
        n /= 10;
    }
    return s;
}

which makes an output of

10
19
28
37
46
55
64
73
82
91
100
SamHoque
  • 2,978
  • 2
  • 13
  • 43