The Artima Developer Community
Sponsored Link

Java Buzz Forum
Finding Factorial of a Number in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Finding Factorial of a Number in Java Posted: Aug 13, 2016 9:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Finding Factorial of a Number in Java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • One of the famous java interview program for freshers is finding factorial of a number using java program
  • Calculating the factorial of a number using java example program with recursion.

  • Factorial number means multiplication of all positive integer from one to that number.
  • n!=1*2*3.......*(n-1)*n.
  • Here ! represents factorial.
  • Two factorial:   2!=  2*1=2
  • Three factorial: 3!= 3*2*1=6.
  • Four factorial :  4!= 4*3*2*1=24.
  • Five factorial:    5!= 5*4*3*2*1=120.
  • Six factorial:      6!= 6*5*4*3*2*1=720
  • Seven factorial: 7!= 7*6*5*4*3*2*1=5040
  • Eight factorial:   8!= 8* 7*6*5*4*3*2*1=40320.
  • By using loops we can find factorial of given number.
  • Lets how can we find factorial of a number using java program without recursion.

Program #1: Java program to find factorial of a number using for loop


  1. package interviewprograms.instanceofjava;

  2. import java.util.Scanner;

  3. public class FactiorialProgram {

  4. public static void main(String args[]){
  5. Scanner in = new Scanner(System.in);
  6. System.out.println("Enter a number to find factorial");
  7. int n= in.nextInt();
  8. int fact=1;

  9. for (int i = 1; i < n; i++) {
  10. fact=fact*i;
  11. }

  12. System.out.println("Factorial of "+n+" is "+fact);

  13. }
  14. }

Output:

  1. Enter a number to find factorial
  2. 5
  3. Factorial of 5 is 24


Program #2: Java program to find factorial of a number using recursion.

  1. package interviewprograms.instanceofjava;

  2. import java.util.Scanner;

  3. public class FactiorialProgram {

  4. public static void main(String args[]){
  5. Scanner in = new Scanner(System.in);
  6. System.out.println("Enter a number to find factorial");
  7. int n= in.nextInt();
  8. int fact=1;

  9. for (int i = 1; i < n; i++) {
  10. fact=fact*i;
  11. }

  12. System.out.println("Factorial of "+n+" is "+fact);

  13. }
  14. }

Output:


  1. Enter a number to find factorial
  2. 5
  3. Factorial of 5 is 24

Program #3: Java program to find factorial of a number using recursion (Eclipse)

factorial number method java


Read: Finding Factorial of a Number in Java

Topic: 32% off Pulse Solo Dimmable LED Light with Dual Channel Bluetooth Speakers - Deal Alert Previous Topic   Next Topic Topic: Don’t be Delta: Four pillars of high availability

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use