0% found this document useful (0 votes)
61 views14 pages

Probleme Rezolvate

The document contains code snippets in C++ for various programming problems involving basic mathematical and logical operations such as calculating sums, differences, products, quotients, modulo, determining even/odd numbers, roots of quadratic equations, maximum values, counting numbers in ranges, powers and more. Each code snippet includes comments describing the problem being solved and uses techniques like for/while loops, if/else statements to iterate, make comparisons and output results.

Uploaded by

Cosmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views14 pages

Probleme Rezolvate

The document contains code snippets in C++ for various programming problems involving basic mathematical and logical operations such as calculating sums, differences, products, quotients, modulo, determining even/odd numbers, roots of quadratic equations, maximum values, counting numbers in ranges, powers and more. Each code snippet includes comments describing the problem being solved and uses techniques like for/while loops, if/else statements to iterate, make comparisons and output results.

Uploaded by

Cosmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 14

Sumaprodus diferenta cat

// ConsoleApplication5.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>

using namespace std;


int main()
{
int a, b, s;
float p;
cout << "introduceti valuarea lui a\n" ;
cin >> a;
cout << "ibtroduceti valuarea lui b";
cin >> b;
s = a + b;
cout << " suma nr este " << s << endl;
s = a - b;
cout << " diferenta este " << s << endl;
s = a * b;
cout << " produsul nr este " << s << endl;
p = a / b;
cout << " catul nr este " << p;
}

triunghi
int lungime, inaltime,p;
cout << "lungimea este: ";
cin >> lungime;
cout << "inaltimea este: ";
cin >> inaltime;
cout << "lungimea laturei este " << lungime <<endl;
cout << "inaltimea triunghiului este " << inaltime<< endl;
p = 3 * lungime;
cout << "perimetrul triunghiului este " << p;

modulo

// ConsoleApplication10.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int x, m;
cout << "x=";
cin >> x;
if (x < 0)
{
m = -x;
}
else
{
m = x;
}
cout << "modulul este = "<<m;
}

Par impar

// ConsoleApplication10.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//
// ConsoleApplication36.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
int x;
cout << "x= ";
cin >> x;
if (x % 2==0)
{
cout << "este numar par " << x;
}
else
{
cout << "este numar impar " << x;
}
return 0 ;

Ec grad 2
float a, b, c, x1, x2, discriminant, realPart, imaginaryPart;
cout << "enter namber a ,b,c ";
cin >> a >> b >> c;
discriminant = b * b - 4 * a*c;

if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2 * a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;

ec grad 2 personalizata

float a, b, c, x1, x2, d, realPart, imaginaryPart;


cout << "enter namber a ,b,c ";
cin >> a >> b >> c;
d = b * b - 4 * a*c;
cout << "delta este " <<d<< endl;
if (d > 0) {
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
cout << "solutiile ecuatiilor sunt." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

if(d==0)
{
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(d)) / (2 * a);
cout << "x1 = x2 =" << x1 << endl;
}
if(d<0)
{
cout << "eruare suma este mai mica decat 0";
}
return 0;

perimetru si arie triunghi

// ConsoleApplication17.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{

int a, b, c;
double p, Aria;
cout << "a= "; cin >> a;
cout << "b= "; cin >> b;
cout << "c= "; cin >> c;
p = (a + b + c) / 2.; //p reprezinta semiperimetrul
Aria = sqrt(p*(p - a)*(p - b)*(p - c));
cout << "perimetrul este" << p << endl;
cout << "Aria este: " << Aria;
}

Numara numere
// ConsoleApplication21.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int numar = 0;
while (numar<2000)
{
numar=numar+1;
cout<< numar<<endl;
};

return 0;;
}

//inmultire;

// ConsoleApplication21.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, x, i;
cin >> n >> x;
for (i = 1; i <= n; i++)

x = x * x;
cout << x;

return 0;
}

Cu while

// ConsoleApplication24.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//
// ConsoleApplication21.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, x, i;
cin >> n >> x;
i = 1;
while (i <= n)
{
x = x * x;
i++;
}
cout << x;

return 0;
}

// cate numere pare se afla intr-un anumit interval

// ConsoleApplication25.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
int a, b, nr, i;
cin >> a >> b;
nr = 0;
for (i = a; i <= b; i++)

if (i % 2 == 0)
nr++;
cout << "nr" << nr;

return 0;
}

Aceeasi prob da cu do wile


// ConsoleApplication25.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//
#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
int a, b, nr, i;
cin >> a >> b;
nr = 0;
i = a;
if(i<=b)
do
{
if (i % 2 == 0)
nr++;
i++;

} while (i <= b);


