This code is implemented and provided by Himanshu from Agra University.
By using this code we can multiply two numbers of any digits.
Note : Before using this code please create a Java Project in Eclipse IDE and save this class inside
MultiplicationOfNumbers.java file.
import java.util.Scanner;
public class MultiplicationOfNumbers {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first String in integer format :");
String num1=sc.next();
System.out.print("Enter second String in integer format :");
String num2=sc.next();
// create two objects of string-builder using the data of
// 'num1' and 'num2' then reverse it and convert into string,
// then store it inside string
String n1 = new StringBuilder(num1).reverse().toString();
String n2 = new StringBuilder(num2).reverse().toString();
int[] d = new int[num1.length()+num2.length()];
for(int i=0; i<n1.length(); i++){
for(int j=0; j<n2.length(); j++){
d[i+j] += (n1.charAt(i)-'0') * (n2.charAt(j)-'0');
}
}
// create object of string-builder class (it is a muttable object)
// in this object we will store the intermediate calculations
StringBuilder sb = new StringBuilder();
for(int i=0; i<d.length; i++){
int mod = d[i]%10;
int carry = d[i]/10;
if(i+1<d.length){
d[i+1] += carry;
}
sb.insert(0, mod);
}
while(sb.charAt(0) == '0' && sb.length()> 1){
sb.deleteCharAt(0);
}
// convert the string-builder into string and
// store the result in string
String result = sb.toString();
System.out.println("Result of multiplication is "+result);
} // end of main
} // end of class
Output of Code :
Enter first String in integer format :200
Enter second String in integer format :900
Result of multiplication is 180000
By using this code we can multiply two numbers of any digits.
Note : Before using this code please create a Java Project in Eclipse IDE and save this class inside
MultiplicationOfNumbers.java file.
import java.util.Scanner;
public class MultiplicationOfNumbers {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first String in integer format :");
String num1=sc.next();
System.out.print("Enter second String in integer format :");
String num2=sc.next();
// create two objects of string-builder using the data of
// 'num1' and 'num2' then reverse it and convert into string,
// then store it inside string
String n1 = new StringBuilder(num1).reverse().toString();
String n2 = new StringBuilder(num2).reverse().toString();
int[] d = new int[num1.length()+num2.length()];
for(int i=0; i<n1.length(); i++){
for(int j=0; j<n2.length(); j++){
d[i+j] += (n1.charAt(i)-'0') * (n2.charAt(j)-'0');
}
}
// create object of string-builder class (it is a muttable object)
// in this object we will store the intermediate calculations
StringBuilder sb = new StringBuilder();
for(int i=0; i<d.length; i++){
int mod = d[i]%10;
int carry = d[i]/10;
if(i+1<d.length){
d[i+1] += carry;
}
sb.insert(0, mod);
}
while(sb.charAt(0) == '0' && sb.length()> 1){
sb.deleteCharAt(0);
}
// convert the string-builder into string and
// store the result in string
String result = sb.toString();
System.out.println("Result of multiplication is "+result);
} // end of main
} // end of class
Output of Code :
Enter first String in integer format :200
Enter second String in integer format :900
Result of multiplication is 180000
No comments:
Post a Comment