-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathCoding_Challenges.java
122 lines (93 loc) · 3.38 KB
/
Coding_Challenges.java
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
//----------------------------------------------------------------Java Coding Challenges--------------------------------------------------------
// Java Coding Challenges on Numbers
// Write a program in Java to -
// 1. Convert decimal numbers to octal numbers.
// 2. Reverse an integer.
// 3. Print the Fibonacci series using the recursive method.
// 4. Return the Nth value from the Fibonacci sequence.
// 5. Find the average of numbers (with explanations).
// 6. Convert Celsius to Fahrenheit.
//----------------------------------------------------------------Solution of Problem:----------------------------------------------------------
// 1. Converting Decimal Numbers to Octal Numbers:
public class Main {
public static void main(String[] args) {
int decimal_number = 25;
int[] octal_number = new int[100];
int i = 0;
while (decimal_number > 0) {
octal_number[i] = decimal_number % 8;
decimal_number = decimal_number / 8;
i++;
}
System.out.print("Octal number: ");
for (int j = i - 1; j >= 0; j--) {
System.out.print(octal_number[j]);
}
return;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 2. Reversing an Integer:
public class Main {
public static void main(String[] args) {
int number = 12345;
int reversed_number = 0;
while (number != 0) {
reversed_number = reversed_number * 10 + number % 10;
number = number / 10;
}
System.out.print(reversed_number);
return;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 3. Printing the Fibonacci Series using Recursion:
public class Main {
static int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
public static void main(String[] args) {
int n = 10;
System.out.print("Fibonacci series: ");
for (int i = 0; i < n; i++) {
System.out.print(fibonacci(i) + " ");
}
return;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 4. Returning the Nth Value from the Fibonacci Sequence:
public class Main {
static int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
public static void main(String[] args) {
int n = 10;
int fibonacci_number = fibonacci(n);
System.out.print(fibonacci_number);
return;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 5. Finding the Average of Numbers:
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int size = numbers.length;
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
float average = (float)sum / size;
System.out.print("Average: " + average);
return;
}
}