h-rai
h-rai

Reputation: 3974

long variable is not accepting a value

I am trying to assign a value to a long variable but eclipse shows compile error. Can anyone work it out what is wrong with this? I have check and am assured that the value is in long's range.

public static void main(String[] args) {
        **long num = 600851475143;**

        for(long i = num/2; i<1; i--) {
            if(num%i == 0 && isPrime(i) == true) {
                System.out.println(i);
                break;
            }
        }

    }

Upvotes: 0

Views: 1265

Answers (2)

Jigar Joshi
Jigar Joshi

Reputation: 240928

add L and make it

long num = 600851475143L;

Also See

Upvotes: 5

Joey
Joey

Reputation: 354714

You have to append L to the literal to tell the compiler it's a long. Integer literals in Java are int by default; since the number you entered doesn't fit in an int, the compiler complains.

Upvotes: 3

Related Questions