Java Program to Add Two Octal Numbers

In this post, we will learn How to Add Two Octal Numbers in Java Programming Language. Let’s understand Octal Numbers and Octal Number Addition, then we will code the Java Program to Add Two Octal Numbers.

What is a Octal Number ?

An Octal Number System is a number system with a base of 8. This system contains numbers from 0 to 7.
e.g. 101 is an octal representation of 5

Now, Let’s learn about Octal Addition and its rules.

Octal Number Addition

In Octal Number System, the range is from 0 to 7. So, in Octal addition, if the sum of the digits exceeds 7, then it starts repeating itself from 0 with a carry of 1.

For Example,
2348 + 4568 = 7228

Now, let’s apply the rules of Octal addition and code the Java Program to Add Two Octal Numbers.

Java Program to Add Two Octal Numbers

import java.util.*;

public class Main{
    
    //Method to add two Octal numbers
    public static int addOctal(int n1, int n2){
        
        int sum = 0;
        int carry = 0;
        int powerOfTen = 1; // 10^0
        
        while(n1 > 0 || n2 > 0 || carry > 0){
            
            int digit1 = n1 % 10;
            n1 = n1 / 10;
            
            int digit2 = n2 % 10;
            n2 = n2 / 10;
            
            int digit = digit1 + digit2 + carry;
            
            carry = digit / 8;
            digit = digit % 8;
            
            sum = sum + digit * powerOfTen;
            
            powerOfTen = powerOfTen * 10;
        }
        
        return sum;
    }
    
    //main method
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter 1st Octal number: ");
        int n1 = sc.nextInt();
        
        System.out.println("Enter 2nd Octal number: ");
        int n2 = sc.nextInt();
        
        int sum = addOctal(n1,n2);
        
        System.out.println("Sum of Octal numbers : "+sum);
        
    }
    
}

Output

Enter 1st Octal number: 234

Enter 2nd Octal number: 456

Sum of Octal numbers : 712

How Does This Program Work ?

In this program, we have taken the input of the two octal numbers from the user using the Scanner Class in Java. Then, we have created a method addOctal(int n1, int n2) which takes two parameters i.e. octal number 1 & octal number 2 and returns the sum of both numbers.

    //Method to add two Octal numbers
    public static int addOctal(int n1, int n2){
        
        int sum = 0;
        int carry = 0;
        int powerOfTen = 1; // 10^0
        
        while(n1 > 0 || n2 > 0 || carry > 0){
            
            int digit1 = n1 % 10;
            n1 = n1 / 10;
            
            int digit2 = n2 % 10;
            n2 = n2 / 10;
            
            int digit = digit1 + digit2 + carry;
            
            carry = digit / 8;
            digit = digit % 8;
            
            sum = sum + digit * powerOfTen;
            
            powerOfTen = powerOfTen * 10;
        }
        
        return sum;
    }

In this program, we have used the rules of Octal Number Addition and made the logic of octal addition in Java. We are adding each digit one by one and also maintaining the carry.

        int sum = addOctal(n1,n2);
        
        System.out.println("Sum of Octal numbers : "+sum);

Then, we received the sum returned by the addOctal function and display the sum. This is the Java Program to Add Two Octal Numbers.

Conclusion

I hope after going through this post, you understand how to code Java Program to Add Two Octal Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *