Friday, December 23, 2011

Program for factorial


import java.util.*;

public class Ch2Q40
{
    public static void main(String[] args)
    {Scanner kbd = new Scanner(System.in);
     System.out.print("Enter n and r: ");
     int n = kbd.nextInt();
     int r = kbd.nextInt();
     System.out.println("nCr = "+fact(n)/(fact(r)*fact(n-r)));
     System.out.println("nPr = "+fact(n)/fact(n-r));
    }
    static long fact(int n)
    {if(n==0)return 1;
     return n*fact(n-1);
    }
}

No comments:

Post a Comment