-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethods.java
More file actions
60 lines (55 loc) · 1.42 KB
/
methods.java
File metadata and controls
60 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package methods;
import java.util.Scanner;
public class methods {
public static void main(String[] args) {
methods m1 = new methods();
// non static method call(with object)
m1.welcomeMethod();
int num;
System.out.println("enter the number to calculate factorial: ");
Scanner s = new Scanner(System.in);
num = s.nextInt();
// static method call(no object required)
long fact = calcFactorial(num);
System.out.println("The factorial is: " + fact + "\n");
// non static method returning array( calculate factors)
int n;
System.out.println("enter the number to find factors: ");
n = s.nextInt();
int[] factors = new int[n];
factors = m1.calcFactor(n);
for (int i = 0; i < factors.length; ++i) {
if (factors[i] != 0) {
System.out.print(factors[i] + "\t");
}
}
System.out.println();
s.close();
}
// non parameterized method without returning value
void welcomeMethod() {
System.out.println("hello! welcome to the methods example program!");
System.out.println("a program by Subhakanta Roul");
}
// Parameterized method with returning value
static long calcFactorial(int m) {
long factCount = 1;
while (m > 0) {
factCount *= m;
m--;
}
return factCount;
}
// function returning multiple values
int[] calcFactor(int n) {
int j = 0;
int[] fact = new int[n];
for (int i = 1; i <= (n / 2); ++i) {
if (n % i == 0) {
fact[j] = i;
j++;
}
}
return fact;
}
}