Salut! Va rog mult sa mă ajutați! Am nevoie de ajutor la aceste probleme.
Răspunsuri la întrebare
6.
int double_ones_and_twos(int n)
{
if(n == 0)
return 0;
int last_digit = n % 10;
if(last_digit == 1 || last_digit == 2)
last_digit *= 2;
return double_ones_and_twos(n / 10) * 10 + last_digit;
}
7.
#include <iostream>
#include <cmath>
using namespace std;
bool is_perfect_square(int n) {
int sqrt_n = (int)sqrt(n);
return sqrt_n * sqrt_n == n;
}
void read_and_display_perfect_squares(int number) {
if (number == -1) return;
cin >> number;
if (is_perfect_square(number)) {
read_and_display_perfect_squares(number);
cout << number << " ";
}
else {
read_and_display_perfect_squares(number);
}
}
int main() {
cout << "Introduceti numere naturale, introduceti -1 pentru a opri citirea:" << endl;
read_and_display_perfect_squares(-1);
return 0;
}
8.
#include <iostream>
using namespace std;
void print_first_n_multiples(int x, int n, int current) {
if (current > n) return;
cout << x * current << " ";
print_first_n_multiples(x, n, current + 1);
}
int main() {
int x, n;
cout << "Introduceti numarul x: ";
cin >> x;
cout << "Introduceti numarul n: ";
cin >> n;
cout << "Primii " << n << " multiplii ai lui " << x << " sunt: ";
print_first_n_multiples(x, n, 1);
return 0;
}
Si la ultimul exercitiu pui cele doua conditii, daca este numar par (if n%2==0) face prima chestie, (else) face conditia de jos si cout n