Write a simple factorial program in java
class Test{
int number;
void set(int num){
number=num;
}
int factorial(){
int f=1;
for(int i=1; i<=number; i++){
f=f*i;
}
return f;
}
}
class Main{
public static void main(String args[]){
Test obj=new Test();
int num=5;
obj.set(num);
int f=obj.factorial();
System.out.println("The factorial is "+f);
}
}
0 Comments