Reputation: 107
I made a program in C to find whether an integer is an Armstrong number or not for three digits numbers. If the number that has been given by the user is an Armstrong number it will print it is an armstrong number
and if it is not an Armstrong number it will print its not an armstrong number
. It is not showing any errors. Also it works with all three digit numbers except for a number. It is 153
. It is printing that it is not an Armstrong number even though it is an Armstrong number. Could anyone tell me whats wrong here?
Here is the code and output:
#include <math.h>
#include <stdio.h>
int main() {
int sum = 0;
int n;
//asking the user to enter a number
printf("Enter a number : ");
scanf("%d", &n);
// copying the value of n in a variable b
int b = n;
while (n != 0) {
// storing the reminders of n divided by 10 in a variable
int r = n % 10;
// finding the power
sum = pow(r, 3) + sum;
n = n / 10;
}
// here I am printing the sum just to show you guys what is the answer i am getting
printf("%d\n", sum);
// if sum equals to b, then do this
if (sum == b) {
printf("The number is an armstrong number\n");
}
// if not then do this
else {
printf("It is not an armstrong number\n");
}
}
Here is the output I am getting if I enter a three digit Armstrong number:
Enter a number : 370
370
The number is an armstrong number
Enter a number : 407
407
The number is an armstrong number
Here I am entering a number which is not an armstrong number:
Enter a number : 100
1
It is not an armstrong number
153
is an Armstrong number. Here is what happens when I enter 153:
Enter a number : 153
152
It is not an armstrong number
It is saying 153 is not a armstrong number
. Why does this happen?
How can I solve this?
Upvotes: 4
Views: 1113
Reputation: 11
#include<stdio.h>
int main()
{
int no=153,r=0,t;
t=no;
while(no>0){
r=r+(no%10)*(no%10)*(no%10);
no=no/10;
}
if(t==r){
printf("The number is a Armstrong number");
}else{
printf("The number is not an Armstrong number");
}
return 0;
}
Upvotes: 0
Reputation: 144951
The problem is probably linked to the implementation of the pow()
function on your system. Unless the implementors made a special case of integral arguments, pow(x, n)
can be computed as exp(log(x) * n)
which may produce a result that is just slightly less than the expected integral result because log(5)
cannot be represented exactly as a floating point number (ie: 124,999999999999
instead of 125
). Conversion to int
is performed as truncation of the fractional part, hence 124
instead of 125
.
Different implementations may produce slightly different results depending on how they handle integral arguments. This explains why you observe a problem and other people on different systems do not.
There are 2 ways to avoid this pitfall:
use round(pow(r, 3))
to ensure the result is the closest integer in case it is not already the expected integer.
use r * r * r
which is performed with integer arithmetics.
Here is a modified version with the latter approach:
#include <stdio.h>
int main(void) {
int n = 0;
//asking the user to enter a number
printf("Enter a number : ");
if (scanf("%d", &n) != 1)
return 1;
// copying the value of n in a temporary variable b
int b = n;
int sum = 0;
while (n != 0) {
// storing the reminders of n divided by 10 in a variable
int r = b % 10;
// finding the power
sum = r * r * r + sum;
b = b / 10;
}
// here I am printing the sum just to show you guys what is the answer I am getting
printf("%d\n", sum);
// if sum equals to b, then do this
if (sum == n) {
printf("The number is an armstrong number\n");
} else {
// if not then do this
printf("It is not an armstrong number\n");
}
return 0;
}
Upvotes: 0
Reputation: 1
#include<stdio.h>
#include<math.h>
int main(){
int num;
printf("Please enter a three digit number: ");
scanf("%d",&num);
//Taking input
int a = (num - (num % 100) );
int hu = a/100;
//Finding hundreds digit
int b = num - a - (num % 10) ;
int te = b/10;
//Finding tens digit
int un = num - a - b;
//Finding units digit
printf("hu = %d\n", hu);
printf("te = %d\n", te);
printf("un = %d\n", un);
//Conditional forms
//Use of pow is allowed through math.h header file
if(num == pow(hu, 3) + pow(te,3)+ pow(un, 3)){
printf("The entered number is an armstrong number");
}
else if(num != pow(hu, 3) + pow(te,3), pow(un, 3)){
printf("The entered number is not an armstrong number");
}
}
Upvotes: 0
Reputation: 1143
Your code is working on gcc compilers. check for yourself https://godbolt.org/z/Pvo19xrxx
as mentioned by others use r*r*r
instead of pow.
Upvotes: 1
Reputation: 223254
The pow
implementation in your standard C library is deficient and returns an approximation even when the mathematical result is exactly representable.
Stop using it for cubes. Replace pow(r,3)
with r*r*r
.
Upvotes: 4