Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to print all 3 digits armstrong numbers in C

1 Answer

0 votes
// Armstrong = a number that equals the sum of its digits, 
//             each raised to a power of the number of digits
// For example 153, it's an Armstrong number because 1^3 + 5^3 + 3^3 = 153

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

bool armstrong(int n) {
    int reminder = 0, sum = 0, total_digits = log10(n) + 1;
    int temp = n;
    
    while (temp > 0) {
        reminder = temp % 10;
        sum += (float)pow((float)reminder, (float)total_digits);
        temp = temp / 10;
    }
    
    if (sum == n) {
        return true;
    }
        
    return false;
}

int main(void) {
    for (int i = 100; i <= 999; i++) {
        if (armstrong(i)) {
            printf("%d, ", i);
        }
    }

    return 0;
}




/*
run:

153, 370, 371, 407, 

*/

 



answered Dec 26, 2023 by avibootz

Related questions

1 answer 92 views
1 answer 108 views
1 answer 94 views
1 answer 111 views
1 answer 163 views
1 answer 125 views
1 answer 130 views
...