A C program to print Armstrong numbers between two Numbers.

in c-library •  last year 

Welcome back ,
In this blog we will learn to print Armstrong Number between any two numbers input by the use.

Those who do not know about Armstrong Number, here are few word about the same.

An Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
Foe example if there is a number 'abc' ( Here total digits 3 such that 'a' is the first , 'b' is the second, and ,'c' is the third digit.
so we will find the cube of a,b,and,c and then add them up to see if it is equal to the number or not.

So let's Start,

Screenshot 2023-02-05 182959.png

Screenshot 2023-02-05 183015.png

You can copy the code from below :

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

int main()
{
int low, high, i, temp1, temp2, n = 0, rem, result = 0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d and %d are: ", low, high);

for (i = low + 1; i < high; ++i) {
temp2 = i;
temp1 = i;

while (temp1 != 0) {
temp1 /= 10;
++n;
}

while (temp2 != 0) {
rem = temp2 % 10;
result += pow(rem, n);
temp2 /= 10;
}

if (result == i) {
printf("%d ", i);
}

n = 0;
result = 0;
}
return 0;
}

Explanation of the above program

The above C program is used to display all Armstrong numbers between two given intervals.

  • The user is prompted to enter two numbers, which define the intervals.

  • The program uses a for loop to iterate through all the numbers between the given intervals. For each number, the program performs the following steps:

  • The number of digits in the current number is calculated and stored in the variable n. This is done by dividing the number by 10 until it becomes zero, and counting the number of iterations.
  • The sum of the digits raised to the power of n is calculated and stored in the variable result. This is done by using a while loop to repeatedly divide the number by 10, get the remainder, and add it to the running total of result.
  • If the calculated result is equal to the current number, it is printed as an Armstrong number.
  • The variables n and result are reset to zero for the next iteration of the for loop.
  • The program continues to iterate through all the numbers between the given intervals, printing the Armstrong numbers as it finds them.
  • Finally, the program returns 0 to indicate that it has finished execution.

Below is an example to understand how the output should look like.

Screenshot 2023-02-05 183030.png

So it looks like we were able to write our desired program successfully.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  

Greetings

Beautiful article. Please do not forget to support other Blurtconnect publications.

Thank you for more engagement on posts in the Blurtconnect community.

Upvotes are regularly obtained on posts published by authors that engage with fellow Blurtians through comments under articles.

Your vote, by clicking here, will mean a lot to the blurtconnect-ng Witness team

Peace



Posted from https://blurtlatam.intinte.org