i want to replace 5 with 6 and print the number
i want to replace 5 with 6 and print the number but i cant understand whats wrong in my code. for instance when i give 200 the output should be same i.e 200 but it gives 199 and when i give 205 as input instead of giving 206 as output it gives 205 itself.
#include<stdio.h>
#include<math.h>
int main()
{
int n,i=0,r,sum=0;
while(1){
printf("\nenter no\n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
printf("r is= %d\n",r);
if(r==5)
{
r=6;
}
sum=sum+(r*pow(10,i));
n=n/10;
i++;
}
printf("new no is:\t %d",sum);
sum=0;i=0;
}
return 0;
}
pow()
- which returns adouble
, not an integral type.pow
. That's a floating-point function that does not produce exact results. It's also completely unnecessary for your application. Get rid ofi
, and replace it with a multiplier that starts at1
. Multiply it by10
each time through the loop.