cout << "nr" << nr;
return 0;
}

Alta met de rezolvare

// ConsoleApplication26.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int a, b, nr, i;
cin >> a >> b;
nr = 0;
if (a <= b)
{
nr = (b - a + 1) / 2;
if (nr & 2 == 0 && b % 2 == 0)
{
nr++;
}

cout<<nr;
}

// interva ntre numere

// ConsoleApplication36.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{

int a, b, nr, i;
cin >> a >> b;
nr = 0;
for (i = a; i <= b; i++)

//if (i % 1 == 0)
nr++;
cout << "nr" << nr;

return 0;

// puterea

// ConsoleApplication39.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, m;
long long p=1;
cin >> n >> m;
for (int i = 1; i <= m; i++)

p = p * n;
cout << p;

return 0;

// cod asci

// ConsoleApplication27.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int c;
for (c = 0; c <= 255; c++)
{
cout << (unsigned char)c << ' ' << c;
cout << '\t'; c++;
cout << (unsigned char)c << ' ' << c;
cout << endl;
}
return 0;
}

Max a 3 nr

// ConsoleApplication28.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
int a, b,max,c;
cin >> a >> b>>c;
max = a;
if (max < b)
{
max = b;

}
if (max < c)
{
max = c;
}

cout << max;


return 0;

Max n nr;
// ConsoleApplication30.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
double a, max;
int n, i;
cin >> n >> max;
for (i = 1; i < n; i++)
{
cin >> a;
}
if (max < a)
{
max = a;
}
cout << max;
return 0;
}

Mai bun e asa

int a, max, n, i;
cout << "a= ";
cin >> a;
cout << "n = ";
cin >> n;
max = a;
for (i = 1; i <= n; i++)
{
cin >> a;
if (a > max)
max = a;
}
cout << max;

max pana la intalnirea cu 0

// ConsoleApplication30.cpp : This file contains the 'main' function. Program execution


begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int a, max;
cout << "a = ";
cin >> a;
max = a;
while (a)
{
if (a > max)
max = a;
cin >> a;
}
cout << max;
}

Suma a n numere
// ConsoleApplication30.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int v[100];
int n, i;
cout << "n="; cin >> n;
//se citesc elementele vectorului
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
int suma = 0;
for (i = 0; i < n; i++)
suma = suma + v[i];
cout << "Media aritmetica a elementelor este egala cu:" << (float)suma / n;
return 0;
}

Combinatie de numere
// ConsoleApplication31.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, a, b;
cin >> n;
for (a = 1; a < n; a++)
for (b = a; b < n; b++)
if (b%a == 0)

cout << a << b;


return 0;
}
Metoda 2 mai efficient
// ConsoleApplication31.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, a, m;
cin >> n;
for (a = 1; a < n; a++)
for (m = 1; a*m < n; m++)
cout << a << a * m;

return 0;
}

Divisor
// ConsoleApplication32.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, x,rad;
cin >> n;
rad = sqrt(n);
cout << "1 ";
for (x = 2; x <= rad; x++)
if (n%x == 0)
cout << x <<n/x;
cout << n ;
return 0;
}

Fie n un numar natural si p un numar prim. Sa.se determine eel mai mare numar natural k astfel incat p'
divide n.
// ConsoleApplication33.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n, p, k = 0;
cout << "n= ";
cin >> n;
cout << "p= ";
cin >> p;
for (k = 0; n % p == 0; k++, n /= p)
cout << "k= " << k << endl;
return 0;
}

Termen Fibonacci
// ConsoleApplication33.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;

int fibonacci(int n)
{
if ((n == 1) || (n == 0))
{
return(n);
}
else
{
return(fibonacci(n - 1) + fibonacci(n - 2));
}
}

int main()
{
int n, i = 0;
cout << "Input the number of terms for Fibonacci Series:";
cin >> n;
cout << "\nFibonacci Series is as follows\n";

while (i < n)
{
cout << " " << fibonacci(i);
i++;
}

return 0;
}

vocale
// ConsoleApplication34.cpp : This file contains the 'main' function. Program execution
begins and ends there.
//

#include "pch.h"
#include <iostream>
using namespace std;
int main()
{

char a[51], vocale[] = "aeiou";


cin.get(a, 50);

int i, nrVocale = 0;
for (i = 0; i < strlen(a); i++) {
if (strchr(vocale, a[i]))
nrVocale++;

cout << "Numarul de vocale din textul citit este: " << nrVocale << '\n';
return 0;

You might also like