0% found this document useful (0 votes)
8 views3 pages

Java Basic Programs

The document contains three Java programs that demonstrate different methods of adding two numbers. Program 1 uses user input to read two integers and display their sum, Program 2 takes two integers as parameters and calculates their sum, while Program 3 directly initializes two integers and prints their sum. Each program showcases a different approach to performing addition in Java.

Uploaded by

mpari312004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Basic Programs

The document contains three Java programs that demonstrate different methods of adding two numbers. Program 1 uses user input to read two integers and display their sum, Program 2 takes two integers as parameters and calculates their sum, while Program 3 directly initializes two integers and prints their sum. Each program showcases a different approach to performing addition in Java.

Uploaded by

mpari312004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM 1:

import [Link]; // Import Scanner class

// Class that handles input and addition


class Adder {
void add() {
Scanner sc = new Scanner([Link]); // Create Scanner object

[Link]("Enter first number: ");


int a = [Link](); // Read first number

[Link]("Enter second number: ");


int b = [Link](); // Read second number

int sum = a + b; // Addition

[Link]("The sum is: " + sum);

[Link](); // Close scanner


}
}

// Main class
class AddNumbers1 {
public static void main(String[] args) {
// Create object of Adder using 'new'
Adder obj = new Adder();
// Call add() method (which handles input + sum)
[Link]();
}
}
OUTPUT:

PROGRAM 2:
class Adder {
void add(int x, int y) {
int sum = x + y; // calculate sum
[Link]("The sum is: " + sum);
}
}

// Main class
class AddNumbers2 {
public static void main(String[] args) {
Adder obj = new Adder(); // create object of Adder class

int a = 15;
int b = 25;
[Link](a, b); // call add() method using object
}
}
OUTPUT:

PROGRAM 3:

class Add1{
public static void main(String[] args) {
int a = 10; // first number
int b = 20; // second number
int sum = a + b; // addition

[Link]("The sum is: " + sum);


}
}

OUTPUT:

You might also like