Binary to Octal code in java

I tried bin to oct code but it was not successful. Can you help me out with it.

@devfest2021_ca0dcbd15233fafb Sure Send me your code and the problem you’re facing.

import java.util.*;

class convertToOct {

public static void main(String args[]) {

    Scanner sc = new Scanner(System.in);

    int bin = sc.nextInt();

    int rem, power = 1, oct = 0;

    while (bin != 0) {

        rem = bin % 8;

        oct += (rem * power);

        power *= 2;

        bin /= 8;

    }

    System.out.println(oct);

}

}

@devfest2021_ca0dcbd15233fafb Your approach is wrong try to use the approach below :

1 Like

Isn’t there any other approach
Any easy one

@devfest2021_ca0dcbd15233fafb Below Approach might be easier.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.