Thursday 10 November 2011

Java program to multiply two numbers upto 100000 places

Here's I'd created a Java program by which you can be able to multiply any two integer numbers upto 100000 places.
e.g., if you had to multiply
    99999999999999999999999999999999999999999999999
X  99999999999999999999999999999999999999999999999
--------------------------------------------------------------------------------------------------
*******What will be the output of your program??????*******
--------------------------------------------------------------------------------------------------



Here's my program:




import java.util.Scanner;

/**
 *
 * @author Mohammad Faisal
 * B.Tech.(IT)
 *
 */
public class MultiplyUptoAnyPlace {
    public static void main(String...args){
        /*try{
            int t[]=new int[126533629];
            System.out.println("Feel free to calculate upto "+t.length+" places.");
            System.out.println(Runtime.getRuntime().totalMemory());
        }catch(Exception e){}*/
        //System.exit(0);
        System.out.print("Enter first number: ");
        Scanner input=new Scanner(System.in);
        String str=input.next();
        int[] a=new int[str.length()];
        for(int i=0;i<str.length();i++){
            a[i]=Integer.parseInt(str.substring(i, i+1));
        }
        /*for(int i:a){
            System.out.print(i);
        }*/
        System.out.print("Enter second number: ");
        str=input.next();
        int[] b=new int[str.length()];
        for(int i=0;i<str.length();i++){
            b[i]=Integer.parseInt(str.substring(i, i+1));
        }
        /*for(int i:b){
            System.out.print(i);
        }*/
        MultiplyUptoAnyPlace m=new MultiplyUptoAnyPlace();
        m.multiply(a, b);
    }
    public String multiply(int[] a,int[] b){
        int i,j,k,l,carry,remainder,result[][],sum[];
        result=new int[b.length][a.length+b.length];
        sum=new int[a.length+b.length];
        String str="";
        i=0;j=0;k=0;carry=0;remainder=0;
        for(i=b.length-1;i>=0;i--){
            l=a.length+b.length-1-k;
            for(j=a.length-1;j>=0;j--){
                /*for(){
                  
                }*/
                str=((b[i]*a[j])+carry)+"";
                if(str.length()>1){
                    carry=Integer.parseInt(str.substring(0,1));
                    remainder=Integer.parseInt(str.substring(1));
                }else{
                    carry=0;
                    remainder=Integer.parseInt(str.substring(0));
                }
                result[k][l]=remainder;
                l--;
            }
            result[k][l]=carry;
            carry=0;
            /*for(int m=l-1;m>=0;m--){
                result[k][m]=0;
            }*/
            k++;
        }
        for(int c[]:result){
            for(int d:c){
                System.out.print(d);
            }
            System.out.println();
        }
        //carry=0;
        for(j=result[0].length-1;j>=0;j--){
            for(i=0;i<result.length;i++){
                sum[j]+=result[i][j];
            }
            str=sum[j]+carry+"";
            System.out.println("str contains: "+str);
            if(str.length()>1){
                sum[j]=Integer.parseInt(str.substring(str.length()-1));
                System.out.println("if>1 sum[j]: "+sum[j]);
                carry=Integer.parseInt(str.substring(0, str.length()-1));
                System.out.println("if>1 carry is: "+carry);
            }else{
                sum[j]=Integer.parseInt(str.substring(0));
                System.out.println("sum[j]: "+sum[j]);
                carry=0;
                System.out.println("carry is: "+carry);
            }
        }
        str="";
        for(int c:sum){
            System.out.print(c);
            str+=c;
        }
        System.out.println("\nOutput: \n"+str);
        return str;
    }
}

4 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. ye blog par kyun...???
    facebook group par kyun nahi....???

    ReplyDelete
  3. coz its assignment question and your group is private....

    now i'm going to post it on FB... ;)

    ReplyDelete
  4. ha ha...."your grp " kya hota hai...???

    ReplyDelete