1. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 2. Ternary Operators in C Programming: A Beginners Guide (2023) 3. Switch Statements in C 2023: Complete Guide for Beginners 4. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 5. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 6. Storage Classes in C Programming: A Comprehensive Guide for 2023 7. Arrays in C Programming: A Comprehensive Guide for 2023 8. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 9. Beginners Guide to Strings in C Programming with Codes and Examples 10. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 11. A Comprehensive Guide to Structures in C Programming in 2023 12. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 13. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 14. Introduction to C Programming 15. Hello world program in C 16. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 17. Program for Fibonacci number in C - Codzify.com 18. What is palindrome number in C? 19. Write a C program to find a factorial of a given number 20. Write a C program to check whether a given number is prime or not 21. Write a C program to check whether a given number is an Armstrong number or not. 22. Write a C Program to transpose a matrix 23. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 24. Master String Handling Functions in C in 2023 - Codzify Topics

Write a C program to check whether a given number is an Armstrong number or not.

Article by: Manish Methani

Last Updated: September 26, 2021 at 2:04pm IST
2 min 45 sec

An Armstrong number is a number that equals the sum of the cube of each digit in a number. 

For example, suppose a given number is 153. 

To check whether a given number is Armstrong or not, we have to take the cube of each digit in a number and take a sum of those.

153 = (1*1*1) + (5*5*5) + (3*3*3)

After the addition of the cube of each digit, we get the original number which is 153. Hence, the given number is Armstrong.

#include <stdio.h>
int main()
{
   int n = 153, sum = 0, remainder = 0,tempNumber;
   tempNumber = n;
   while(tempNumber!=0)
   {
      remainder = tempNumber % 10;
      sum = sum + (remainder*remainder*remainder);
      tempNumber /= 10;
   }

   if(sum==n)
   {
     printf("%d is an Armstrong number.",n);
   }
   else
   {
     printf("%d is not an Armstrong number.",n);
   }
   return 0;
  }

Output

153 is an Armstrong number

Program Logic

  1. First, we take the original number into a temporary variable which is tempNumber.
  2. Then, we get the last digit of a temporary number using the modulo method inside a loop.
  3. After getting the last digit of a number, we added it with the cube of that last digit number.
  4. Then assigned the remaining digits except for the last digit of a number using the Quotient method.
  5. This process will be in the loop till we find that givenNumber do not reach 0.

If at the end we find that this new number after addition is found as. same or not. If yes, then the given number is an Armstrong number else it is not an Armstrong number.

C Program to check whether a given number is Armstrong or not using a user-defined function

#include <stdio.h> 
int isArmstrong(int n)
{
  int tempNumber=n;
  int remainder,sum;
  sum=0;
  while(tempNumber!=0)
  {
    remainder=tempNumber%10;
    sum = sum + (remainder*remainder*remainder);
    tempNumber/=10;
  }

if(sum==n) 
   return 1
else
   return 0;
}

int main()
{
  int n = 153;
  if(isArmstrong(n))
      printf("%d is an Armstrong number.",n);
  else
      printf("%d is not an Armstrong number.",n);
  return 0;
}

Output

153 is an Armstrong number

Learn to Build 5 Apps Without Coding using FlutterFlow

  • Lifetime access to all the 5 FlutterFlow Courses
  • Complete FlutterFlow training
  • All future course updates
  • Access via Codzify mobile app
  • 24h 42m of Learning Content
  • 5 FlutterFlow at Codzify just ₹1500 ($17.44)
  • Access to Dating App Course, Grocery App Course, Courses App Course, FlutterFlow AI Agents Course, Doctor Appointment Booking Supabase Course.
  • Clone Ready-Made Courses App instantly in FlutterFlow.
  • 30-Day 100% Money-Back Guarantee.

Learn More
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: [email protected]