How do I call a function of the same class in java without using an object?
I tried this but got an error:
'non-static method facti(int) cannot be referenced from a static context' System.out.print(facti(number));
public class Facto {
int i, fact =1;
int facti(int num){
if(num == 0){
System.out.print("For Zero ");
return 1;
}
else
for (i = 1; i <= num; ++i)
{
fact = fact * i;
}
return fact;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number for factorial : ");
int number = sc.nextInt();
Facto f1 = new Facto();
System.out.println(f1.facti(number));
}
}