Adding two number using Class and Object.
Adding two number without taking user input.
package com.ritesh;
class sum
{
public int add2(int a,int b)
{
return a+b;
}
}
public class add {
public static void main(String[] args) {
sum sc=new sum();
System.out.println(sc.add2(10,12));
}
}
Adding two number by taking user input.
package com.ritesh;
import java.util.Scanner;
class sum
{
public long add2(long a,long b)
{
return a+b;
}
}
public class add {
public static void main(String[] args) {
sum sc=new sum();
Scanner add=new Scanner(System.in);
System.out.println("Enter first number"); //taking the first number
long num1=add.nextLong();
System.out.println("Enter the second number"); //taking the second number
long num2=add.nextLong();
System.out.println("The sum of "+num1+" and "+num2+" is "+sc.add2(num1,num2));
}
}
Comments
Post a Comment