Vous êtes sur la page 1sur 10

KINGDOM OF SAUDI ARABIA

MINISTRY OF HIGHER EDUCATION ‫المملكة العربية السعودية‬


IMAM MUHAMMAD IBN SAUD ISLAMIC ‫وزارة التعليم العالي‬
UNIVERSITY
‫جامعة المام محمد بن سعود السلمية‬
COLLEGE OF COMPUTER AND
INFORMATION SCIENCES ‫كلية علوم الحاسب والمعلومات‬

Department: Computer Science Course: Computer Programming 1 (CS140)


Year: 1434/1435 Second Semester
Sections: all sections Allowed Time: 50 minutes

Tutorial 1 Solution (1 page, 10 Exercises)


Exercise 1
Write a C++ program that determines whether a number n given by the user is even or odd.
#include <iostream>
using namespace std;

int main() {
int n;

cout << "n = ";


cin >> 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;

cout << "n = ";


cin >> 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;

cout << "n = ";


cin >> n;

cout << "p = ";


cin >> 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;

cout << "n1 = ";


cin >> n1;

cout << "d1 = ";


cin >> d1;

cout << "n2 = ";


cin >> n2;

cout << "d2 = ";


cin >> d2;

n = n1 * d2 + n2 * d1;
d = d1 * d2;

cout << n1 << "/" << d1;


cout << " + ";
cout << n2 << "/" << d2;
cout << " = ";
cout << n << "/" << d;
cout << endl;

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 = ";


cin >> re1;

cout << "im1 = ";


cin >> im1;

cout << "re2 = ";


cin >> re2;

cout << "im2 = ";


cin >> im2;

re = re1 * re2 - im1 * im2;


im = re1 * im2 + im1 * re2;

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;

cout << "a = ";


cin >> a;

cout << "b = ";


cin >> b;

cout << "c = ";


cin >> c;

min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}

cout << "min = " << min << endl;

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;

cout << "a = ";


cin >> a;

cout << "b = ";


cin >> b;

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;
}

Vous aimerez peut-être aussi