beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C Program to check Armstrong number

By Chaitanya Singh | Filed Under: C Programs

A number is called as Armstrong number if sum of cubes of digits of number is equal to the number itself. In the below C program, we are checking whether the input number is Armstrong or not.

#include<stdio.h>
int main()
{
   int num,copy_of_num,sum=0,rem;

   //Store input number in variable num
   printf("\nEnter a number:");
   scanf("%d",&num);

   /* Value of variable num would change in the
      below while loop so we are storing it in 
      another variable to compare the results 
      at the end of program
   */
   copy_of_num = num;

   /* We are adding cubes of every digit
    * and storing the sum in variable sum
    */ 
   while (num != 0)
   {
      rem = num % 10;
      sum = sum + (rem*rem*rem);
      num = num / 10;
   }

   /* If sum of cubes of every digit is equal to number
    * itself then the number is Armstrong
    */
   if(copy_of_num == sum)
      printf("\n%d is an Armstrong Number",copy_of_num);
   else
      printf("\n%d is not an Armstrong Number",copy_of_num);
   return(0);
}

Output:

Enter a number: 370
370 is an Armstrong Number

You can verify the result like this:

370 = 3*3*3 + 7*7*7 + 0*0*0
    = 27 + 343 + 0
    = 370

As you can see that sum of digits of number 370 is equal to the number itself.

Enjoyed this post? Try these related posts

  1. C Program to Convert Octal Number to Decimal Number
  2. Selection Sort Program in C
  3. Quicksort program in C
  4. C Program to find the Sum of First n Natural numbers
  5. C Program to check Leap Year
  6. C Program to Print String using Pointer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap