1-الگوریتم تبدیل یک عدد از پایه b به پایه 2:
#include <iostream>
#include <vector>
std::vector<int> convert_to_base_2(int number, int base) {
std::vector<int> binary_representation;
while (number > 0) {
binary_representation.push_back(number % 2);
number /= 2;
}
return binary_representation;
}
int main() {
int number, base;
std::cout << "Enter the number: ";
std::cin >> number;
std::cout << "Enter the base: ";
std::cin >> base;
std::vector<int> binary_representation = convert_to_base_2(number, base);
std::cout << "The binary representation of " << number << " in base " << base << " is: ";
for (auto it = binary_representation.rbegin(); it != binary_representation.rend(); ++it) {
std::cout << *it;
}
std::cout << std::endl;
return 0;
}
2-الگوریتم چاپ 50 جمله از دنباله فیبوناچی:
#include <iostream>
int main() {
int first = 0, second = 1, next;
for (int i = 0; i < 50; ++i) {
std::cout << "Fibonacci sequence " << i + 1 << ": " << first << std::endl;
next = first + second;
first = second;
second = next;
}
return 0;
}
3-الگوریتم محاسبه توان مربع یک عدد n:
#include <iostream>
int calculate_square_power(int n) {
int sum = 0;
for (int i = 1; i <= 2 * n - 1; i += 2) {
sum += i;
}
return sum;
}
int main() {
int n;
std::cout << "Enter a natural number: ";
std::cin >> n;
int square_power = calculate_square_power(n);
std::cout << "The square power of " << n << " is " << square_power << std::endl;
return 0;
}