-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.java
More file actions
94 lines (78 loc) · 2.72 KB
/
function.java
File metadata and controls
94 lines (78 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class function {
// funtions and method
/*function :its a block of code which perform some work we can call fi=unction to execute the work
* method : function which are written under the classes to execute some of the work is called methods
* syntax eith parameter
* return type name (type para1 , type para2){
* body
* return statement;
* }
*/
//ques1. find the product of a and b
public static int product(int a ,int b){
int product=a*b;
return product;
}
// find factorial of numerical n
public static int factorial(int d){
int factorial = d*(d-1);
return factorial;
}
// form a calculator using switch case
public static int calculator(int number1 , int number2, int operation){
switch(operation){
case ('+'):
System.out.println(number1+number2);
break;
case ('-'):
System.out.println(number1-number2);
break;
case ('*'):
System.out.println(number1*number2);
break;
case ('/'):
System.out.println(number1/number2);
break;
case ('%'):
System.out.println(number1%number2);
break;
default:
System.out.println("you have not put valid input");
}
return operation;
}
public static int binomial(int n , int r){
System.out.println("enter binomial number6");
int fact_n = factorial(n);
int fact_r =factorial(r);
int fact_nmr = factorial(n-r);
int bin_coeff = fact_n/(fact_r * fact_nmr);
return bin_coeff;
}
public static void main (String args[]){
try (Scanner input = new Scanner(System.in)) {
System.out.println("product of two number are:");
int a =input.nextInt();
int b=input.nextInt();
int cal=product(a, b);
System.out.println(cal);
System.out.println("factorial of number is:");
int d = input.nextInt();
int fac =factorial(d);
System.out.println(fac);
System.out.println("enter number1");
int number1 = input.nextInt();
System.out.println("enter number2");
int number2 = input.nextInt();
System.out.println("enter operation");
char operation = input.next().charAt(0);
int calci= calculator(number1, number2, operation) ;
System.out.println(calci);
int n = input.nextInt();
int r = input.nextInt();
int bino = binomial(n, r);
System.out.println(bino);
}
}
}