Q: 4(a) Write an application that
reads a nonnegative integer from an input dialog and computes and
prints its factorial.
import java.util.Scanner;
public class fact {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number to find Fictorial: ");
int num = input.nextInt();
if(num < 0 )
System.out.println("Negative values not allowed, Enter value greater than 0");
else
System.out.println("Answer: "+factorial(num));
}
public static long factorial(int n)
{
int x;
if(n == 0)
{
return 0;
}
else
{
if(n == 1)
{
return 1;
}
else if(n > 1)
{
return n*factorial(n-1);
}
}
return 0;
}
}
import java.util.Scanner;
public class fact {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number to find Fictorial: ");
int num = input.nextInt();
if(num < 0 )
System.out.println("Negative values not allowed, Enter value greater than 0");
else
System.out.println("Answer: "+factorial(num));
}
public static long factorial(int n)
{
int x;
if(n == 0)
{
return 0;
}
else
{
if(n == 1)
{
return 1;
}
else if(n > 1)
{
return n*factorial(n-1);
}
}
return 0;
}
}
Post a Comment