Waivio

Recommended Posts

C program to check whether a number is Armstrong Number.

0 comments

a1edutech0.342 years agoPeakD4 min read

Mathematical Definition of an Armstrong Number:

A positive integer having n digits, is said to be an Armstrong number if the total sum of each digit (of original number) which is raised by the power n (number of digits) is equal to original number.

https://files.peakd.com/file/peakd-hive/a1edutech/23u5bmCL2po6Eq9v1RAJb88rMs6ReezfEpDhTko1z9uwPHnw7tJ7j8Rn7PSGue2BaduiV.png

Examples:

We will take 153 as our number:
First, we will determine the number of digits in the number.
In this case,
153 has three digits (n=3).

Next, we will calculate the sum of each digit raised to the power of the number of digits (in this case, 3).
So:
→ 1³ + 5³ + 3³
→ 1 + 125 + 27 = 153
As you can see, the sum of each digit raised to the power of 3 is equal to the original number, 153. Therefore, 153 is an Armstrong number.
In other words, an Armstrong number is a number that can be expressed as the sum of its individual digits raised to the power of the number of digits. In this case, 1³ + 5³ + 3³ = 153, which meets this definition and makes 153 an Armstrong number.

Another example is 9474. It has four digits, so we raise each digit to the fourth power (9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474). So, 9474 is also an Armstrong number.


C Program to Check if a Number is an Armstrong Number:

Language-C

//C program to check whether number is an Armstrong Number.

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

int main() 
{
   int number, originalNumber, remainder, result=0, n=0;

   printf("Enter an integer:\n");
   scanf("%d", &number);

   originalNumber = number;

   // Counting the number of digits in the original number
   while (originalNumber != 0) {
      originalNumber /= 10;
      n++;
   }
   originalNumber = number;

   // Calculate sum of nth power of each digit
   while (originalNumber != 0) 
   {
      remainder = originalNumber % 10;
      result += pow(remainder, n);
      originalNumber /= 10;
   }

   // Check if the number is an Armstrong number
   if (result == number)
      printf("%d is an Armstrong number.\n", number);
   else
      printf("%d is not an Armstrong number.\n", number);

   return 0;
}


Let us go through the program step-by-step to understand how it works.

  1. We start by declaring the necessary variables:
    “number” — to store the user input.
    “originalNumber” — to store a copy of the original number.
    “remainder” — to store the remainder when we divide the number by 10.
    “result” — to store the sum of the nth power of each digit.
    “n” — to store the number of digits in the number.

  2. We then prompt the user to enter an integer and read the input using scanf() function.

  3. We store a copy of the original number in originalNumber.

  4. We then use a while loop to count the number of digits in the number. We divide the originalNumber by 10 repeatedly until it becomes 0, incrementing n each time (by n++).

  5. We then reset originalNumber to the original input number.

  6. We use another while loop to calculate the sum of the nth power of each digit. We calculate the remainder when we divide the originalNumber by 10, raise it to the power of n using the pow function from the math.h library, and add it to result, and divide originalNumber by 10.

  7. Once the loop completes, we check if result is equal to the original input number.
    → If it is, we print a message indicating that the number is an Armstrong number.
    → If not, we print a message indicating that the number is not an Armstrong number.

  8. Finally, we return 0 to indicate successful execution of the program.


Program Outputs:

https://files.peakd.com/file/peakd-hive/a1edutech/Eo6BgSSSpbTGJo5F5kZiWdKA83gwTCByamtiBMepcMQ4mbmvRQKTquZH6HAAhkpXx3r.png

***************

https://files.peakd.com/file/peakd-hive/a1edutech/23tGVhjLj1iirVqB72ocx3Wr9Ze6TmLa24MTe4nye5W6uJ4AcRup1G6cw9NVv3rwx3JY9.png

***************

https://files.peakd.com/file/peakd-hive/a1edutech/23tGVhjLj1iirVqB72ocx3Wr9ZKBujAp9jRzi4YipF6EffXk3WNuET4jtDFumCkXc4hkt.png


Finally, thanks for reading my post,
If you like my post, please do support me.
For More Details:
Youtube Channel: A1 EduTech
Peakd blogs: A1 EduTech
Artstation: Asad Ali
DTube Channel: A1 EduTech

Soon i will be making a video on YouTube explaining this program.

Comments

Sort byBest
AI
Waivio AI Assistant
How can I help you today?