-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.java
More file actions
102 lines (86 loc) · 2.28 KB
/
functions.java
File metadata and controls
102 lines (86 loc) · 2.28 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
import java.util.*;
public class functions {
public static int CAlsum( int a, int b){
int sum = a+b;
return sum;
}
public static int multiply(int u, int v){
int mul = u*v;
return mul;
}
public static int factorial(int n){
int f = 1;
for(int i = 1; i<=n;i++){
f = f*i;
}
return f;
}
public static int bincoef(int n , int r){
int nfact = factorial(n);
int rfact = factorial(r);
int nmrfact = factorial(n-r);
int mul = rfact*nmrfact;
int bc = nfact/mul;
return bc;
}
// public static boolean isprime(int n){
// boolean isp = true;
// for(int i = 2; i<=n-1;i++){
// if(n%i==0){
// isp = false;
// break;
// }
// }
// return isp;
// }
// optimized code
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;
}
// prime in range
public static void rangeprime(int n){
for(int i = 2; i<=n;i++){
if(isprime(i)){
System.out.print(i+" ");
}
}
System.out.println();
}
// binary to decimal
public static void binToDec(int bNum){
int Pow = 0;
int decNum = 0;
while(bNum>0){
int lastDigit = bNum%10;
decNum = decNum+(lastDigit*(int)Math.pow(2,Pow));
Pow++;
bNum = bNum/10;
}
System.out.println(decNum);
}
// decimal to binary
public static void dectobin(int d){
int Pow =0;
int bin = 0;
while(d>0){
int rem = d%2;
bin = bin+rem*(int)Math.pow(10,Pow);
Pow++;
d = d/2;
}
System.out.println(bin);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
dectobin(7);
sc.close();
}
}