//BigMultiply.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BigMultiply implements ActionListener
{
JFrame frame;
JTextField num1,num2;
JButton multiply;
JLabel intro,first,second;
BigMultiply()
{
frame=new JFrame("BigMultiply ~~~By:Milan~~~");
num1=new JTextField();
num2=new JTextField();
multiply=new JButton("Multiply");
intro=new JLabel("Enter the numbers (upto 300 digits) to multiply");
first=new JLabel("First Number:");
second=new JLabel("Second Number:");
frame.setLayout(null);
intro.setBounds(5,5,300,40);
first.setBounds(5,50,100,40);
num1.setBounds(5,100,300,40);
second.setBounds(5,150,100,40);
num2.setBounds(5,200,300,40);
multiply.setBounds(120,300,80,40);
frame.add(intro);
frame.add(num1);
frame.add(num2);
frame.add(first);
frame.add(second);
frame.add(multiply);
frame.setSize(350,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(300,200);
frame.setResizable(false);
multiply.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String firstnumber=num1.getText();
String secondnumber=num2.getText();
if(firstnumber.equals("") || secondnumber.equals(""))
JOptionPane.showMessageDialog(null,"Enter the both values","Error",JOptionPane.ERROR_MESSAGE);
else
{
if(firstnumber.indexOf(".")!=-1 || secondnumber.indexOf(".")!=-1)
JOptionPane.showMessageDialog(null,"Both values must be Integers","Error",JOptionPane.ERROR_MESSAGE);
else
{
String result=MyMultiplication.getmultiply(firstnumber,secondnumber);
JOptionPane.showMessageDialog(null,"Result is:\n"+result,"Result",JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String... s)
{
new BigMultiply();
}
}
//MyMultiplication.java
import java.util.*;
class MyMultiplication
{
public static void main(String... s1)
{
//System.out.println(getmultiply("98657896541236598745632145698745632","9865231223654588965478965236544123655"));
System.out.println(getmultiply("2","10"));
//getmultiply("99","4");
}
public static String getmultiply(String s1,String s2)
{
int carry=0;
int len1=s1.length();
int len2=s2.length();
int count=-1;
String result[]=new String[1000]; //for 1000 digits u can take more
for(int l=0;l
result[l]="";
}
for(int i=len2-1;i>=0;i--)
{
carry=0;
count++;
int y=Integer.parseInt(""+s2.charAt(i));
for(int j=len1-1;j>=0;j--)
{
int x=Integer.parseInt(""+s1.charAt(j));
if(j==0)
{
result[i]=""+(x*y+carry)+result[i];
}
else
{
result[i]=""+(getresult(x*y+carry))+result[i];
}
carry=getcarry(x*y+carry);
}
result[i]=appendforcross(result[i],count); //count for padding zero at right for cross symbols in multiplication
}
//show(result,len2);
int maxlen=findmaxlen(result,len2); //needed for masking the string to a length
//System.out.println("maximum length is"+maxlen);
String finalstring[]=mask(result,maxlen); //masking the strings so that they have same number of char/digit
//show(finalstring,len2); //show the strings
String go=finalsum(result,maxlen,len2); //find the final sum
//System.out.println("the result is :"+go); //showing the result
return go;
}//end function
public static int getcarry(int mul) ///return the carry.......if mul=9 will return 0,if mul=34 will return 3,if mul=123 will return 12 and so on.....
{
String x=""+mul;
int temp1=0;
if(x.length()==1) //if length is one then no carry that is cary is 0
{
temp1=0;
}
else
{
temp1=Integer.parseInt(x.substring(0,x.length()-1));
}
return temp1;
}
public static int getresult(int s) //return the lastdigit from the sum
{
String p=""+s;
int temp=0;
temp=Integer.parseInt(""+p.charAt(p.length()-1));
return temp;
}
public static String appendforcross(String result,int count) //count tell us a which level we are and how much
{ //zeros we have to append at the end
for(int i=0;i
return result;
}
public static void show(String[] str,int len)
{
for(int i=0;i
}
public static int findmaxlen(String result[],int len) //return the maximum length so that all string can be of
{ //same ength after masking
int max=result[0].length();
for(int i=0;i
int templen=result[i].length();
if(templen>max)
max=templen;
}
return max;
}
public static String[] mask(String result[],int maxlen) //mask the strings so that they all have same max langth
{ //append/pad zeros at the left side
for(int i=0;i
int tmp=maxlen-result[i].length();
for(int j=0;j
}
return result;
}
public static String finalsum(String result[],int maxlen,int len2)
{
int tempsum=0;
String get="";
int carry=0;
for(int i=maxlen-1;i>=0;i--)
{
tempsum=0;
for(int j=0;j
tempsum=Integer.parseInt(""+result[j].charAt(i))+tempsum;
}
tempsum+=carry;
if(i==0)
{
get=""+tempsum+get;
}
else
{
get=""+getresult(tempsum)+get;
}
carry=getcarry(tempsum);
}
return get;
}
}//end class
Download
hey why are you wasting your money on advertise your blog. Join Us for free promotion and get more than 1lac visitor daily in just free. http://bit.ly/pg8sXp
ReplyDelete