Factorial


This is a program to find the factorial of a number

import java.io.*;
class factorial
{
long fact(int n)
{
long f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of m and n");
int m=Integer.parseInt(br.readLine());
int n=Integer.parseInt(br.readLine());
long S=(fact(n)/fact(m))*fact(n-m);
System.out.println("The value of S is "+S);
}}

Comments

  1. import java.io.*;
    class factorial
    {
    public static void main(String args[])throws IOException
    {
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader br=-new BufferedReader(isr);

    System.out.println("Enter a number");
    double n,fact=1;//fact=1 because if we take fact=0 then anything multiply by zero will be zero so we had assumed fact=1
    n=Douuble.parseDouble(br.readLine);

    while(n>=1)
    {
    fact=fact*n;// dry run---STP1:::[fact= 1*5=5 .....5--=4].....STP2:::[fact=5*4=20...4--=3]...STP3:::[fact=20*3=60...3--=2]....STP4:::[fact=60*2=120...2--=1]....STP5::[fact=120*1=120 ....1--=0]
    n--;
    System.out.println("The factorial of the number is :="+fact);
    }
    }
    }


    CHECK THIS PROGRAM IF RIGHT OR WRONG PLS REPLY.

    ReplyDelete

Post a Comment

Popular posts from this blog

Automorphic number

Bubble sort

Prime number