Can someone read me this code? Thanks

This is a Program identifying an Armstrong number.
I've search this from google, And have it right. But my problem is,
*I can't understand why the initialization of power has its "(int, int)"
*Why do I need a loop in the first part of the program like this
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
*Also, in this power has its another "()" thingy
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
*And why did this exist here in the last part of the program
int power(int n, int r) {
int c, p = 1;

for (c = 1; c <= r; c++)
p = p*n;

return p;
}


Please help me with my project. This program works in turbo c/c++ 7.
As turbo c is the only one we use in our class. Hope that anyone helps me.


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
#include <iostream.h>
#include <conio.h>
int power(int, int);

int main()
{
   clrscr();
   int n, sum = 0, temp, remainder, digits = 0;

   cout<<"Please type a Number:\n";
   cin>>n;

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

   temp = n;

   while (temp != 0) {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
   }

   if (n == sum)
      cout<<n<<" is an Armstrong number.\n";
   else
      cout<<n<<" is not an Armstrong number.\n";
   getch();
   return (0);
}

int power(int n, int r) {
   int c, p = 1;

   for (c = 1; c <= r; c++)
      p = p*n;

   return p;
}
UP
*I can't understand why the initialization of power has its "(int, int)"

If you're referring to line 3, that is declaring the function power(). When you declare a function, you have to tell the compiler the types of the arguments. In this case it takes two arguments, both are integers.
*Why do I need a loop in the first part of the program like this

A nice trick to figuring out code is to look at how the data is used. In this case, the the first loop computes the number of digits in the number. The number of digits are used to determine if it's an Armstrong number in the second loop.
Ahh. So when the power has two int function, what does the operation between the (remainder, digits)

can you explain? How does the loop counts the digits of the inputted number?
And how does this

temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

compute to know if the inputted number is an Armstrong number?
too confused with the (xxx, xxx)
Thanks for your response.
Last edited on
power is a maths function. 2 to the power of 3 is 2*2*2.
so power(2,3) should yield 8.

the digit count happens here.
1
2
3
4
5
  temp = n;
      while (temp != 0) {
      digits++;
      temp = temp/10;
   }


take the number 10000.
if its not zero add 1 to the digit count.
divide by 10
number is now 1000
repeat til the number is zero.
see, dividing a decimal number by 10 reduces its digit count by 1. so we count how many times we can do that, which tells us how many digits there are.

Topic archived. No new replies allowed.