-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsixth_function.java
More file actions
90 lines (70 loc) · 2.26 KB
/
sixth_function.java
File metadata and controls
90 lines (70 loc) · 2.26 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
public class sixth_function {
public static void main (String args[]){
// whether the number isprime or not
System.out.println(isprime(8));
inrangeprime(100);
System.out.println(sum(34, 4));
System.out.println(sum(6, 7, 8));
System.out.println(sum1(3, 4, 5));
System.out.println(sum1(66, 77, 88));
binToDecimal(1);
decToBin(2);
}
public static boolean isprime(int n){
//corner case:
// for value of 2
// if(n==2){
// return true;
// }
for (int i = 2; i<= n-1; i++ ){ //i<=math.sqrt(n)
if(n% i==0){
return false;
}
}
return true;
}
static void inrangeprime(int n){
for(int i =2; i<=n; i++){
if( isprime(i)){ //true
System.out.println(i+" ");
}
}
System.out.println();
}
// using parameter
public static int sum(int a , int b){
return a+b;
}
public static int sum(int c ,int d, int e){
return c+d+e;
}
// using datatype
public static int sum1(int a ,int b, int c){
return a+b+c;
}
public static float sum1(float a , float b, float c){
return a+b+c;
}
//convert from binary to decimal
//binary number sestem-bitwise operator{0,1}
//binary to decimal
public static void binToDecimal(int binNumber){
int dec = 0;
while(binNumber>0){
int lastdigit = binNumber % 10;
dec = dec +(lastdigit * (int)Math.pow(2, binNumber) );
binNumber=binNumber/10;
}
System.out.println("decimal of "+ binNumber + " ="+dec );
}
//decimal to binary
public static void decToBin(int decNumber){
int bin = 0;
while(decNumber>0){
int rem = decNumber % 2;
bin = bin +(bin * (rem *(int)Math.pow(10, bin)));
decNumber=decNumber/2;
}
System.out.println("binary form " + decNumber+"="+ bin);
}
}