0% found this document useful (0 votes)
41 views8 pages

C++ Exercitii1

The document contains 11 coding examples in C++ that demonstrate solving various math problems like solving quadratic equations, calculating the result of arithmetic operations on two numbers based on user input, determining if three numbers are in arithmetic progression, calculating the area of different types of triangles based on side lengths, finding the minimum and maximum of three numbers, and more. Each example contains the necessary #include statements, defines a main function, gets input from the user, performs the relevant calculations, and outputs the results.

Uploaded by

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

C++ Exercitii1

The document contains 11 coding examples in C++ that demonstrate solving various math problems like solving quadratic equations, calculating the result of arithmetic operations on two numbers based on user input, determining if three numbers are in arithmetic progression, calculating the area of different types of triangles based on side lengths, finding the minimum and maximum of three numbers, and more. Each example contains the necessary #include statements, defines a main function, gets input from the user, performs the relevant calculations, and outputs the results.

Uploaded by

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

Laborator 2

Ex 1
#include <iostream>
using namespace std;

int main()
{
int a, b, c;
float x1, x2, delta;

cout << "Introduceti coeficientii ecuatiei: " << endl;


cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
if (a == 0)
if (b == 0)
if (c == 0)
cout << "Ecuatia admite o infinitate de solutii" << endl;
else
cout << "Ecuatia nu admite solutii reale" << endl;
else {
x1 = -(float)c / b;
cout << "Solutia ecuatiei este: " << x1 << endl;
}
else {
delta = b * b - 4 * a * c;
if (delta < 0)
cout << "Ecuatia admite solutii din multimea nr. complexe" << endl;
else
if (delta == 0) {
x1 = -b / (2 * a);
cout << "Solutia ecuatiei este: " << x1 << endl;
}
else {
x1 = (-b - sqrt(delta)) / (2 * a);
x2 = (-b + sqrt(delta)) / (2 * a);
cout << "Solutiile ecuatiei sunt: " << x1 << "si " << x2 << endl;
}
}

system("pause");
return 0;
}

Ex 2
#include <iostream>
using namespace std;

int main()
{
int a, b;
cout << "Introduceti primul numar: ";
cin >> a;
cout << "Introduceti al doilea numar: ";
cin >> b;
float r;
char x;

cout << "Alegeti o operatie: ";


cout << "1. Adunare" << endl;
cout << "2. Scadere" << endl;
cout << "3. Inmultire" << endl;
cout << "4. Impartire" << endl;
cin >> x;

switch (x) {
case '1': r = a + b; break;
case '2': r = a - b; break;
case '3': r = a * b; break;
case '4': r = a / (float)b; break;
};

if (x == '1' or x == '2' or x == '3' or x == '4')


cout << "Rezultatul operatiei este: " << r << endl;

system("pause");
return 0;
}

Ex 3
#include <iostream>
using namespace std;

int main()
{
int a, b, c;
float x1, x2, delta;

ecuatia2: {
cout << "Introduceti coeficientii ecuatiei: " << endl;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
if (a == 0)
if (b == 0)
if (c == 0)
cout << "Ecuatia admite o infinitate de solutii" << endl;
else
cout << "Ecuatia nu admite solutii reale" << endl;
else {
x1 = -(float)c / b;
cout << "Solutia ecuatiei este: " << x1 << endl;
}
else {
delta = b * b - 4 * a * c;
if (delta < 0)
cout << "Ecuatia admite solutii din multimea nr. complexe" << endl;
else
if (delta == 0) {
x1 = -b / (2 * a);
cout << "Solutia ecuatiei este: " << x1 << endl;
}
else {
x1 = (-b - sqrt(delta)) / (2 * a);
x2 = (-b + sqrt(delta)) / (2 * a);
cout << "Solutiile ecuatiei sunt: " << x1 << "si " << x2 << endl;
}
}

};
cout << "Doriti sa continuati? (d/D = Da, n/N = Nu): ";
char k;
cin >> k;
if (k == 'd' or k == 'D') {
system("cls");
goto ecuatia2;
}

system("pause");
return 0;
}

Ex 4
#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Introduceti numarul n: " << endl;
cin >> n;

int valAbs = 0;
if (n >= 0) {
valAbs = n;
}
else {
valAbs = -n;
}
cout << "|" << n << "| = " << valAbs << endl;

system("pause");
return 0;
}

Ex 5
#include <iostream>
using namespace std;

