You are on page 1of 5

8.

Create a class HugeInteger that uses a 40-element array of digits to store


integers as large as 40 digits each. Provide member functions input, output, add
and subtract. ForcomparingHugeInteger objects, provide functions isEqualTo,
isLessThan, isGreaterThan, isNotEqual-each of this is a predicate function that
simply return true if relationship holds between two HugeIntegers and returns
false otherwise.create objects of class in main function defined in class
HugeIntegerMain to do all the operations.
Ans:
import java.util.Scanner;
class hugeInteger
{
int arr[]=new int[40];
int n;
Scanner sc=new Scanner(System.in);

public void input()


{
int i;
System.out.println("Enter the number of elements");
n=sc.nextInt();
System.out.println("Enter"+n+" 10 digit numbers");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
}

void output()
{
System.out.println("The Elements Are");
for(int i=0;i<n;i++)
{
System.out.println(arr[i]);
}

void add()
{
int i,j;
int c;
System.out.println("Input the index of 2 numbers you want to
add");
i=sc.nextInt();
j=i;
i=sc.nextInt();
c=arr[i]+arr[j];
System.out.println("Sum is"+c);
}

void subtract()
{
int i,j;
int c;
System.out.println("Input the index of 2 numbers you want to
subtract");
i=sc.nextInt();
j=i;
i=sc.nextInt();
c=arr[j]-arr[i];
System.out.println("Difference is"+c);
}

boolean isEqualTo()
{
int i,j;
int c;

System.out.println("Input the index of 2 numbers you want to


check if it is equal");
i=sc.nextInt();
j=i;
i=sc.nextInt();
if(arr[j]==arr[i])
{
System.out.println("true");
return true;
}
else
{
System.out.println("false");
return false;
}
}
boolean isLessorGreat()
{
int i,j;
int c;
System.out.println("Input the index of 2 numbers you want to
check if 1st value is greater");
i=sc.nextInt();
j=i;
i=sc.nextInt();
if(arr[i]>arr[j])
{
System.out.println("true");
return true;
}
else
{
System.out.println("false");

return false;
}
}

}
class hugeIntegerMain
{
public static void main(String args[])
{
hugeInteger obj=new hugeInteger();
obj.input();
obj.output();
obj.add();
obj.subtract();
obj.isEqualTo();
obj.isLessorGreat();
}
Output:
Enter the number of elements
3
Enter 3 10 digit numbers
500
500
100
Input the index of 2 numbers you want to add
0
2
Sum is 600
Input the index of 2 numbers you want to subtract
0
2
Difference is 400

Input the index of 2 numbers you want to check if it is equal


0
2
false

You might also like