Pages

Friday, March 28, 2025

code to check Armstrong numbers in C++

 #include <iostream>

#include <cmath>

using namespace std;

int main() {
    int n = 153;  // Change this number to test other values
    int num = n;
    int cubeSum = 0;

    while (num > 0) {
        int lastDig = num % 10; // Gets the last digit
        cubeSum += lastDig * lastDig * lastDig; // Adds the cube of the digit
        num /= 10;  // Removes the last digit
    }

    if (n == cubeSum) {
        cout << n << " is an Armstrong number." << endl;
    } else {
        cout << n << " is not an Armstrong number." << endl;
    }

    return 0;
}

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...