public static boolean compare(int[] a,int n)
    {

        for(int i=0;i<n;i++)
        {
         for(int j=i+1;j<n;j++)
         {
             if((a[i]*a[j])%2==0)

                 return false;
             else
                 return true;
         }
        }
     }

there's an error coming when I try to include this function in my prog. I think the place of return true is causing problem.When I write the return statement outside the two for loops then there is no error. Why is it like this?

Recommended Answers

All 4 Replies

What happens if n is 0 (or 1 for that matter)?

P.S. that's a hint/clue.

What happens if n is 0 (or 1 for that matter)?

P.S. that's a hint/clue.

Thanks for your reply. I am still not able to find solution to this problem.Then how to return boolean value here?

In other words, think of a logical "default" value to be returned and add that as a return line at the end of the method. The compiler is complaining because you have declared the method to return a boolean, but, logically, paths exist where nothing would be returned.

In other words, think of a logical "default" value to be returned and add that as a return line at the end of the method. The compiler is complaining because you have declared the method to return a boolean, but, logically, paths exist where nothing would be returned.

Thanks. I think I fixed it. I am returning false at the end.When n is less than 0 then also it'll return false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.