Our class assignment is to add, subtract, multiply, and divide two huge ints i'm currently working on my multiply function and my multiply isn't working and i cannot pin point the problem.

When i input 3*3 it inputs 9 but when you input 4*4 it outputs 6 (the carryover function doesn't seem to work to the tens place)

This is also apparent in the function 13*13=169 but 14*14 prints 186 (carryover error)?

the multiply function goes through how multiplication works eg. 49*49= 40*40+40*9+9*9 and it multiplies then adds and the power function denotes the place holder eg. if power is 39 that means the value is 4 *10^39.

//Computer Science period 2
//WVHS
import java.util.*;
public class HugeInteger {
   static int max=40;
    static Scanner in = new Scanner(System.in);
    static int multiplyend[]=new int[80];
    public static void main(String[] args)
    {
        int chooser=0;
        int array1[]=new int[max];
        int array2[]=new int[max];
        int dummy[]=new int[max];
        int multiplydummy[]=new int[max*2];
        int multiplydummy2[]=new int[max*2];
        int switcher[]=new int[max*2];
        for(int i=0; i<max*2; i++)
        {
        	multiplydummy[i]=0;
        	multiplydummy2[i]=0;
        	switcher[i]=0;
        }
        setnumber(array1);
        setnumber(array2);

        while(chooser<=11)
            {
            switch(chooser)
            {
            case 1:
                {
                    add(array1,array2,dummy);
                    System.out.print("array1+array2= ");
                   printnumber(dummy);

               break;
                }
            case 2:
                {
                       subtract(array1,array2,dummy);
                       System.out.print("array1-array2= ");
                       printnumber(dummy);
               break;
                }
            case 3:
                {
                    multiply(array1, array2, multiplydummy, multiplydummy2);
                    System.out.print("array1*array2 = ");
                    printnumbermultiply();
                    break;
                }
                case 4:
                    {

                   break;
                    }
                case 5:
                    {
                      System.out.println("Is array1 equal to array2?"+isEqualTo(array1,array2));
                   break;
                    }
                case 6:
                    {
                    System.out.println("Is array1 not equal to array2?"+ isNotEqualTo(array1,array2));
                   break;
                    }
                case 7:
                    {
                System.out.println("Is array1 greater then array2? "+isGreaterThan(array1,array2));
                   break;
                    }
                case 8:
                    {
                    System.out.println("is array1 smaller than array2 "+isLessThan(array1, array2));
                   break;
                    }
                case 9:
                    {
   System.out.println("array1 greater than or equal to array2 "+isGreaterThanOrEqualTo(array1,array2));
                   break;
                    }
                case 10:
                    {
         System.out.println("array1 less than or equal to array2 "+isLessThanOrEqualTo(array1,array2));
                   break;
                    }
                case 11:
                {
                    System.out.println("is array1 zero? " + iszero(array1));
                    System.out.println("is array2 zero? "+ iszero(array2));
                    break;
                }

            }


            chooser = menu();
            }
    }
    public static void setnumber(int a[])

    {
        String transfer;
        System.out.println("Please input numbers");
        transfer=in.nextLine();
        for(int g=transfer.length()-1; g>=0; g--)
            {
            a[max-1]=(transfer.charAt(g)-48);
            max--;
            }
        max=40;
        return;
    }
    public static boolean iszero(int b[])
    {
        int a=0;
         for(int i=0;i<max;i++)
                 a=a+b[i];
                         if(a==0)
                       return true;
         return false;
    }
    private static int menu()
    {
        Scanner input = new Scanner( System.in );
        System.out.println( "1. Add" );
        System.out.println( "2. Subtract" );
        System.out.println( "3. Multiply" );
        System.out.println( "4. Divide" );
        System.out.println( "5. Are they Equal?" );
        System.out.println( "6. Are they Not Equal?" );
        System.out.println( "7. Are they Greater Than?" );
        System.out.println( "8. Are They Less Than?" );
        System.out.println( "9. Are they Greater Than or Equal To?" );
        System.out.println( "10. Are they Less Then or Equal To?" );
        System.out.println( "11. Are any of them zero?");
        System.out.println( "12. Exit" );
        System.out.print( "Choice: " );

        return input.nextInt();
    }
    public static void add(int a[], int b[], int c[])
    {

             int carryover=0,sum,i;
              for(i=max-1;i>=0;i--)
                   {
                          sum=a[i]+b[i]+carryover;
                          c[i]=sum%10;
                          carryover=sum/10;
                     }
              
               return;

    }
    public static void printnumber(int a[])
 {

        int start,i=0;
       
       while(i<max&&a[i]==0)
       {
               i++;

       }
           start=i;
     for(int k=start; k<max;k++)
     {
         System.out.print(a[k]);
     }
     System.out.println();
}
    public static int getnumbers(int a[])
    {
        int i;

        i=max-1;
        while(i>0&&a[i]==0)
          i--;
           return i;
    }
    public static void subtract(int a[],int b[],int c[])
    {int dummy=1;
     if(isLessThan(a,b))
          {
           dummy=-1;
           sub(b,a,c,dummy);
            }
      else
          sub(a,b,c,dummy);
      return;
      }
    public static void sub(int a[],int b[],int c[],int dummy)
    {
    int d[]=new int[max];
    int i,j;
    for(i=0;i<max;i++)
        {
        d[i] = a[i];
        c[i]=0;
          }
     for(i=0;i<max;i++)
          {
         if(d[i]>=b[i])
                c[i]=d[i]-b[i];
          else
            {
              j=i-1;
               d[j]--;
               d[i]+=10;
               i--;                  
             }
           }
        i=max-1;
            while(i>0&&a[i]==0)
                i--;
       
      return;
    }
    public static boolean isGreaterThanOrEqualTo(int a[],int b[])
         {if(isGreaterThan(a,b)||isEqualTo(a,b))
               return true;
           else
               return false;
           }
    public static boolean isLessThanOrEqualTo(int a[],int b[])
         {if(isLessThan(a,b)||isEqualTo(a,b))
               return true;
           else
               return false;
           }
    public static boolean isLessThan(int a[],int b[])
          {int beginningofa,bstart,i;
          beginningofa=getnumbers(a);
              bstart=getnumbers(b);

              if(beginningofa>bstart)
                    return false;
           if(beginningofa<bstart)
                     return true;
              for(i=beginningofa;i>=0;i--)
                 if(a[i]>b[i])
                      return false;
           return true;

            }
    public static boolean isGreaterThan(int a[],int b[])
          {int astart,bstart,i;

          astart=getnumbers(a);
           bstart=getnumbers(b);

         if(astart<bstart)    //a has less digits than b so can't be
                 return false;
       if(astart>bstart)
                  return true;   //a has more digits than b so has to be
           for(i=astart;i>=0;i--)
              if(a[i]<b[i])
                   return false;

        return true;
           }
    public static boolean isEqualTo(int a[],int b[])
     {
          for(int i=0;i<max;i++)
               if(a[i]!=b[i])
                    return false;
      return true;
     }
    public static boolean isNotEqualTo(int a[],int b[])
     {
          for(int i=0;i<max;i++)
               if(a[i]!=b[i])
                    return true;
      return false;
     }
    public static void multiply(int a[], int b[], int adder1[], int adder2[])
    {
    	int p=39, o=39, power, sum, carryover=0;
    	
    	for(p=39; p>=0; p--)
    	{
    		
    		for(o=39; o>=0; o--)
    		{
    			int x=a[p]*b[o];
    			power=p+o;
    			adder1[power+1]=x/10;
    			adder1[power]=x%10;
    				for(int k=79; k>=0; k--)
    				{
    					sum=adder1[k]+adder2[k]+carryover;
    					multiplyend[k]=sum%10;
    					carryover=sum/10;
    				}
    				for(int l=79; l>=0; l--)
    				{
    					adder1[l]=0;
    				}
    				adder2=multiplyend;
    		}
    		
    	}
    }
    public static void printnumbermultiply()
    {

           int start,i=0;
          
          while(i<max*2&&multiplyend[i]==0)
          {
                  i++;

          }
              start=i;
        for(int k=start; k<(max*2)-1;k++)
        {
            System.out.print(multiplyend[k]);
        }
        System.out.println();
   }
}

Could you preload the Scanner input by changing this line to have valid input.

static Scanner in = new Scanner("4 4\n"); //System.in);

For debug testing it is easier if the program doesn't have to ask for the input.
The correct input is provided every time without user intervention.

Another thing to improve the code: Use a variable to define the size of the arrays.
Do not hard code 80 and 79 and 39 all over the program.

Put these statements at the beginning of the class:

final static int NbrDigits = 10;          //<<<<<<<<<Set small for testing
    inal static int max = NbrDigits/2;    // What is max used for??? 
    static Scanner in = new Scanner("4\n4\n"); //System.in);
    static int multiplyend[] = new int[NbrDigits];

Use the length of the arrays to control for loops, not a hard coded number:
for(int l=79; l>=0; l--) //<<<<<<< Poor technique use array.length-1 here

commented: not helpful at all +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.