Écrit moi un programme c++ qui vérifie si un nombre est un nombre d’amstrong
- User
Écrit moi un programme c++ qui vérifie si un nombre est un nombre d’amstrong
- Assistant
Voici un exemple de programme C++ qui vérifie si un nombre est un nombre d'Armstrong :
#include <iostream> #include <cmath> using namespace std; // Fonction pour calculer la puissance d'un nombre donné int puissance(int base, int exposant) { int resultat = 1; while (exposant != 0) { resultat *= base; --exposant; } return resultat; } // Fonction pour vérifier si un nombre est un nombre d'Armstrong bool estNombreArmstrong(int nombre) { int somme = 0; int original = nombre; int taille = to_string(nombre).length(); while (nombre != 0) { int chiffre = nombre % 10; somme += puissance(chiffre, taille); nombre /= 10; } return (somme == original); } int main() { int nombre; cout << "Entrez un nombre : "; cin >> nombre; if (estNombreArmstrong(nombre)) { cout << nombre << " est un nombre d'Armstrong." << endl; } else { cout << nombre << " n'est pas un nombre d'Armstrong." << endl; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.