Monday, December 21, 2015

Java program to show Factorail Number:


Java program to show Factorail Number:

import java.util.Scanner;

class MyFactorial {
  public static void main(String args[]) {
    int n, fact = 1;
 
    System.out.println("Enter an integer to calculate it's factorial");
    Scanner in = new Scanner(System.in);  
    n = in.nextInt();
 
    if ( n < 0 ) {
      System.out.println("Number should be non-negative.");
    } else {
      for ( int c = 1 ; c <= n ; c++ ) {
        fact *= c;
      }
      System.out.println("Factorial of "+n+" is = "+fact);
    }
  }
}
The output of this program is as:
Enter an integer to calculate it's factorial
6
Factorial of 6 is = 720

Related Posts:

  • Character Extraction Character Extraction: 1) charAt() charAt() method is used to extract single character from String.Here is a small program to demonstrate chatAt() … Read More
  • Character Extraction Character Extraction: 2)getChars() If you want to extract more than one character at a time,you can use the getChars() method. It has general form… Read More
  • replace() method Modifying a String: replace();     The replace method is used to replace the one character of string.It has the general form:   &nbs… Read More
  • Fibonacci Using Recusion method Fibonacci Using Recusion method: package diamond; import static diamond.Fibonacci.fibonacciRecusion; public class FibonacciDemo {     pu… Read More
  • Access Protection in java: Access Protection in java: private     No modifier protected public  Same class … Read More

0 comments:

Post a Comment