tags:

views:

364

answers:

7

In a program to find whether the given number is an Armstrong number, I stored the input no (3 digit) as string as follows.

char input[10];
scanf("%s",&input);

Now I have to calculate cube of each digit by using pow method of math.h as follows.

int a;
a = pow(input[0],3);

By coding like this, I could not get correct result. If I print the value of "a", it shows some irrelevant answer. My doubt is, how to convert from string value to integer value?

+1  A: 

You do not need to get the address of the input array using &input. Simply passing input will pass the pointer to your string to scanf(). Your call to scanf() should look like this:

scanf("%s", input);

Another way of doing it, with the address of operator:

scanf("%s", &input[0]);
yjerem
Adam Rosenfield
+8  A: 

You are performing your calculation on the ASCII value of the digit. You'll need to convert it to a numeric value like so:

int digit = input[0] - '0';

int a; a = pow(digit, 3);
Adam Pierce
I like the old-school-C separate definition and declaration of a.
Stefan Mai
+2  A: 

There are two problems, both already detailed. The first is that your scanf needs a char*, not a char**. Fix this with what Jeremy said:

scanf("%s", input);

Next, calculate the power correctly, like Adam said:

a = pow(input[0]-'0',3);
Stefan Mai
Johannes Schaub - litb
Johannes Schaub - litb
here too. if you are going to change this answer, i upvote you. if this is continued to be upvoted, i will downvote it, because i think we should be exact on stackoverflow :)
Johannes Schaub - litb
A: 

On the other hand, you may need to replace the power with a more generic one like power = strlen(input)

so the code should look like this.

char input[10];
int power, sum = 0;

scanf("%s", input);
power = strlen(input);
sum += pow(input[0] - '0', power);

/* you need to compare in here */
Omer
There needs to be a loop around the 'sum += ...' line, of course.
Jonathan Leffler
Though the question does not show that either.
Jonathan Leffler
A: 

Doh, I might have spend too much time far of C, because I think most of the answers here miss the final goal... The advice on dropping the & is fine, of course, but there are several other issues with the scanf.
First, it should be OK for throw away code/homework, but it is dangerous: type 11 chars and get a buffer overflow.
Second, IIRC, since you need a number, you should use an int variable and get that. Untested code from a rusty mind:

int inputNumber;
scanf("%d", &inputNumber);

Here you need the & because you no longer have a pointer.
C should do the conversion for you, and you have a safer input.

Other problematic code:

int a = pow(input[0], 3);

You are not making math with the first digit, but with the Ascii value of the first digit! Ie. 49 for 1, 50 for 2...
Since you got a number from my previous correction, divide it by the correct power of 10 to get the digit.

If you prefer to go the string route, use input[0] - '0' at least.

PhiLho
A: 

Freehand coding, building on @PhiLho's idea to use an actual integer input:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int a, sum, i;

  printf("Enter an Armstrong number, an integer in the range 100..999:\n");
  if(scanf("%d", &a) != 1)
    return EXIT_FAILURE;
  if(a < 100 || a > 999)
    return EXIT_FAILURE;

  /* Now extract digits, and compute sum of cubes. */
  for(sum = i = 0; i < 3; i++)
  {
    sum += pow(a % 10, 3);
    a /= 10;
  }
  printf("sum is: %d\n", sum);
  return EXIT_SUCCESS;
}
unwind
+1  A: 

armstrong numbers are numbers that exhibit the armstron property in any given base, not just base 10.

int isArmstrong(int n, int b)
{
    int sum = 0;
    int n2 = n;
    int nDigits = 0;
    while(n2 != 0)
    {
        nDigits++;
        n2 /= b;
    }
    n2 = n;
    for(int i = 0; i < nDigits; i+++)
    {
        sum += pow(n2 % b, nDigits);
        n2 /= b;
    }

     return sum == n;
}
luke
+1 avoiding using scanf()
Nathan Strong