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,895 questions

51,826 answers

573 users

How to print all armstrong numbers between 1 to 500 in C

1 Answer

0 votes
#include <stdio.h>
 
int armstrong(int n) {
    int result = 0, remainder;  
          
    while(n > 0) {  
        remainder = n % 10;  
        n = n / 10;  
        result += remainder * remainder * remainder;  
    }  
    return result;
}
 
int main(void) {
    // 153 = 1*1*1 + 5*5*5 + 3*3*3 = 153 = armstrong
          
    for(int i = 1; i <= 500; i++)          
        if (i == armstrong(i))  
            printf("%d\n", i);   

    return 0;
}
   
 
   
   
/*
run:
   
1
153
370
371
407
   
*/

 



answered Aug 22, 2021 by avibootz

Related questions

1 answer 142 views
1 answer 100 views
1 answer 204 views
1 answer 254 views
1 answer 253 views
...