-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStaticMethod.java
39 lines (35 loc) · 1009 Bytes
/
StaticMethod.java
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
//Demonstarting static method in Java.
//Program to find factorial of any number
import java.lang.*;
import java.io.*;
class StaticMethod
{
public static void main(String args[])
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number :- ");
int n = Integer.parseInt(br.readLine());
System.out.println("You Entered : " + n + "Factorial is : " + StaticMethod.facto(n)); //Static method belongs to class, so need to access it by class reference
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
public static int facto(int n) //Static Method named as facto
{
int f = 1;
while(n > 0)
{
f *= n; //Shorthand for f = f * n
n--;
}
return f;
}
}