Vous êtes sur la page 1sur 4

TP1

Ex1:

#include <iostream>
using namespace std;

int main(){
int a = 1;
int b = 2;
string nom;
cout<<"Entrez votre nom : ";
cin>>nom;
cout<<"Bonjour "<<nom<<", a = "<<a<<" et b = "<<b<<endl;
return 0;
}

Ex2:

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Entrez deux nombres entiers: ";
cin >> a;
cin >> b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}

Ex3:

#include <iostream>
using namespace std;

int main(){
int a, b, somme;
cout<<"Entrez deux nombres entiers: ";
cin>>a;
cin>>b;
somme = a + b;
cout<<"La somme de "<<a<<" et "<<b<<" est "<<somme<<endl;
return 0;
}
TP3

Ex1:

#include <iostream>
#include <vector>

using namespace std;

vector<int> facteursPremiers(int n) {
vector<int> facteurs;
for (int i = 2; i <= n; ++i) {
while (n % i == 0) {
facteurs.push_back(i);
n /= i;
}
}
return facteurs;
}

bool sontHomogenes(const vector<int>& v1, const vector<int>& v2) {


return v1 == v2;
}

int main() {
int N, M;
cout << "Donner N : ";
cin >> N;
cout << "Donner M : ";
cin >> M;

if (N <= 5 || M <= N) {
cerr << "Veuillez saisir des valeurs valides (5 < N < M)." << endl;
return 1;
}

vector<int> facteursN = facteursPremiers(N);


vector<int> facteursM = facteursPremiers(M);

cout << "Les facteurs premiers de " << N << " sont : [";
for (int facteur : facteursN) {
cout << facteur << ", ";
}
cout << "\b\b]" << endl;

cout << "Les facteurs premiers de " << M << " sont : [";
for (int facteur : facteursM) {
cout << facteur << ", ";
}
cout << "\b\b]" << endl;

if (sontHomogenes(facteursN, facteursM)) {
cout << N << " et " << M << " sont homogenes." << endl;
} else {
cout << N << " et " << M << " ne sont pas homogenes." << endl;
}

return 0;
}

Ex2:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

bool sontPremiersEntreEux(int a, int b) {


return gcd(a, b) == 1;
}

void chercherNombresParfaits(const vector<int>& L, int A) {


for (size_t i = 0; i < L.size(); ++i) {
for (size_t j = i + 1; j < L.size(); ++j) {
if (sontPremiersEntreEux(L[i], L[j])) {
cout << "Nombre unitairement parfait trouvé : " << L[i] <<
endl;
}
}
}
}

int main() {
vector<int> L;
int tailleL, A;
cout << "Donner la taille de L : ";
cin >> tailleL;

cout << "L = {";


for (int i = 0; i < tailleL; ++i) {
int nombre;
cin >> nombre;
L.push_back(nombre);
}
cout << "}" << endl;

cout << "Donner un entier A : ";


cin >> A;

chercherNombresParfaits(L, A);

return 0;
}

Vous aimerez peut-être aussi