-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFunction.java
More file actions
254 lines (206 loc) · 5.64 KB
/
Copy pathJFunction.java
File metadata and controls
254 lines (206 loc) · 5.64 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* JAVA FUNCTIONS */
import java.util.*;
public class JFunction{
public static void printHelloWorld(){
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("I'm NAVLOK Device 07");
}
public static int calculatesum(int num1, int num2){
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
return sum;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = calculatesum(a,b); //here the varible sum is diffent from the above one coz its stored in another function.
printHelloWorld();
}
}
/* CALL BY VALUE */
/* PRODUCT OF TWO NOS */
// public class JFunction{
public static int multiply(int a,int b){
int product = a*b;
return product;
}
public static void main(String args[]){
int x = 5;
int y = 4;
int product = multiply(x,y);
System.out.print("The product = " + product);
}
// }
/* FACTORIAL OF N */
// import java.util.*;
// public class JFunction{
public static int factorial(int n){
int fact = 1;
for (int i=1; i<=n; i++){
fact = fact*i;
}
return fact;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Wanna know the factorial? \n Enter your no here : ");
int n = sc.nextInt();
int fact = factorial(n);
System.out.println("The factorial is " + fact);
}
// }
// public class JFunction{
public static int factorial(int n){
int fact = 1;
for (int i=1; i<=n; i++){
fact = fact * i;
}
return fact;
}
public static int bincoff(int n, int r){
int nfact = factorial(n);
int rfact = factorial(r);
int n_rfact = factorial(n-r);
int bincoff = nfact / (rfact * n_rfact);
return bincoff;
}
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
System.out.print("Wanna Calculate Binomial Co-efficient? \nProvide following values: \nThe value of n = ");
int n = sc.nextInt();
System.out.print("The value of r = ");
int r = sc.nextInt();
int bincoff = bincoff(n,r);
System.out.print("The Binomial Co-efficient is : " + bincoff);
}
}
/* FUNCTIONAL OVERLOADING
Diff No of Parameters*/
public class JFunction{
public static int sum(int a, int b){
return a+b;
}
public static int sum( int a , int b , int c){
return a+b+c;
}
public static void main(String args[]){
int s1 = sum(3,5);
int s2 = sum(3,5,7);
System.out.println(s1+ " " + s2 );
System.out.print(s2);
}
}
// Different type
public class JFunction{
public static int sum(int a, int b){
return a+b;
}
public static float sum(float a, float b){
return a+b;
}
public static void main(String args[]){
System.out.println(sum(4,8));
System.out.print(sum(4.2f,8.6f));
}
}
/* IS PRIME NORMAL & OPTIMISED CASE */
/* NORMAL */
public class JFunction{
public static boolean isPrime(int n){
if (n==2){
return true;
}
for (int i=2; i<=n-1; i++){
if (n%i==0){
return false;
}
}
return true;
}
public static void main(String args[]){
System.out.print(isPrime(1));
}
}
/* OPTIMISED */
//import java.math.*;
public class JFunction{
public static boolean isPrime(int n){
if (n==2){
return true;
}
for (int i=2; i<=(Math.sqrt(n)); i++){
if (n%i==0){
return false;
}
}
return true;
}
public static void main(String args[] ){
System.out.print(isPrime(7));
}
}
//import java.Scanner;
public class JFunction{
public static boolean isPrime(int n){
if (n==2){
return true;
}
for (int i=2; i<=Math.sqrt(n); i++){
if (n % i == 0){
return false;
}
}
return true;
}
public static int primeInRange(int n){
for (int i=2; i<=n; i++){
if (isPrime(i)==true){
System.out.print(i + " ");
}
}
return n;
}
public static void main(String args[]){
primeInRange(100);
}
}
/* Binary to Decimal*/
public class JFunction{
public static int binaryToDecimal(int binNum){
int myNum = binNum;
int pow = 0;
int dec = 0;
while (binNum>0){
int lastDigit = binNum % 10;
dec += lastDigit * (int)Math.pow(2,pow);
pow++;
binNum /= 10;
}
System.out.println("Decimal value of " + myNum + " = " + dec );
return dec;
}
public static void main(String args[]){
binaryToDecimal(1111);
}
}*/
/* Decimal To Binary */
// public class JFunction{
public static int decimalToBinary(int n){
int pow = 0;
int myNum = n;
int binNum = 0;
while (n>0){
int rem = n % 2;
binNum += rem * Math.pow(10,pow);
pow++;
n/=2;
}
System.out.print("The Binary for " + myNum + " in " + binNum);
return binNum;
}
// public static void main(String args[]){
// decimalToBinary(5);
// }
// }