Java Program to Perform Arithmetic Operations without Data Type

This is a java program to perform arithmetic operations on numbers which are greater than size of the integers.

Here is the source code of the Java Program to Perform Arithmetic Operations on Numbers of Size Greater than that of Int Without Using any Data Type of Size Greater than Int. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. package com.sanfoundry.numerical;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BigNumber
  6. {
  7.     static final int MAXDIGITS = 100; /* maximum length bignum */
  8.     static final int PLUS      = 1;  /* positive sign bit */
  9.     static final int MINUS     = -1; /* negative sign bit */
  10.     char             digits[];       /* represent the number */
  11.     int              signbit;        /* 1 if positive, -1 if negative */
  12.     int              lastdigit;      /* index of high-order digit */
  13.  
  14.     BigNumber()
  15.     {
  16.         digits = new char[MAXDIGITS];
  17.         intToBigNumber(0);
  18.     }
  19.  
  20.     void printBigNumber()
  21.     {
  22.         int i;
  23.         if (signbit == MINUS)
  24.             System.out.printf("- ");
  25.         for (i = lastdigit; i >= 0; i--)
  26.             System.out.printf("%c", '0' + digits[i]);
  27.         System.out.printf("\n");
  28.     }
  29.  
  30.     void intToBigNumber(int s)
  31.     {
  32.         if (s >= 0)
  33.             signbit = PLUS;
  34.         else
  35.             signbit = MINUS;
  36.         for (int i = 0; i < MAXDIGITS; i++)
  37.             digits[i] = (char) 0;
  38.         lastdigit = -1;
  39.         int t = Math.abs(s);
  40.         while (t > 0)
  41.         {
  42.             lastdigit++;
  43.             digits[lastdigit] = (char) (t % 10);
  44.             t /= 10;
  45.         }
  46.         if (s == 0)
  47.             lastdigit = 0;
  48.     }
  49.  
  50.     BigNumber addBigNumber(BigNumber b)
  51.     {
  52.         int carry, i;
  53.         BigNumber c = new BigNumber();
  54.         if (signbit == b.signbit)
  55.             c.signbit = signbit;
  56.         else
  57.         {
  58.             if (signbit == MINUS)
  59.             {
  60.                 signbit = PLUS;
  61.                 c = b.subtractBigNumber(this);
  62.                 signbit = MINUS;
  63.             }
  64.             else
  65.             {
  66.                 b.signbit = PLUS;
  67.                 c = this.subtractBigNumber(b);
  68.                 b.signbit = MINUS;
  69.             }
  70.             return c;
  71.         }
  72.         c.lastdigit = Math.max(lastdigit, b.lastdigit) + 1;
  73.         carry = 0;
  74.         for (i = 0; i <= c.lastdigit; i++)
  75.         {
  76.             c.digits[i] = (char) ((carry + digits[i] + b.digits[i]) % 10);
  77.             carry = (carry + digits[i] + b.digits[i]) / 10;
  78.         }
  79.         c.zeroJustify();
  80.         return c;
  81.     }
  82.  
  83.     BigNumber subtractBigNumber(BigNumber b)
  84.     {
  85.         int borrow, v, i;
  86.         BigNumber c = new BigNumber();
  87.         if (signbit == MINUS || b.signbit == MINUS)
  88.         {
  89.             b.signbit = -b.signbit;
  90.             c = addBigNumber(b);
  91.             b.signbit = -b.signbit;
  92.             return c;
  93.         }
  94.         if (compareBigNumber(b) == PLUS)
  95.         {
  96.             c = b.subtractBigNumber(this);
  97.             c.signbit = MINUS;
  98.             return c;
  99.         }
  100.         c.lastdigit = Math.max(lastdigit, b.lastdigit);
  101.         borrow = 0;
  102.         for (i = 0; i <= c.lastdigit; i++)
  103.         {
  104.             v = digits[i] - borrow - b.digits[i];
  105.             if (digits[i] > 0)
  106.                 borrow = 0;
  107.             if (v < 0)
  108.             {
  109.                 v = v + 10;
  110.                 borrow = 1;
  111.             }
  112.             c.digits[i] = (char) (v % 10);
  113.         }
  114.         c.zeroJustify();
  115.         return c;
  116.     }
  117.  
  118.     int compareBigNumber(BigNumber b)
  119.     {
  120.         int i;
  121.         if (signbit == MINUS && b.signbit == PLUS)
  122.             return PLUS;
  123.         if (signbit == PLUS && b.signbit == MINUS)
  124.             return MINUS;
  125.         if (b.lastdigit > lastdigit)
  126.             return PLUS * signbit;
  127.         if (lastdigit > b.lastdigit)
  128.             return MINUS * signbit;
  129.         for (i = lastdigit; i >= 0; i--)
  130.         {
  131.             if (digits[i] > b.digits[i])
  132.                 return MINUS * signbit;
  133.             if (b.digits[i] > digits[i])
  134.                 return PLUS * signbit;
  135.         }
  136.         return 0;
  137.     }
  138.  
  139.     void zeroJustify()
  140.     {
  141.         while (lastdigit > 0 && digits[lastdigit] == 0)
  142.             lastdigit--;
  143.         if (lastdigit == 0 && digits[0] == 0)
  144.             signbit = PLUS; /* hack to avoid -0 */
  145.     }
  146.  
  147.     void digitShift(int d)
  148.     {
  149.         int i;
  150.         if (lastdigit == 0 && digits[0] == 0)
  151.             return;
  152.         for (i = lastdigit; i >= 0; i--)
  153.             digits[i + d] = digits[i];
  154.         for (i = 0; i < d; i++)
  155.             digits[i] = 0;
  156.         lastdigit += d;
  157.     }
  158.  
  159.     BigNumber multiplyBigNumber(BigNumber b)
  160.     {
  161.         BigNumber row = new BigNumber();
  162.         BigNumber tmp = new BigNumber();
  163.         BigNumber c = new BigNumber();
  164.         int i, j;
  165.         row.signbit = this.signbit;
  166.         row.lastdigit = this.lastdigit;
  167.         System.arraycopy(this.digits, 0, row.digits, 0, this.digits.length);
  168.         for (i = 0; i <= b.lastdigit; i++)
  169.         {
  170.             for (j = 1; j <= b.digits[i]; j++)
  171.             {
  172.                 tmp = c.addBigNumber(row);
  173.                 c = tmp;
  174.             }
  175.             row.digitShift(1);
  176.         }
  177.         c.signbit = signbit * b.signbit;
  178.         c.zeroJustify();
  179.         return c;
  180.     }
  181.  
  182.     BigNumber divideBigNumber(BigNumber b)
  183.     {
  184.         BigNumber row = new BigNumber();
  185.         BigNumber tmp = new BigNumber();
  186.         BigNumber c = new BigNumber();
  187.         int asign, bsign, i;
  188.         c.signbit = signbit * b.signbit;
  189.         asign = signbit;
  190.         bsign = b.signbit;
  191.         signbit = PLUS;
  192.         b.signbit = PLUS;
  193.         c.lastdigit = lastdigit;
  194.         for (i = lastdigit; i >= 0; i--)
  195.         {
  196.             row.digitShift(1);
  197.             row.digits[0] = digits[i];
  198.             c.digits[i] = 0;
  199.             while (row.compareBigNumber(b) != PLUS)
  200.             {
  201.                 c.digits[i]++;
  202.                 tmp = row.subtractBigNumber(b);
  203.                 row = tmp;
  204.             }
  205.         }
  206.         c.zeroJustify();
  207.         signbit = asign;
  208.         b.signbit = bsign;
  209.         return c;
  210.     }
  211. }
  212.  
  213. public class ArithmeticOpsBigNumbers
  214. {
  215.     public static void main(String[] args)
  216.     {
  217.         int a, b;
  218.         BigNumber n1 = new BigNumber();
  219.         BigNumber n2 = new BigNumber();
  220.         BigNumber n3 = new BigNumber();
  221.         BigNumber zero = new BigNumber();
  222.         Scanner sc = new Scanner(System.in);
  223.         while (sc.hasNextInt())
  224.         {
  225.             a = sc.nextInt();
  226.             b = sc.nextInt();
  227.             System.out.printf("a = %d    b = %d\n", a, b);
  228.             n1.intToBigNumber(a);
  229.             n2.intToBigNumber(b);
  230.             n3 = n1.addBigNumber(n2);
  231.             System.out.printf("Addition: ");
  232.             n3.printBigNumber();
  233.             System.out.printf("Comparing Numbers a ? b: %d\n",
  234.                     n1.compareBigNumber(n2));
  235.             n3 = n1.subtractBigNumber(n2);
  236.             System.out.printf("Subtraction: ");
  237.             n3.printBigNumber();
  238.             n3 = n1.multiplyBigNumber(n2);
  239.             System.out.printf("Multiplication: ");
  240.             n3.printBigNumber();
  241.             zero.intToBigNumber(0);
  242.             if (zero.compareBigNumber(n2) == 0)
  243.                 System.out.printf("Division: NaN \n");
  244.             else
  245.             {
  246.                 n3 = n1.divideBigNumber(n2);
  247.                 System.out.printf("Division: ");
  248.                 n3.printBigNumber();
  249.             }
  250.         }
  251.         sc.close();
  252.     }
  253. }

Output:

$ javac ArithmeticOpsBigNumbers.java
$ java ArithmeticOpsBigNumbers
 
12342424
12313423
a = 12342424    b = 12313423
Addition: 24655847
Comparing Numbers a ? b: -1
Subtraction: 29001
Multiplication: 151977487557352
Division: 1

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.