Subtract two octal numbers directly without converting to decimal

771 views Asked by At

How can a subtraction be done?

Deciding on the stopping condition - you want your while loop to continue until both numbers a, b, and carry turn zero. In other words, the condition should be a || b || carry

Adding the next digit to the sum - since the result is coded as decimal, you need to multiply digit d by the next consecutive power of ten. A simple way of doing that would be adding a new variable m which starts at 1 and gets multiplied by ten each iteration.

Can somebody please help make subtraction:

static int addition(int num1, int num2)
        {
            int sum = 0, digit = 0, carry = 0, digit_rank = 1;

            // Calculate the sum
            while (num1 > 0 || num2 > 0 || carry > 0)
            {
                // Calculate the digit
                digit = num1 % 10 + num2 % 10 + carry;

                // Determine if you should carry or not
                if (digit > 7)
                {
                    carry = 1;
                    digit %= 8;
                }
                else
                    carry = 0;

                // Add the digit at the beggining of the sum
                sum += digit * digit_rank;
                digit_rank *= 10;

                // Get rid of the digits of a and b we used
                num1 /= 10;
                num2 /= 10;
            }
            return sum;
        }
3

There are 3 answers

2
tuu On
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
 
int main(int argc, char ** argv)
{
    int a[4]={5,6,0,0}, b[4]={3,4,5,7}, c[4]={0}; 
    int point=0; 
    for(int i=3;i>-1;i--) 
    {
        if(a[i]==0)
        {
            for(int j=3;j>-1;j--)   
            {
                if(a[j]!=0 && point==0) 
                {
                    a[j]=a[j]-1; 
                    point=1; 
                    c[i]=8-b[i]; 
                    break; 
                }
                c[i]=7-b[i];
            }
        }
        else
        {
            c[i]=a[i]-b[i]; 
        }
    }
    for(int i=0;i<4;i++)
    {
        cout<<c[i];
    }
}
0
vaishnavi mendre On
#include <stdio.h>
#include <stdlib.h>

int main()
{
     int a, b ,c;
     printf("Enter 1st number: ");
     scanf("%o",&a);
     printf("Enter 2nd number: ");
     scanf("%o",&b);
     c=a-b;
     printf("octal subtraction=%o", c);
}
0
Ahmad Saleh On

Easiest Way Using 8 Complement :

  1. Make Two Numbers With Same Length(Add Leading Zeros For Shortest Number)
  2. Get 8 Complement For Second Number
  3. Addition First Number And (8 Complement For Second Number)
  4. If Answer Length Is Greater Than Length For (Max Length Between Two Numbers) Then Discard First Digit (Thats Meaning Answer Of Subtraction Is Positive)
    Otherwise Find Again 8 Complement For Answer (Answer In This Case Is Negative)

About Complements And Addition , Subtraction Operations [https://www.youtube.com/watch?v=M0mx8S05v60&list=PLBlnK6fEyqRjMH3mWf6kwqiTbT798eAOm][Neso Academy]