Facing problem with a specific input using pow() [duplicate] - c

This question already has answers here:
Is floating point math broken?
(31 answers)
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 2 years ago.
#include<stdio.h>
#include<math.h>
int main()
{
int n, num, sum=0, count=0, a, remain=0;
printf("Enter a number : ");
scanf("%d", &num);
a=num;
n=num;
while(a!=0)
{
a=a/10;
count++;
}
while(n!=0)
{
remain=n%10;
sum=sum+pow(remain, count);
printf("%d\n", sum);
n=n/10;
}
if(sum==num)
printf("Armstrong Number");
else
printf("Not an Armstrong Number");
return 0;
}
guys I am trying to make a program of checking if a number is Armstrong number or not. And I am facing this problem with '153' as an input specifically. The program works fine with various inputs but compiler is showing unusual behavior while adding 153. I will also attach the output with different inputs explicitly showing the addition of numbers in the 'sum' variable.
OUTPUT:
Enter a number : 153
27
151
152
Not an Armstrong Number
Enter a number : 371
1
344
371
Armstrong Number

pow returns a double, which means you could have weird rounding issues. You may want to consider implementing your own integer exponentiation function.

Related

Counting how many dozen in a number and also count its extra amount? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
How do i make this program counts how many dozen in a number and also count its extra amount?
This is what only I came up
#include<stdio.h>
int main()
{
float number, dozen;
printf("Please Enter any integer Value : ");
scanf("%f", &number);
dozen = number / 12;
printf("dozen of a given number %.2f is = %.2f", number, dozen);
return 0;
}
I dont know how i will get to count the dozen in a number, for example there is 45, i need to get 3 dozen and the extra will be 9.
You prompt for an integer but then use floats. You already had the correct dozen calculation and just miss the modulo operator %. Reformatted code for readability.
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number % 12
);
return 0;
}
and example execution:
Please Enter any integer Value : 14
dozen of a given number 14 is 1 with remainder 2
Now that there is an accepted answer,
here's a small lesson in arithmetic (plagiarising #Allan Wind's answer):
#include <stdio.h>
int main() {
printf("Please Enter any integer Value : ");
int number;
scanf("%d", &number);
printf("dozen of a given number %d is %d with remainder %d\n",
number,
number / 12,
number - ( number / 12 * 12)
);
return 0;
}

Getting incorrect output for pow function typecasted in int for 5,11,15... and so on raised to 2 [duplicate]

This question already has answers here:
Strange behaviour of the pow function
(5 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
other number number raised to 2 are giving correct answer but 5,11,15... are showing 1 number less than the correct answer.
#include<stdio.h>
#include<math.h>
int main(){
int side;
printf("Enter side value \n");
scanf("%d", &side);
printf("The area is %d", (int) pow(side,2.0));
return 0;
}

What causes a code to just randomly subtract or add small fractals to floating-point numbers? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 3 years ago.
Made a code that calculated the amount of negative numbers in an array and shows those numbers. The problem is when the program prints out the number, it changes the number a slight bit, adding or subtracting something like 0.003.
I have absolutely no clue as to what's wrong with it, tried asking my professor; she said she didn't know, so I am here.
float col[10];
...
for (int i = 0; i < 10; ++i)
{
scanf_s("%f", &col[i]);
}
...
printf("\n");
printf("There are %d negative numbers\n", ct);
for (int i = 0; i < 10; ++i)
{
if (col[i] < 0)
{
printf("[%d]=%f ", i, col[i]);
}
}
...
Put in -7786.88, command line printed out -7786.888184. It's fine on integers, just prints out a bunch of zeroes after the dot.

Why does this code print greater? [duplicate]

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 8 years ago.
1)#include<stdio.h>
int main()
{
float x=0.5;
if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}
output->lesser
2)#include<stdio.h>
int main()
{
float x=0.1;
if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");
return 0;
}
output->greater
Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?
EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?
I almost sure it's because you are comparing float and double.
There's an answer to why it is greater answered here : 0.1 float greater than double

Guess the number [duplicate]

This question already has answers here:
Why do I always get the same sequence of random numbers with rand()?
(12 answers)
Closed 9 years ago.
I have written a program but it always gives the same number (41).
Why does not it change next time I play?
Second question is: How can I limit the answer between 2 numbers?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int magic,guess;
char ans='y';
magic=rand();
printf("\t\t\tgame(guess the number)\n");
do{
printf("guess the magic number\n");
scanf("%d",&guess);
if(guess==magic){
printf("\n*****Right*****\n");
printf("%d is the magic number.",magic);
getch();
ans='n';
}else{
printf("\n*****Wrong*****\n");
if(guess>magic)
printf("your guess is too high\n");
else printf("your guess is too low\n");
printf("do you want to continue?\n");
ans=getch();
}
}while(ans=='y');
return 0;
}
I want to limit answer between 50 and 500. How can I do that?
put srand (time(NULL)); as the very first line of your main() function and let the magic start :)
now .... rand() gives you number in the range 0 to RAND_MAX
so lets say you want to limit it between x and y(inclusive)(x < y)
then int rand_num = rand() %(y-x+1) + x; would be your solution
Cheers :)
Seed the random number generator with srand - http://www.cplusplus.com/reference/cstdlib/srand/
Use something like the process ID and/Or the current time

Resources