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

Java User Defined Methods Examples

Chapter 5 presents four Java programs demonstrating user-defined methods. The programs cover summing two numbers, calculating the area of a circle, finding the factorial of a number, and checking if a number is prime. Each program illustrates different method types, including those with and without parameters and return types.

Uploaded by

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

Java User Defined Methods Examples

Chapter 5 presents four Java programs demonstrating user-defined methods. The programs cover summing two numbers, calculating the area of a circle, finding the factorial of a number, and checking if a number is prime. Each program illustrates different method types, including those with and without parameters and return types.

Uploaded by

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

Chapter 5 – User Defined Methods (4 Programs)

Program 1: Sum of Two Numbers (Method without Parameters & without


Return Type)
// Program 1: Sum of two numbers using method without parameters and return type
public class SumNoParam {
void sum() {
int a = 10, b = 20;
int s = a + b;
[Link]("Sum = " + s);
}

public static void main(String[] args) {


SumNoParam obj = new SumNoParam();
[Link]();
}
}

/*
Output:
Sum = 30
*/

Program 2: Calculate Area of Circle (Method with Parameters & without Return
Type)
// Program 2: Area of circle using method with parameters and no return type
public class AreaCircle {
void area(double r) {
double a = 3.14 * r * r;
[Link]("Area = " + a);
}

public static void main(String[] args) {


AreaCircle obj = new AreaCircle();
[Link](5.0);
}
}

/*
Output:
Area = 78.5
*/

Program 3: Factorial of a Number (Method with Parameters & with Return


Type)
// Program 3: Factorial using method with parameters and return type
public class Factorial {
int fact(int n) {
int f = 1;
for (int i = 1; i <= n; i++)
f *= i;
return f;
}

public static void main(String[] args) {


Factorial obj = new Factorial();
int result = [Link](5);
[Link]("Factorial = " + result);
}
}

/*
Output:
Factorial = 120
*/

Program 4: Check Prime Number (Method with Return Type & Parameter)
// Program 4: Check if number is prime using method with return type
public class PrimeCheck {
boolean isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return false;
}
return true;
}

public static void main(String[] args) {


PrimeCheck obj = new PrimeCheck();
int num = 13;
if ([Link](num))
[Link](num + " is Prime.");
else
[Link](num + " is NOT Prime.");
}
}

/*
Output:
13 is Prime.
*/

You might also like