Search code examples
javareturnprimespalindromefunction-call

return statement in Java use


I'm a beginner Java programmer. I am learning methods, functions and return statements. While solving questions on finding the prime numbers and palindrome numbers within a range given by the user I could notice that in the public static boolean prime(int n) function I needed to compulsorily mention return (true) before ending the prime(int n) function even though I was returning proper true and false statements within the if and else blocks for checking whether its prime or not. However in palindrome(int n) function I was asked not to put return statement before ending the scope even though here also I was returning proper true and false statements within the if and else blocks for checking whether its prime or not.

Code for printing prime numbers within a given range

//printing prime numbers within a range given by the user
  import java.util.*;
  public class primeiinrange
  {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the first range");
        int a = sc.nextInt();
        System.out.println("enter the second range");
        int b = sc.nextInt();
        int c=0;
        for (int i = a; i <= b; i++) {
            if (prime(i) == true) {
                System.out.print(i + ", ");
                c++;
            }
        }
        if (c == 0) {
            System.out.println("No prime number found");
        }
    }

    public static boolean prime(int n)
    {
       if(n<=1)
       { 
           return false;
       }
       int c=2;
       while(c*c<=n)
       {
           if(n%c==0)
           { 
               return false;
           }
          c++;
       }
       if(c*c>n)
       {
           return true;
       }
       return true; //if this return statement is not given then error shown: missing return statement 
    }
}

Code for printing prime numbers within a given range

// palindrome numbers between a given range
import java.util.*;
public class palindrome 
{
    public static void main(String[] args) 
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter the first range");
    int a = sc.nextInt();
    System.out.println("enter the second range");
    int b = sc.nextInt();
    int c=0;
    for (int i = a; i <= b; i++) {
        if (palindrome(i) == true) {
            System.out.print(i + ", ");
            c++;
        }
    }
    if (c == 0) {
        System.out.println("No palindrome number found");
        }
    }

public static boolean palindrome(int n)
    {
    int copy = n;
    int add = 0;
    int rem = 0;
    while (n > 0) {
        rem = n % 10;
        add = add * 10 + rem;
        n = n / 10;
    }
    if (add == copy) {
        return true;
    } else {
        return false;
    }
    //if return (true) statement is given in this line the error shown: unreachable statement
    }
}

why do we have to write return (true) before ending scope of of prime(int n) compulsorily but we cannot write return (true) before the end of scope of palindrome(int n)?If anyone could help a bit, that would be very nice .Thanks. NOTE: I use IntelliJ Idea IDE


Solution

    1. When detecting prime numbers, the statement if (c * c > n) is redundant because this line is reached only when while (c * c <= n) loop has completed, that is, this condition is already verified in the while:
    public static boolean prime(int n) {
        if (n <= 1) { 
            return false;
        }
        int c = 2;
        while (c * c <= n) {
            if (n % c == 0) { 
                return false;
            }
            c++;
        }
        return true; 
    }
    
    1. When detecting a palindrome, instead of if-else statement returning a boolean, a cleaner form should be used to return the result of the comparison:
    public static boolean palindrome(int n) {
        int copy = n;
        int add = 0;
        int rem = 0;
        while (n > 0) {
            rem = n % 10;
            add = add * 10 + rem;
            n = n / 10;
        }
        return add == copy;
    }