-1

This is a program to find Armstrong number between 1-1000 (Sum of cubes of each digit of the number equals the number itself).It is printing some of the numbers correctly however it is not printing 153. My question is that why the case 153 is excluded? Thank you in advance.

#include<stdio.h>
#include<math.h>
void main()
{
    int i,save,rem;
    for(i=1;i<1000;i++)
    {
        int s=0;
        int save=i;                        /*Creating copy of the 
                                           variable i.*/
        while(save!=0)
        {
            rem=save%10;
            save/=10;
            s+=pow(rem,3.0);
        }
        if(i==s)                           //Comparing i with the sum.
            printf("\n%d",i);              //Printing armstrong number.
    }
}

Screenshot of the output window

yash gandhi
  • 45
  • 1
  • 5
  • Don't you think all the 1 digits numbers should also be in the output? – Brij Raj Kishore Jan 26 '19 at 05:14
  • Well I don't know which compiler you are using but it is printing 153. See here https://ide.geeksforgeeks.org/PN0grwbAFI – Brij Raj Kishore Jan 26 '19 at 05:16
  • 1
    Better to use `s += rem * rem * rem;` and avoid `pow` which returns a *double* and rounding may cause it to be slightly less than `i` causing your comparison to fail. I get `1, 153, 370, 371, 407` – David C. Rankin Jan 26 '19 at 05:16
  • Don't use `pow` for integers. It computes values using floating point arithmetic, which can have [floating point error](https://stackoverflow.com/q/588004/9254539). Just use `rem * rem * rem` instead. – eesiraed Jan 26 '19 at 05:17
  • See: [What Every Programmer Should Know About Floating-Point Arithmetic](http://www.phys.uconn.edu/~rozman/Courses/P2200_15F/downloads/floating-point-guide-2015-10-15.pdf) and [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – David C. Rankin Jan 26 '19 at 05:17
  • Declare variables only when you need them and don't reuse them. – eesiraed Jan 26 '19 at 05:19

4 Answers4

0

Using gcc on Windows 7 works with pow. However, this code may work for you to avoid rounding with pow().

This code also simplifies the looping and removes the re-declaration of save.

#include <stdio.h>

int main(void) {

    int i, sum, ones, tens, hunds;
    for(i = 1; i < 1000; i++) {

        sum = i/10;
        ones = i%10;
        tens = sum%10;
        hunds = sum/10;

        sum = ones*ones*ones + tens*tens*tens + hunds*hunds*hunds;


        if(i == sum)
            printf("\n%d", i);

    }

}

EDIT

Based on the comments by @Brij Raj Kishore, in case the post did intend to display all Armstrong Numbers 1-1000, substitute the following for loop for that above.

for(i = 1; i < 1000; i++) {

    sum = i/10;
    ones = i%10;
    tens = sum%10;
    hunds = sum/10;

    if(!(hunds | tens))
        sum = ones;
    else if (hunds == 0 && tens != 0)
        sum = ones*ones + tens*tens;
    else
        sum = ones*ones*ones + tens*tens*tens + hunds*hunds*hunds;

    if(i == sum)
        printf("\n%d", i);

}
RJM
  • 206
  • 3
  • 7
0

I have done some modifications in your program such as printing 1 digit numbers since they are armstrong numbers and avoiding floating point power function and other minor changes which can be seen in the comment in the code.

#include<stdio.h>
#include<math.h>
void main()
{
    int i,save,rem;
    for(i=1; i<= 1000;i++)
    {
        int s=0;
        int save=i;                        /*Creating copy of the    variable i.*/
        while(save!=0)
        {
            rem=save%10;
            save/=10;
            int temp = rem;
            for(int j = 1; j < 3; ++j) {  // power function
                rem = rem * temp;
            }
            s+=rem;
        }
        if(i==s)                           //Comparing i with the sum.
            printf("%d\n",i);              //Printing armstrong number.
    }
}

Output

1 153 370 371 407

Edit : I have made changes according to the OP defines the Armstrong number

Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
  • How would 3^3 = 3? – RJM Jan 26 '19 at 05:40
  • I think you misunderstood the definition of armstrong numbers. https://oeis.org/A005188 – Brij Raj Kishore Jan 26 '19 at 05:43
  • In the given number if we calculate the sum of each (digit)^(number of digits of that number) then the resulting number is same as given number, then it is armstrong. So 1^1 = 1, 2^1 = 2, 3^1 = 3 and so on. For 3 digit number 153 = 1^3 + 5^3 + 3^3. Similarly for 4 digit number for eg : 1634 = 1^4+6^4+3^4+4^4 – Brij Raj Kishore Jan 26 '19 at 05:44
  • You are correct, but that is not how the post defines it. The post states the cube of the digits. Essentially, this is 1 and the 3-digit Armstrong numbers. – RJM Jan 26 '19 at 05:46
  • So, Ok so just I just have to make some modification. – Brij Raj Kishore Jan 26 '19 at 05:49
  • It is unclear, if the OP actually wanted all Armstrong Numbers of just those as define in the post. – RJM Jan 26 '19 at 05:50
0
#include <stdio.h>
 
main()
{
    int n, temp, d1, d2, d3;
 
    printf("Print all Armstrong numbers between 1 and 1000:\n");
    n = 001;
    while (n<= 900)
    {
        d1 = n - ((n / 10) * 10);
        d2 = (n / 10) - ((n / 100) * 10);
        d3 = (n / 100) - ((n / 1000) * 10);
        temp = (d1 * d1 * d1) + (d2 * d2 * d2) + (d3 * 
        d3 * d3);
        if (temp == n)
        {
            printf("\n Armstrong no is:%d", temp);
        }
        n++;
    }
}
  • 1
    Appreciate the code snippet, but can you explain a bit about what you did/why you did it in some sort brief paragraph? A little narrative to go along with your code will help others in the future. Welcome to SO! – Justin Carroll Oct 22 '21 at 20:09
-1
#include<stdio.h>  
int main()    
{    
    int n,r,sum=0,temp;    
    printf("enter the number=");    
    scanf("%d",&n);    
    temp=n;    
    while(n>0)    
    {    
        r=n%10;    
        sum=sum+(r*r*r);    
        n=n/10;    
    }    
    if(temp==sum)    
        printf("Armstrong  number ");    
    else    
        printf("not Armstrong number");    
    return 0;  
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83