0
#include<stdio.h>
#include<math.h>
#define pow
int main()
{
    int num,num1,num2,n,q,sum=0,no_of_digits=0;
    printf("enter  a number\n");
    scanf("%d",&num);
    num1=num2=num; //storing the entered number in another variable 'num1'
    while (num!=0) //calclating and storing the number of digits in 'digit'
    {
        num=num/10; //dividing the number by 10. int data type ignores the decimal value, giving an integer
        no_of_digits++; 
    }
    while (num1!=0) 
    {
        n=num1%10; //taking one digit at a time in n
        q=pow(n,no_of_digits);//each digit^no. of digits
        sum=sum+q; // sum of all digit^no. of digits
        num1=num1/10;
        
    }
     printf("Sum of each digit^no. of digits =%d\n",sum);
     if (sum==num2)
        printf("%d is an armstrong number\n",num2);
     else
        printf("%d is NOT an armstrong number\n",num2);

  return 0;
} 

MY output: Sum of each digit^no. of digits =9 370 is NOT an armstrong number

Also, if i don't add the #define pow line,i get the following error. /usr/bin/ld: /tmp/ccGBiCYH.o: in function main': 7.c:(.text+0xda): undefined reference to pow' collect2: error: ld returned 1 exit status

Please tell me what I'm doing wrong. I checked all the other values (num,num1,....) and they all are right. The 'pow' function doesn't seem to be working correctly.

Abh_03
  • 1
  • 2
  • 2
    For the undefined reference to pow, see for the millionth time https://stackoverflow.com/questions/8671366/undefined-reference-to-pow-and-floor What do you think `#define pow` does? As for the logic here: Are you sure your computations fit in `int`s? – gspr Apr 16 '22 at 08:59
  • 2
    Your `#define pow` turns this: `q=pow(n,no_of_digits);` into this: `q=(n,no_of_digits);` which is synonymous with `q=no_of_digits;` so it is no wonder the answers were wrong. This is why we don't *guess*, especially when it comes to standard library functions. When it doubt, read the docs, see what headers contain formal declarations, and what *libraries* contain formal implementations, including the former and linking the latter respectively. And I concur with others; don't think to be using floating-point here regardless. – WhozCraig Apr 16 '22 at 09:11
  • Abh_03, to count the number of digits, do you consider 0 to have one or zero digits? Code here calculates zero. – chux - Reinstate Monica Apr 16 '22 at 09:54
  • Ref: [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number#C). – chux - Reinstate Monica Apr 16 '22 at 09:58
  • Does this answer your question? [Undefined reference to \`pow' and \`floor'](https://stackoverflow.com/questions/8671366/undefined-reference-to-pow-and-floor) – hyde Apr 16 '22 at 10:17

1 Answers1

0

It's required to give a definition for macro. When you write #define something, you'll have to define what it will do in your code.

A macro in C is a set of program statements that is replaced by the value of the macro throughout the entire program.

In your code, you wanted to write a macro for the power function.

So what a power function does?

It takes the base and exponent as parameters, then returns base ^ exponent.

You've to define this functionality in the macro. This is called Function-like Macros.

I would suggest learning more about macros before moving forward with it.

Let's get back to your code.

If you write a proper power function, I think your code will give the correct output.

Note: In case of both base and exponent being integer, it is safer to write your own integer power function.

Reason: Strange behavior of the pow function

Here I'm giving a sample code with a very basic approach of writing a power method.

#include<stdio.h>
#include<math.h>

int _power(int base,int exponent)
{
    int result = 1;
    for(int i = 1; i <= exponent; i++) result *= base;
    return result;
}

int main()
{
    int num,num1,num2,n,q,sum=0,no_of_digits=0;
    printf("enter  a number\n");
    scanf("%d",&num);
    num1=num2=num; //storing the entered number in another variable 'num1'
    while (num!=0) //calclating and storing the number of digits in 'digit'
    {
        num=num/10; //dividing the number by 10. int data type ignores the decimal value, giving an integer
        no_of_digits++;
    }
    while (num1!=0)
    {
        n=num1%10; //taking one digit at a time in n
        q=_power(n,no_of_digits);//each digit^no. of digits
        printf("n: %d   no_of_digits: %d    q: %d\n", n, no_of_digits, q);
        sum=sum+q; // sum of all digit^no. of digits
        printf("Sum: %d\n", sum);
        num1=num1/10;

    }
    printf("Sum of each digit^no. of digits =%d\n",sum);
    if (sum==num2)
        printf("%d is an armstrong number\n",num2);
    else
        printf("%d is NOT an armstrong number\n",num2);

    return 0;
}

Please check out the following resources to learn more about Macros and pow function

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14