-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This Java program calculates and prints a term from the binomial expansion using user-provided values for the binomial coefficients and powers. It takes four inputs: two numbers for the binomial coefficient, and two powers, then computes and displays the specified term.
- Loading branch information
1 parent
781d317
commit ea00ed2
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.Scanner; | ||
|
||
class Bino { | ||
int p, m, e, w; | ||
int comb; | ||
Scanner sc = new Scanner(System.in); | ||
|
||
Bino() { | ||
p = 0; | ||
m = 0; | ||
} | ||
|
||
void input() { | ||
System.out.println("Enter the first number (p): "); | ||
p = sc.nextInt(); | ||
System.out.println("Enter the second number (m): "); | ||
m = sc.nextInt(); | ||
System.out.println("Enter the highest power (w): "); | ||
w = sc.nextInt(); | ||
System.out.println("Enter the lowest power (e): "); | ||
e = sc.nextInt(); | ||
} | ||
|
||
int fact(int x) { | ||
int s = 1; | ||
for (int i = 1; i <= x; i++) { | ||
s = s * i; | ||
} | ||
return s; | ||
} | ||
|
||
int compute() { | ||
int a = fact(p); | ||
int b = fact(m); | ||
int k = fact(p - m); | ||
comb = a / (b * k); | ||
return comb; | ||
} | ||
|
||
void bmial() { | ||
int d = compute(); | ||
System.out.print(d + " * " + Math.pow(p, w) + " * " + Math.pow(m, e)); | ||
w--; | ||
e++; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Bino ob = new Bino(); | ||
ob.input(); | ||
ob.bmial(); | ||
} | ||
} |