In terminal –
PS C:\Users\Paradox\Documents\Prog\C> cd "c:\Users\Paradox\Documents\Prog\C\" ; if ($?) { gcc armstrong.c -o armstrong } ; if ($?) { .\armstrong }
Enter the upper limit for the list of Armstrong Numbers :
Output for 1000 as input:
All-Armstrong number between 1 and 1000 are: 1 2 3 4 5 6 7 8 9 370 371 407
It’s omitting 153.
Also, it’s only taking 35 seconds to print the first statement. Apart from that, it prints final output instantaneously. I am using mingw btw.
Code –
#include <stdio.h>
#include <math.h>
int main()
{
int n1, a, n2, i, r;
printf("Enter the upper limit for the list of Armstrong Numbers : ");
scanf("%d", &n1);
printf("All-Armstrong number between 1 and %d are: ", n1);
for (int k = 1; k <= n1; k++)
{
a = 0,n2 = k,i=0;
do
{
n2/= 10;
i++;
}
while (n2!= 0);
n2 = k;
do
{
r = n2 % 10;
a+=pow(r, i);
n2/= 10;
}
while (n2!= 0);
if (a == k)
{
printf("%d ", a);
}
}
}
You need to sign in to view this answers