Problem Statement:
Create a class HugeInteger which uses a 40 element array of digits to store integers as
large as 40 digits each. Provide methods input, output, add , subtract, isEqualTo, and isLessThan to perform operations on the HugeInteger objects.
Input and Output Requirements:
Program should read two large integer values and perform operations addition, subtraction and comparison on these objects.
Algorithm:
Begin
Define a class HugeInteger with variable h which is an integer array and str a string variable.
Define method input() which reads a string and convert the value to integer and strore it in integer array h.
Define method add() to add two huge integer values.
Define method sub() to find the difference between the two values.
Define the method EqualTo() which returns true if two objects are equal, otherwise false.
Define the method isLessThan() which returns true if the first object is less than the second, otherwise false.
End
Program:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class HugeInteger { int[] number = new int[40]; int j; int size; char[] s = new char[40]; String str; public HugeInteger() { for(int i = 0 ; i < 39; i++) number[i] = 0; } void input() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a Huge Integer : "); str = in.readLine(); size = j = str.length(); for(int i = 0; i < size ; i++) { s[i] = str.charAt(i); number[39-j] = s[i]-'0'; j--; } } void output() { for(int i = 39; i > 0 ; i--) System.out.print(number[39-i]); } public void add(HugeInteger H1, HugeInteger H2) { for(int j = 0 ; j < 39; j++) { number[39-j] = number[39-j] + H1.number[39-j] + H2.number[39-j]; number[39-j-1] = number[39-j] / 10; number[39-j] = number[39-j] % 10; } } public void subtract(HugeInteger H1, HugeInteger H2) { HugeInteger H3 = new HugeInteger(); HugeInteger n; int no; if(H1.isLessThan(H1, H2)) { HugeInteger temp = new HugeInteger(); temp = H1; H1 = H2; H2 = temp; } for(int j = 0 ; j < 39; j++) { if(H1.number[39-j] < H2.number[39-j]) { int incr = H1.number[39-j] + 10; no = incr - H2.number[39-j]; H1.number[39-j-1] = H1.number[39-j-1] - 1; } else { no = H1.number[39-j] - H2.number[39-j]; } H3.number[39-j] = no; } H3.output(); } public boolean isLessThan(HugeInteger H1, HugeInteger H2) { boolean less = false; for(int i = 0 ; i < 39 ; i++) { if(H1.number[39-i] < H2.number[39-i]) less = true; } return less; } public boolean isEqualTo(HugeInteger H1, HugeInteger H2) { boolean same = true; for(int i = 0 ; i < 39 ; i++) { if(H1.number[39-i] != H2.number[39-i]) same = false; } return same; } } public class Huge { public static void main(String[] args) throws IOException { HugeInteger H1 = new HugeInteger(); HugeInteger H2 = new HugeInteger(); HugeInteger H3 = new HugeInteger(); H1.input(); H2.input(); System.out.println("\nAdding....."); H3.add(H1,H2); System.out.print("\nSum = "); H3.output(); boolean L = H3.isLessThan(H1, H2); if(L) System.out.println("\nThe first HugeInteger is Smaller"); else System.out.println("\nThe first HugeInteger is Greater"); L = H3.isEqualTo(H1, H2); if(!L) System.out.println("\nTwo Numbers are not equal!!!"); else System.out.println("\nTwo Numbers are equal!!!"); System.out.println("\nSubtracting....."); System.out.print("\nDifference = "); H3.subtract(H1, H2); } } |
Output:
Enter a Huge Integer : 5451541512156122
Enter a Huge Integer : 48465174231121524444
Adding…..
Sum = 000000000000000000048470625772633680566
The first HugeInteger is Smaller
Two Numbers are not equal!!!
Subtracting…..
Difference = 000000000000000000048459722689609368322
Enter a Huge Integer : 4576234831256452102451
Enter a Huge Integer : 4576234831256452102451
Adding…..
Sum = 000000000000000009152469662512904204902
The first HugeInteger is Greater
Two Numbers are equal!!!
Subtracting…..
Difference = 000000000000000000000000000000000000000
Leave a Reply