TP1 – C++
#include <iostream>
using namespace std;
#include <math.h>
EXERCICE 1
int cpt = 1;
while(cpt!=0){
cout << "Entrez une valeur positive (0 pour terminer): "<< endl;
cin >> cpt ;
if(cpt>0){
cout << "La racine de " << cpt << " est " <<(sqrt(cpt)) << endl;
else if (cpt<0){
cout << "Erreur, la valeur ne peut etre negative"<<endl;
EXERCICE 2 :
int n ;
cout<<"Entrez une valeur pour n : "<<endl;
cin >> n ;
float somme = 0 ;
for(int i=1;i<=n;i++){
somme = somme + 1/double(i) ;
cout << "La somme des n premiers termes de la suite est : "<< somme<<endl;
EXERCICE 3
cout << "Entrez la hauteur de l'arbre:"<<endl;
int h ;
cin >> h;
int etoile =1;
for(int i = h;i>=1;i--){
for(int j =1 ; j<i;j++){
cout<<"=";
for(int j = 1;j<=etoile;j++){
cout << "*";
etoile=etoile +2;
for(int j =1 ; j<i;j++){
cout<<"=";
cout<<"\n";
EXERCICE 4
int n ;
cout<<"Entrez une valeur pour n : "<<endl;
cin >> n ;
int u_n3=0;
int u_n2=0;
int u_n1=1;
if(n==0){
cout << u_n3;
}
else if(n==1){
cout << u_n2;
else if (n==2){
cout << u_n1;
else{
int u_n;
for(int i=3;i<=n;i++){
u_n=u_n1+u_n2+u_n3;
u_n3=u_n2;
u_n2=u_n1;
u_n1=u_n;
cout << u_n;
EXERCICE 5
float calculatrice(float x, float y, char o){
if(o!='+' & o!='-' & o !='*' & o!='/'){
throw invalid_argument("L'operateur est incorrect.");
if(o=='/' & y==0){
throw invalid_argument("On ne peut pas diviser par 0");
switch (o){
case '*':
return (x*y);
case '+':
return (x+y);
case '-':
return(x-y);
case '/':
return(x/y);
////////////////////////////////////////////////////////
float x;
float y;
cout<<"Entrez x:"<<endl;
cin>>x;
cout<<"Entrez y:"<<endl;
cin>>y;
cout<<"x+y="<< (calculatrice(x,y,'+'))<<endl;
cout<<"x-y="<< (calculatrice(x,y,'-'))<<endl;
cout<<"x*y="<< (calculatrice(x,y,'*'))<<endl;
cout<<"x/y="<< (calculatrice(x,y,'/'))<<endl;
EXERCICE 6 :
int appel=0;
void nb_appel(){
appel++;
cout<<"Appel de fonction numero "<<appel<<endl;
}
EXERCICE 7
int pgcd(int a, int b){
int t;
while(b!=0){
t=b;
b= a%b;
a=t;
return a;
int main() {
//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!