Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Just a random program for 'Binomial Theorem' (Class-XI Maths).
- */
- import java.util.*;
- public class Binomial_Theorem
- {
- static long fact(int num) //Function to find factorial of a number.
- {
- int i;
- long f=1;
- for(i=1;i<=num;i++)
- {
- f *= i;
- }
- return f;
- }
- static long combination(int n, int r) //Function to find the combination required for a term.
- {
- long comb = fact(n) / (fact(n-r)*fact(r));
- return comb;
- }
- static void findExpansion(char a, char b, int power) //Function to find the terms of an expansion.
- {
- int i;
- String aterm = "";
- String bterm = "";
- String term;
- long ncr; String ncrterm = "";
- for(i=0;i<=power;i++)
- {
- ncr = combination(power, i);
- ncrterm = Long.toString(ncr); //nCr.
- aterm = Character.toString(a) + "^" + Integer.toString(power - i); //a^n-r
- bterm = Character.toString(b) + "^" + Integer.toString(i); //b^r
- term = ncrterm + " * " + aterm + " * " + bterm;
- System.out.println("Term "+(i+1)+" = "+term);
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter the first variable in (a+b)^n.");
- char a = sc.next().charAt(0);
- System.out.println("Please enter the second variable in (a+b)^n.");
- char b = sc.next().charAt(0);
- System.out.println("Please enter 'n' in (a+b)^n.");
- int power = sc.nextInt();
- findExpansion(a, b, power);
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment