Check multiple numbers if they are Armstrong numbers

Hello want to create program that check multiple numbers if they are Armstrong numbers.
This program works without while cycle on one number but with multiple numbers its not working.
Thanks

INPUT
1
2
3
4
3
371
123
8208


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>
int main()
{
  int n, n1, rem, num = 0; 
  n1=n;
  while (scanf ("%d", &n)>0)
  {
    rem = n1%10;
    num += rem*rem*rem;
    n1/= 10;
    
 if (num == n)
   printf("%d is Armstrong number \n", n);
  else
    printf("%d is not Armstrong number \n", n);
  }
  return 0;
}

Output now
1
2
3
4
3 is not Armstrong number 
371 is not Armstrong number 
123 is not Armstrong number 
8208 is not Armstrong number



correct output
1
2
3
4
3 is not Armstrong number 
371 is  Armstrong number 
123 is not Armstrong number 
8208 is Armstrong number
Last edited on
Here is a working code.
First you need to find the digits of the number that is entered; this will give you the power to which you need to raise each digit in order to achieve the final sum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <math.h>
#include <vector>


void FindNumberDigits( std::vector<int>& v, int number )
{
  bool endDigits = true;
  do{
    int digit = number % 10;
    number = number / 10;
    if ( 0 == number )
    {
      endDigits = false;
    }
    
    v.push_back( digit );
  } while (endDigits);
}


bool IsArmstrongNumber( int number )
{
  std::vector<int> digits;
  FindNumberDigits( digits, number );

  int sumOfCubeDigits = 0;
  for ( int i = 0; i < digits.size(); ++i )
  {
    sumOfCubeDigits += pow( digits[i], digits.size() );
  }
  
  return (number == sumOfCubeDigits);
}


int main()
{
  int n;
  while (scanf ("%d", &n)>0)
  {
    if ( IsArmstrongNumber( n ) )
    {
      printf("%d is Armstrong number \n", n);
    }
    else
    {
      printf("%d is not Armstrong number \n", n);
    }
  }
  return 0;
}
Keep in mind the pow() function requires type float, double, or long double. Using integers may lead to "possible loss of data".

http://www.cplusplus.com/reference/cmath/pow/?kw=pow
http://stackoverflow.com/questions/2398442/why-isnt-int-powint-base-int-exponent-in-the-standard-c-libraries
The numbers that pow receives are int so from int to double or float there is no loss of data. From float or double to int there could be loss of data if the result is something like ex. 10.234 it will be considered 10 and you loose the decimals. In this case the numbers do not have decimals and so the result will not have decimals either. Hence the use without worry.
The reason I mention is because in Code Blocks (GNU) I tried this:
1
2
    int t = 5;
    t = pow(t,3);
with the result: 124 which is obviously incorrect. I also didn't get any warnings or error messages at compile time. VS2015 complains "conversion from 'double' to 'int', possible loss of data" but gives the correct output. Since I got the same C4244 warning with your code I just thought I'd mention it. Regards
I cant use #include <vector>
No using C++?

Your original code is curiously close to http://www.programiz.com/c-programming/examples/check-armstrong-number

That (linked) version is actually a better starting point. It does not mix input with the computation.

The first thing is to move the code into a function. From
1
2
3
4
int main() {
  // code
  return 0;
}

into:
1
2
3
4
5
6
7
8
void foo() {
  // code
}

int main() {
  foo();
  return 0;
}


Next, remove the user input code from the foo(), return it to the main() and pass the number to foo as function argument.
1
2
3
4
5
6
7
8
9
10
void foo( int number ) {
  // code
}

int main() {
  int n = 0;
  // get value of n from user
  foo( n );
  return 0;
}

Then you should figure out how to make the program to repeatedly ask and evaluate numbers. That repetition has nothing to do with the evaluation of any single number.


As already pointed out "An Armstrong number is an N-digit number that is equal to the sum of the Nth powers of its digits." I did not know that before. You did not tell that in the first post. Why should we have to look up the basics of your problem?

Anyway. number of digits is easy to calculate. It is better to have a separate function for that.
Likewise, integer version of pow() is easy to write and use.
You do need those two operations, because you input is not limited to 3-digit values.


PS. Your original code does not use anything from header <math.h>. Do not include headers that you don't need.
if you can't use <vector> and since according to keskiverto's link this looks mostly like a homework. we don't do homework here.
you can remove the vector and can implement an array to store the digits from the number and also use a counter when you fill the array this way you will know the size of the array and you can use it for the pow function.

Good luck.
Yes it is homework didnt know you are not doing homeworks here guys sorry ...
Ok guys thanks i did it but its now working with negative numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <stdio.h>
#include <math.h>
int armstrong(int n){
  int n1, sum = 0; 
  int length = floor(log10(abs(n))) + 1;
  n1=n;
    do
    {
	sum += pow(n % 10,length);
	n = n / 10;
	}
  while(n > 0);
    
  if (sum == n1)
    return 1;
  else
 return 0;
}

int main()
{
  int n; 
  while (scanf ("%d", &n)>0)
  {
 if (armstrong(n))
   printf("%d je Armstrongove cislo \n", n);
  else
    printf("%d nie je Armstrongove cislo\n", n);
  }
  return 0;
}


Last edited on
(Your indentation is not systematic. Confusing.)

That does not work as intended for positive numbers either.

Your armstrong() uses only the last digit of input. Shouldn't there be a loop?

Your armstrong() raises every digit to power of 3. Isn't that correct only for numbers that have three digits?


Your call to armstrong() does not change anything in the main program. Your main() is thus equivalent to:
1
2
3
4
5
6
7
8
9
10
int main()
{
  int n; 
  while (scanf ("%d", &n)>0)
  {
    if (0 == n) printf("%d is Armstrong number \n", n);
    else printf("%d is not Armstrong number\n", n);
  }
  return 0;
}


There are two options:
1. Print the "is / is not" in the function armstrong(). Not much fun.
2. Change the armstrong() to return a bool value.
(armstrong(n) should return true, if n is, and false, if not.)
Then you can do:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  int n; 
  while (scanf ("%d", &n)>0)
  {
    if ( armstrong(n) ) {
      printf("%d is Armstrong number \n", n);
    } else {
      printf("%d is not Armstrong number\n", n);
    }
  }
  return 0;
}


Negative values. Should the armstrong evaluation ignore the sign? If yes, then call the function with absolute value.
See http://www.cplusplus.com/reference/cstdlib/abs/

Topic archived. No new replies allowed.