-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathCoding_Challenges.cpp
129 lines (93 loc) · 3.03 KB
/
Coding_Challenges.cpp
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
//----------------------------------------------------------------C++ Coding Challenges---------------------------------------------------------
// C++ Coding Challenges on Numbers
// Write a program in C++ 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:
#include <iostream>
using namespace std;
int main() {
int decimal_number = 25;
int octal_number[100];
int i = 0;
while (decimal_number > 0) {
octal_number[i] = decimal_number % 8;
decimal_number = decimal_number / 8;
i++;
}
cout << "Octal number: ";
for (int j = i - 1; j >= 0; j--) {
cout << octal_number[j];
}
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 2. Reversing an Integer:
#include <iostream>
using namespace std;
int main() {
int number = 12345;
int reversed_number = 0;
while (number != 0) {
reversed_number = reversed_number * 10 + number % 10;
number = number / 10;
}
cout << reversed_number;
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 3. Printing the Fibonacci Series using Recursion:
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n = 10;
cout << "Fibonacci series: ";
for (int i = 0; i < n; i++) {
cout << fibonacci(i) << " ";
}
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 4. Returning the Nth Value from the Fibonacci Sequence:
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n = 10;
int fibonacci_number = fibonacci(n);
cout << fibonacci_number;
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------
// 5. Finding the Average of Numbers:
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
float average = (float)sum / size;
cout << "Average: " << average;
return 0;
}