1 / 6

C Program to Check Armstrong Number

https://data-flair.training/blogs/c-program-to-check-armstrong-number/

ayushii12
Download Presentation

C Program to Check Armstrong Number

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Program to Check Armstrong Number - Data Flair -

  2. C Program to Check Armstrong Number An Armstrong number can be described as the total of each of its digits scaled to the power of the total number of digits. For example, 153 is an Armstrong number since 1^3 + 5^3 + 3^3 = 153. In this article, we will discuss a C program that checks whether a given number is an Armstrong number or not. We will also explore alternative ways to Check Armstrong Number in C.

  3. 1. C Program to check Armstrong number Using Loop and Mathematical Calculations We start by defining a function `isArmstrong_Dataflair` that takes an integer `num` as a parameter and returns an integer value (1 if the number is an Armstrong number, 0 otherwise). Inside the function, we declare variables `originalNum`, `remainder`, `digits`, and `result`. We store the original value of `num` in `originalNum` for later comparison. We use a `while` loop to count the number of digits in the given number `num` by continuously dividing `originalNum` by ten until it becomes 0, incrementing the `digits` variable on each iteration. After counting the digits, we reset `originalNum` to its original value. Another while look can be used to find sum of all digits raised to power of total number of digits. We obtain each digit by taking the remainder of `originalNum` divided by ten and adding the result to the `result` variable using the `pow` function from the `math` library.

  4. 1. C Program to check Armstrong number Using Loop and Mathematical Calculations Finally, we compare the `result` with the original number `num`. If they are equal (after converting `result` to an integer), we return 1, indicating that the number is an Armstrong number; otherwise, we return 0. In the `main` function, we prompt the user to enter a number and store it in the `number` variable. We call the `isArmstrong_Dataflair` function with `number` as an argument and based on the returned value, we print the appropriate message.

  5. 2. Using Recursion to find Armstrong Number in C The recursive function `isArmstrong_Dataflair` takes two arguments: `num` (the number to be checked) and `digits` (the total number of digits in the number). The function calculates the sum of digits raised to the power of digits by recursively calling itself with a reduced number obtained by dividing `num` by 10. The recursion terminates when `num` becomes 0, and the sum is returned back to the calling function. The rest of the program remains the same as the previous approach, counting the digits and comparing the result with the original number to determine if it is an Armstrong number. Both the approaches described above can be used to determine whether a given number is an Armstrong number or not. Choose the approach that suits your programming style or requirements.

  6. Summary This was all about finding Armstrong’s number using C.

More Related