Académique Documents
Professionnel Documents
Culture Documents
int main() {
int n;
if (n % 2 == 0) {
cout << n << " is even\n";
}
else {
cout << n << " is odd\n";
}
return 0;
}
Exercise 2
Write a C++ program that determines whether a number n given by the user is a multiple of 7 or
not.
#include <iostream>
using namespace std;
int main() {
int n;
if (n % 7 == 0) {
cout << n << " is a multiple of 7\n";
}
else {
cout << n << " is not a multiple of 7\n";
}
return 0;
}
Exercise 3
Write a C++ program that determines whether a number n given by the user is a multiple of another
number p given by the user or not.
#include <iostream>
using namespace std;
int main() {
int n;
int p;
if (n % p == 0) {
cout << n << " is a multiple of " << p << endl;
}
else {
cout << n << " is not a multiple of " << p << endl;
}
return 0;
}
Exercise 4
Write a C++ program that computes the sum of two rational numbers given by the user.
#include <iostream>
using namespace std;
int main() {
int n1, d1;
int n2, d2;
int n, d;
n = n1 * d2 + n2 * d1;
d = d1 * d2;
return 0;
}
Exercise 5
Write a C++ program that computes the product of two complex numbers given by the user.
#include <iostream>
using namespace std;
int main() {
double re1, im1;
double re2, im2;
double re, im;
cout << "(" << re1 << " + " << im1 << "i)";
cout << " * ";
cout << "(" << re2 << " + " << im2 << "i)";
cout << " = ";
cout << re << " + " << im << "i\n";
return 0;
}
Exercise 6
Write a C++ program that determines the minimum of 3 numbers a, b, and c given by the user.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int min;
min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
return 0;
}
Exercise 7
Write a C++ program that determines the median of 3 numbers a, b, and c given by the user.
#include <iostream>
using namespace std;
int main() {
int a, b, c;
int med;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
if (b <= a)
if (a <= c)
med = a;
if (c <= a)
if (a <= b)
med = a;
if (a <= b)
if (b <= c)
med = b;
if (c <= b)
if (b <= a)
med = b;
if (a <= c)
if (c <= b)
med = c;
if (b <= c) {
if (c <= a)
med = c;
cout << "med = " << med << endl;
return 0;
}
Exercise 8
Write a C++ program that computes the solution of a linear equation: ax + b = 0 where the
coefficients a and b are given by the user.
#include <iostream>
using namespace std;
int main() {
double a, b;
double x;
if (a != 0) {
x = - b / a;
cout << "x = " << x << endl;
}
else {
cout << "Error\n";
}
return 0;
}
Exercise 9
Write a C++ program that computes the real roots of a quadratic equation: ax2 + bx + c = 0 where
the coefficients a, b, and c are given by the user.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
double x1, x2;
double d;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
d = b * b - 4 * a * c;
if (d < 0) {
cout << "No real solutions\n";
}
else {
if (d == 0) {
x1 = - b / (2 * a);
cout << "x1 = " << x1 << endl;
}
else {
x1 = (- b + sqrt(d)) / (2 * a);
cout << "x1 = " << x1 << endl;
x2 = (- b - sqrt(d)) / (2 * a);
cout << "x2 = " << x2 << endl;
}
}
return 0;
}
Exercise 10
Write a C++ program that prints the letter grade (A+, A, B+, B, C+, C, D+, D, F) relative to a grade
given by the user.
#include <iostream>
using namespace std;
int main() {
int g;
cout << "g = ";
cin >> g;
if (g >= 95)
cout << "A+\n";
else
if (g >= 90)
cout << "A\n";
else
if (g >= 85)
cout << "B+\n";
else
if (g >= 80)
cout << "B\n";
else
if (g >= 75)
cout << "C+\n";
else
if (g >= 70)
cout << "C\n";
else
if (g >= 65)
cout << "D+\n";
else
if (g >= 60)
cout << "D\n";
else
cout << "F\n";
return 0;
}