'C program to check armstrong number
#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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Md. Faisal Habib |
