23511

Reversing a five digit number with POW function in C

I have an exercise which demands a program that reverts a five digit number using pow, here is my try:

#include <math.h> #include <stdio.h> void main( void ) { int number, counter = 0, last_digit, reversed = 0; printf( "Please type a five digit number and I will reverse it\n" ); scanf( "%d", &number ); for( counter = 4; counter >= 0; counter-- ) { last_digit = number % 10; number = number / 10; //printf( "%d %d %.0f %.0f\n\n", reversed, last_digit, pow ( 10, counter ), reversed + last_digit * pow( 10, counter )); reversed = reversed + last_digit * pow( 10, counter ); }//end for printf( "The number reversed is %d", reversed ); }//end main

But if I type 12345 it returns 54320, the last digit is wrong! To check what was going on I enabled the printf comment an here it is a sample of execution:

Please type a five digit number and I will reverse it 12345 0 5 10000 50000 49999 4 1000 53999 53999 3 100 54299 54299 2 10 54319 54319 1 1 54320 The number reversed is 54320

For some reason the first 50000 is converted to 49999, there is one less! And the weird part is that it only happens the first time, then for example 53999 is correctly converted to 53999. What is happening here?

Answer1:

As the other commenters said, the issue is with rounding. pow() returns a double, and pow(10,4) must be returning a double close to 10000 but a bit or two off. Then in your print statement, when you print reversed + last_digit * pow( 10, counter ), this value is a double, close to 50000, but a bit off. When it prints, printf rounds to the precision you print to, so it prints as 50000. But when you assign to the integer reversed, the value is truncated, and if it is even one bit below 50000, it becomes 49999. Your teacher devised a very nice question - teaches nicely!

Answer2:

You can use round(pow(10,counter))

Recommend

  • Calculating Digital Root, is there a better way?
  • Edge-case: When (only) reversing order of template parameters in specialization, can non-specialized
  • Making a switch statement in C with an array?
  • How can I write a recursive function to reverse a linked list?
  • Getting directory of input file (Applescript)
  • Finding parents in a tree hierarchy for a given child LINQ (lambda expression)
  • How to sort things out in ListView?
  • How to delete a newline using \\b
  • how to pass class attribute and value to markdown syntax
  • Run EF6 Query in separate Thread on WinForm Button Click Event
  • Leaflet z-index
  • Cursor in wrong place in contenteditable
  • Get current user from inside the model in Sails
  • Single django queryset to get n adjacent items
  • C function strchr - How to calculate the position of the character?
  • Geom_jitter colour based on values
  • back button function for phonegap windows phone 7
  • Select value from xtype selection type checkbox CQ5
  • Cuda Clang and OS X Mavericks
  • allocating memory to an array of string
  • How to print columns containing value
  • Undefined references when compiling gSOAP client
  • Invalid Date on validation Date of js
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • JqueryMobile Popup menu is not working
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Delete MySQLi record without showing the id in the URL
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Arrays break string types in Julia
  • Linker errors when using intrinsic function via function pointer
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs