//Author ramraj.vasavi
package com.rrv;
import java.util.Scanner;
class Add
{
public static void main(String[] args)
{
Scanner scannerInput=new Scanner(System.in);
System.out.println("Enter your First Number");
String first=scannerInput.next();
System.out.println("Enter your Second Number");
String second=scannerInput.next();
int[] num1=convertInt(first);
int[] num2=convertInt(second);
int fLength=num1.length;
int sLength=num2.length;
if(first.equals("0")&&second.equals("0"))
{
System.out.println("Addition of two Numbers is: "+"0");
}
else if(first.equals("0"))
{
System.out.println("Addition of two Numbers is: "+second);
}
else if(second.equals("0"))
{
System.out.println("Addition of two Numbers is: "+first);
}
else{
if(fLength>sLength)
Add.add(num1,num2);
else
Add.add(num2,num1);
}
}
public static void add(int[] oparend1,int[] oparend2)
{
int[] result=new int[oparend1.length];
int op2Length=oparend2.length-1;
int op1Length=oparend1.length-1;
int sum=0;
for(int index=0;index<=op1Length;index++)
{
if(index>=oparend2.length)
sum=oparend1[op1Length-index]+sum;
else
sum+=oparend1[op1Length-index]+oparend2[op2Length-index];
int digit=sum%10;
sum=sum/10;
result[index]=digit;
}
String output="";
for(int index=result.length-1;index>=0;index--)
{
output=output+result[index];
}
System.out.println("Addition of two numbers is: "+output);
}
public static int[] convertInt(String str){
int[] arr=new int[str.length()];
for(int index=0;index<str.length();index++)
{
arr[index]=(int)str.charAt(index)-48;
}
return arr;
}
}
Output of Addition.java:
Enter your First number: 54832415789132118
Enter your Second Number: 9213849898
Addition of Two numbers is: 54832425002982016
package com.rrv;
import java.util.Scanner;
class Add
{
public static void main(String[] args)
{
Scanner scannerInput=new Scanner(System.in);
System.out.println("Enter your First Number");
String first=scannerInput.next();
System.out.println("Enter your Second Number");
String second=scannerInput.next();
int[] num1=convertInt(first);
int[] num2=convertInt(second);
int fLength=num1.length;
int sLength=num2.length;
if(first.equals("0")&&second.equals("0"))
{
System.out.println("Addition of two Numbers is: "+"0");
}
else if(first.equals("0"))
{
System.out.println("Addition of two Numbers is: "+second);
}
else if(second.equals("0"))
{
System.out.println("Addition of two Numbers is: "+first);
}
else{
if(fLength>sLength)
Add.add(num1,num2);
else
Add.add(num2,num1);
}
}
public static void add(int[] oparend1,int[] oparend2)
{
int[] result=new int[oparend1.length];
int op2Length=oparend2.length-1;
int op1Length=oparend1.length-1;
int sum=0;
for(int index=0;index<=op1Length;index++)
{
if(index>=oparend2.length)
sum=oparend1[op1Length-index]+sum;
else
sum+=oparend1[op1Length-index]+oparend2[op2Length-index];
int digit=sum%10;
sum=sum/10;
result[index]=digit;
}
String output="";
for(int index=result.length-1;index>=0;index--)
{
output=output+result[index];
}
System.out.println("Addition of two numbers is: "+output);
}
public static int[] convertInt(String str){
int[] arr=new int[str.length()];
for(int index=0;index<str.length();index++)
{
arr[index]=(int)str.charAt(index)-48;
}
return arr;
}
}
Output of Addition.java:
Enter your First number: 54832415789132118
Enter your Second Number: 9213849898
Addition of Two numbers is: 54832425002982016