{"id":590,"date":"2011-04-26T23:02:34","date_gmt":"2011-04-27T05:02:34","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=590"},"modified":"2011-04-26T23:03:34","modified_gmt":"2011-04-27T05:03:34","slug":"code-of-the-day-c-get-the-digits-from-a-number","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/04\/code-of-the-day-c-get-the-digits-from-a-number\/","title":{"rendered":"Code of the day: C, Get the Digits from a Number"},"content":{"rendered":"<p>Today&#8217;s Code of the Day is about how to find the digit of a number at a given position. I decided to go with the C \/ C++ programming languages to time, but you can easily translate the algorithms to other languages.<\/p>\n<p>C Code.<\/p>\n<pre lang='c' line=\"1\">\r\n#include <stdio.h>\r\n#include <math.h>\r\n\r\n\/\/ Function: getDigitFromNum returns a digit at a given index of a integer.\r\n\/\/ Goes from right to left with the starting index of 0.\r\nint getDigitFromNum( int num, int digit ){\r\n    num \/= pow( 10, digit );\r\n    return num % 10;\r\n}\r\n\/\/ Function: getDigitFromDec returns a digit at a given index of a double.\r\n\/\/ Goes from left to right with the starting index of 0.\r\nint getDigitFromDec( double dec, int digit ){\r\n    dec *= pow( 10, digit );\r\n    return (int)dec % 10;\r\n}\r\n\/\/ Function: getDigitFromNum returns the decimal values of a double.\r\ndouble getDecimals( double dec ){\r\n    return dec - (int)dec;\r\n}\r\n<\/pre>\n<p>Example:<br \/>\n<b>Input File<\/b><\/p>\n<pre lang='c' line=\"1\">\r\n\/\/ Programmed by Larry Battle, bateru.com\/news\r\n\/\/ Date: April 26, 2011\r\n\/\/ Purpose: To demonstrate how to get a digit by a position index.\r\n#include <stdio.h>\r\n#include <math.h>\r\n\r\nint getDigitFromNum( int num, int digit ){\r\n    num \/= pow( 10, digit );\r\n    return num % 10;\r\n}\r\nint getDigitFromDec( double dec, int digit ){\r\n    dec *= pow( 10, digit );\r\n    return ((int)dec) % 10;\r\n}\r\ndouble getDecimals( double dec ){\r\n    return dec - (int)dec;\r\n}\r\n\r\nvoid main(){\r\n    double dec = 1234.56789;\r\n    printf( \"\\n%d\", getDigitFromNum( (int)dec, 0 ));\r\n    printf( \"\\n%d\", getDigitFromDec( dec, 2 ));\r\n    printf( \"\\n%f\", getDecimals( dec ));\r\n}\r\n<\/pre>\n<p><b>Output:<\/b><\/p>\n<pre lang='c' line=\"1\">\r\n4\r\n6\r\n0.567890\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Code of the Day is about how to find the digit of a number at a given position. I decided to go with the C \/ C++ programming languages to time, but you can easily translate the algorithms to other languages. C Code. #include #include \/\/ Function: getDigitFromNum returns a digit at a given &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/04\/code-of-the-day-c-get-the-digits-from-a-number\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: C, Get the Digits from a Number<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,23,10],"tags":[175,166],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-backend-tech","category-math","category-tutorials","tag-c-c","tag-math"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":4,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/590\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}