class Addition{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}}
OUTPUT: 20
Method #2
Import java.util.*;
class Addition{
public static void main(String[] args){
Scanner scan= new Scanner (Syetem.in);
System.out.println("Enter 1st number:");
int a=scan.nextInt() ;
System.out.println("Enter 2nd number:");
int b=scan.nextInt();
int c=a+b;
System.out.println(c);
}}
Method #3
Import java.util.*;
class Addition{
public static void main(String[] args){
int a=10;
int b=10;
System.out.println(a+b);
}}
Method #4 (using methods)
class Addition
{
public static void main(String[] args)
{
int m=12;
int n=10;
int p= Add(m,n);
System.out.println(p);
}
private static int Add(int a, int b) {
int Add=a+b;
return Add;
}
}
Method #5 (using methods and scanner class)
import java.util.*;
public class Addition {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Enter 1st number");
int m=sc.nextInt();
System.out.println("Enter 2nd number");
int n=sc.nextInt();
int p= Add(m,n);
System.out.println(p);
}
private static int Add(int a, int b) {
int Add=a+b;
return Add;
}
}
No comments:
Post a Comment