int main()
{
int a, b, c;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
if (2 * b == a + c)
cout << "Numere in progresie aritmetica" << endl;
else if (2 * c == a + b)
cout << "Numere in progresie aritmetica" << endl;
else if (2 * a == b + c)
cout << "Numere in progresie aritmetica" << endl;
else
cout << "Numerele nu sunt in progresie aritmetica" << endl;

system("pause");
return 0;
}

Ex 6
#include <iostream>
using namespace std;

int main()
{
int a, b, c, p;
float s;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
if ((a < b + c) && (b < a + c) && (c < a + b) && (a > 0) && (b > 0) && (c > 0)) {
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
if (a != b && b != c && a != c)
cout << "Triunghi oarecare, cu aria: " << s << endl;
else if (a == b && b == c & c == a)
cout << "Triunghi echilateral, cu aria: " << s << endl;
else if (a==b || b == c || a == c)
cout << "Triunghi isoscel, cu aria: " << s << endl;
if ((a * a == b * b + c * c) or (b * b == a * a + c * c) or (c * c == a * a
+ b * b))
cout << "Triunghi dreptunghic, cu aria: " << s << endl;
}
else
cout << "Valorile nu reprezinta laturile unui triunghi" << endl;

system("pause");
return 0;
}

Ex 7
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c, minim, maxim;
cout << "Introduceti 3 numere a,b,c: " << endl;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
minim = abs(a);
if (abs(b) < minim)
minim = abs(b);
if (abs(c) < minim)
minim = abs(c);
maxim = abs(a);
if (abs(b) > maxim)
maxim = abs(b);
if (abs(c) > maxim)
maxim = abs(c);
cout << "Minimul modulelor celor 3 numere este: " << minim << endl;
cout << "Maximul modulelor celor 3 numere este: " << maxim << endl;
system("pause");
return 0;
}

Ex 8
#include <iostream>
using namespace std;

int main()
{
int a, b, c, n;
float e;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
cout << "n= ";
cin >> n;
if (n == 1)
e = (a + b) / (float)c;
else if (n == 2)
e = (b + c) / (float)a;
else if (n == 3)
e = (c + a) / (float)b;
else
cout << "Optiunea aleasa este invalida" << endl;
if (n == 1 or n == 2 or n == 3)
cout << "Rezultatul valorii e este: " << e << endl;

system("pause");
return 0;
}
Varianta cu switch

#include <iostream>
using namespace std;

int main()
{
int a, b, c, n;
float e;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
cout << "n= ";
cin >> n;
switch (n) {
case 1: e = (a + b) / (float)c; break;
case 2: e = (b + c) / (float)a; break;
case 3: e = (a + c) / (float)b; break;
default: cout << "Optiunea aleasa este invalida" << endl;
};
if (n == 1 or n == 2 or n == 3)
cout << "Valoarea e este egala cu: " << e << endl;

system("pause");
return 0;
}

Ex 9
#include <iostream>
using namespace std;

int main()
{
char k;
cout << "Introduceti un caracter: ";
cin >> k;
int ok = 0;
switch (k) {
case'a':
case'A':
case'e':
case'E':
case'i':
case'I':
case'o':
case'O':
case'u':
case'U': ok = 1; break;
default:
if ((k >= 'b' and k <= 'z') or (k >= 'B' and k <= 'Z'))
ok = 2;
}
if (ok == 1)
cout << "Caracterul este vocala" << endl;
else if (ok == 2)
cout << "Caracterul este consoana" << endl;
else if (ok == 0)
cout << "Caracterul este un alt semn" << endl;

system("pause");
return 0;
}

Ex 10
#include <iostream>
using namespace std;

int main()
{
int a, b, x;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "x= ";
cin >> x;
if ((x >= a) and (x <= b))
cout << x << " apartine intervalului " << "[" << a << "," << b << "]" <<
endl;
else
cout << x << " nu apartine intervalului " << "[" << a << "," << b << "]" <<
endl;

system("pause");
return 0;
}

Ex 11
#include <iostream>
using namespace std;

int main()
{
int a, b, c, min, max;
cout << "a= ";
cin >> a;
cout << "b= ";
cin >> b;
cout << "c= ";
cin >> c;
min = max = a;
if (b < min)
min = b;
if (c < min)
min = c;
if (b > max)
max = b;
if (c > max)
max = c;
cout << "Minim= " << min << endl;
cout << "Maxim= " << max << endl;

system("pause");
return 0;
}

You might also like