Calculating the ranks of classmates' exam grades The questions from hackerearth:


  Geeko is in worry now because an exam is coming up and he has to know
  what rank he can get on exams. So he goes back into the the school records
  and finds the amazing pattern.
  
  He finds that if a student is having a current rank \$n\$, then his rank on
  the final exam will be the count positive numbers between in the range
  \$[1,n]\$ which are relatively prime to \$n\$.
  
  As being a geek, he became curious now and wants to calculate the rank of
  all his classmates on the final exam.  But he finds this task a bit hard,
  so he asks you programmers to solve this task for him.
  
  Input: The first line of each test file contains a integer \$t\$ denoting
  the number of test case.  Each test case contains a numbers \$n\$
  representing the current rank of each student
  
  Output: for each test case output single integer the rank of student
  in final exam in new line.
  
  Constraints:
  
  \$1 &lt;= t &lt;= 2000\$
  
  \$1 &lt;= n &lt; 10^6\$


My solution:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

class relative {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        ArrayList&lt;Integer&gt; input = new ArrayList&lt;Integer&gt;();

        ArrayList&lt;Integer&gt; result = new ArrayList&lt;Integer&gt;();
        // int input[] = new int[t];
        // int result[] = new int[t];
        for (int i = 0; i &lt; t; i++) {
            int count = 1;
            int in;
            input.add(in = Integer.parseInt(br.readLine()));
            for (int j = 2; j &lt; in; j++) {
                if (in % j == 0)
                    continue;
                else if (checkRelativePrime(j, in))
                    count++;
            }
            result.add(count);
        }
        for (int i : result)
            System.out.println(i);
    }

    static boolean checkRelativePrime(int a, int b) {
        int i = 2;
        while (a &gt; 1) {
            if (a % i == 0) {
                {
                    if (b % i == 0)
                        return false;
                }
                a = a / i;
            } else
                i++;
        }

        return true;
    }
}
in your checkRelativePrime method you have 

static boolean checkRelativePrime(int a, int b) {
    int i = 2;
    while (a &gt; 1) {
        if (a % i == 0) {
            {
                if (b % i == 0)
                    return false;
            }
            a = a / i;
        } else
            i++;
    }

    return true;
}



you have an extra set of brackets
you have a statement in purgatory.

a = a / i;


it sits in-between the ending bracket of your if statement and the else statement that is supposed to be attached to the if statement
if you use brackets for one part of an if statement, you should use them for the entire if statement. I recommend using brackets even for one-liners no matter what, it's cleaner and leaves less room for mistakes.




I think you wanted to keep that if statement separate from the declaration to the a variable, but you don't need to do that(especially if you are using brackets properly)

like this

while (a &gt; 1) {
    if (a % i == 0) {
        if (b % i == 0) {
            return false;
        }
        a = a / i;
    } else {
        i++;
    }
}


if you enter that if statement and return false then a = a /i; will never happen and your boolean method will return false