S10

//1 //
#include <stdio.h>

int f(unsigned int n) {
if (n == 0) {
return 0;
}
int suma;
suma = (n-1) + f(n-1);
return suma;
}

int main() {
unsigned int n;
printf("Introduceti un numar intreg fara semn: ");
scanf("%d", &n);
printf("Suma primelor %d numere naturale este %d.\n", n, f(n));
return 0;

}

//2//
#include <stdio.h>

void delete(int v[100], int numar_elemente, int pozitie) {
if (pozitie == numar_elemente - 1) {
return;
}
v[pozitie] = v[pozitie + 1];
printf("%d ", v[pozitie]);
delete(v, numar_elemente, pozitie + 1);

return;
}

int main () {
int numar_elemente, pozitie, v[100], i;

printf("Introduceti numarul de elemente din vector: ");
scanf("%d", &numar_elemente);
printf("Introduceti elementele vectorului: ");
for (i = 0; i < numar_elemente; i++) {
scanf("%d", &v[i]);
}
printf("Introduceti pozitia: ");
scanf("%d", &pozitie);

for (i = 0; i < pozitie; i++) {
printf("%d ", v[i]);
}
delete(v, numar_elemente, pozitie);
printf("\n");
return 0;
}

//3//
#include <stdio.h>

int fibo(unsigned int n) {
if (n == 0) {
return 0;
}else if (n == 1) {
return 1;
}
int termen_pozitia_n = fibo(n-1) + fibo(n-2);
return termen_pozitia_n;
}

int main() {
unsigned int n, i;
printf("Introduceti pozitia din cadrul sirului Fibonacci. ");
scanf("%d", &n);
if (n == 1) {
printf("Primul termen din sirul Fibonacci este:\n");
}else {
printf("Primii %d termeni din sirul Fibonacci sunt:\n", n);
}
for (i = 0; i < n; i++) {
printf("%d ", fibo(i));
}
printf("\n");
return 0;
}

//4//
#include <stdio.h>

int putere(unsigned int n){
if(n == 0){
return 1;
}
int produs;
produs = 2 * putere(n-1);
return produs;
}

int main() {
unsigned int n;
printf("Introduceti numarul intreg fara semn: ");
scanf("%d", &n);
printf("Valoarea calcului 2 la puterea %d este %d.\n", n, putere(n));
return 0;
}

//5//
#include <stdio.h>

void descompunere(unsigned int n, int counter) {
unsigned int y = 1, i;
if (counter == 64) {
return;
}

int x = (n>>counter) & 1;
if (counter == 0) {
y = y * x;
}else {
for (i = 0; i < counter; i++) {
y = y *2;
}
y = y * x;
}

if (y != 0) {
if (y > n / 2) {
printf("%u[2^%d].", y, counter);
}else {
printf("%u[2^%d] + ", y, counter);
}
}
descompunere (n, counter + 1);

return;
}

int main() {
unsigned int n, counter = 0;
printf("Introduceti numarul: ");
scanf("%u", &n);

printf("Descompunerea, dupa puterile lui 2, a numarului %u este:\nn = ", n);
descompunere(n, counter);
printf("\n");
return 0;
}

//6//
#include <stdio.h>

int fibo(unsigned int n) {
if (n == 0) {
return 0;
}else if (n == 1) {
return 1;
}
int termen_pozitia_n = fibo(n-1) + fibo(n-2);
return termen_pozitia_n;
}

void functie(unsigned int n) {
int i = 0;
while (fibo(i) <= n) {
i++;
}
i = i - 1;
n = n - fibo(i);

if (fibo(i) == 0) {
printf("%u[f0].", fibo(i));
return;
}else {
printf("%u[f%d] + ", fibo(i), i);
}
functie(n);
return;
}

int main() {
unsigned int n;
printf("Introduceti numarul:");
scanf("%d", &n);

printf("Descompunerea, dupa termenii sirului Fibonacci, a numarului %u este:\nn = ", n);
functie(n);
printf("\n");
return 0;
}



Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

You forgot to ask a question, so we have no idea what you want.
Topic archived. No new replies allowed.