/* This Java program uses 2 functions : main and fact. The main
* function accepts a number from the user, calls the
* fact function and prints the value(the answer) which it
* recieves from the fact function.
*
* The fact function at first reciaves a number from the
* main function and the keeps calling itself with a number,
* 1 less then the number it first recieved.
*/
/*Output wndow :-
Enter a number : 10
The factorial of 10 = 3628800.0
/*
import java.util.*;
class factorialRecursion
{
public static void main()
{
System.out.print("Enter a number : ");
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
double ans=fact(n);
System.out.println("The factorial of "+n+" = "+ans);
}
public static double fact(int num)
{
if(num==1)
{
return(1);
}
else
{
return(num*(fact(num-1))); //Calling itself again with a different value.
}
}
}
//Author : Mayank Rajoria
* function accepts a number from the user, calls the
* fact function and prints the value(the answer) which it
* recieves from the fact function.
*
* The fact function at first reciaves a number from the
* main function and the keeps calling itself with a number,
* 1 less then the number it first recieved.
*/
/*Output wndow :-
Enter a number : 10
The factorial of 10 = 3628800.0
/*
import java.util.*;
class factorialRecursion
{
public static void main()
{
System.out.print("Enter a number : ");
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
double ans=fact(n);
System.out.println("The factorial of "+n+" = "+ans);
}
public static double fact(int num)
{
if(num==1)
{
return(1);
}
else
{
return(num*(fact(num-1))); //Calling itself again with a different value.
}
}
}
//Author : Mayank Rajoria
No comments:
Post a Comment