Advertisements
Home > Java > Java Program to find factorial of a given number with recursion – Q3

Java Program to find factorial of a given number with recursion – Q3


Q3: Java Program to find factorial of a given number with recursion

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

For example:
5! =     5*4*3*2*1 = 120
6! = 6*5*4*3*2*1 = 720

class fact_rec{
	public static void main(String args[]){
		int num;
		long fac = 1;
		num = Integer.parseInt(args[0]);
		fac = factorial(num);
		System.out.println("Factorial of " + num + " is: " + fac);
	}
	public static long factorial(int num){
		if(num<=1)
			return 1;
		else
			return (num * factorial(num-1));
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: