Java Program for Check Octal Number

By | 2018-03-21T21:14:06+05:30 June 20th, 2016|java programs|

Here we are going to write a program to check given number is octal number or not using Java.

Check Octal Number or Not ?

import java.util.Scanner;

public class IsOctal_Check {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Number : \n");
        String input = sc.nextLine();
        System.out.println("isOctal : "+isOctal(input));
    }
    
    public static boolean isOctal(String octal) {

        boolean isOctal = false;

        if (octal != null && !octal.isEmpty()) {
            int number = Integer.parseInt(octal);

            while (number > 0) {
                if (number % 10 <= 7) {
                    isOctal = true;
                } else {
                    isOctal = false;
                    break;
                }
                number /= 10;
            }
        }
        return isOctal;
    }
}

Output :

Enter Number :  20
isOctal : true
Enter Number :  18
isOctal : false

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment