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

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...