{"id":25625,"date":"2017-10-15T20:02:16","date_gmt":"2017-10-15T14:32:16","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25625"},"modified":"2017-10-15T20:02:16","modified_gmt":"2017-10-15T14:32:16","slug":"c-programming-program-add-two-numbers-base-14","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-program-add-two-numbers-base-14\/","title":{"rendered":"C Programming-program to add two numbers in base 14"},"content":{"rendered":"<p><strong>Method 1<\/strong><br \/>\nThanks to Raj for suggesting this method.<\/p>\n<pre>  1. Convert both i\/p base 14 numbers to base 10.\r\n  2. Add numbers.\r\n  3. Convert the result back to base 14.\r\n<\/pre>\n<p><strong>Method 2<\/strong><br \/>\nJust add the numbers in base 14 in same way we add in base 10. Add numerals of both numbers one by one from right to left. If there is a carry while adding two numerals, consider the carry for adding next numerals.<\/p>\n<p>Let us consider the presentation of base 14 numbers same as hexadecimal numbers<\/p>\n<pre>   A --> 10\r\n   B --> 11\r\n   C --> 12\r\n   D --> 13\r\n<\/pre>\n<pre>Example:\r\n   num1 =       1  2  A\r\n   num2 =       C  D  3   \r\n\r\n   1. Add A and 3, we get 13(D). Since 13 is smaller than \r\n14, carry becomes 0 and resultant numeral becomes D         \r\n\r\n  2. Add 2, D and carry(0). we get 15. Since 15 is greater \r\nthan 13, carry becomes 1 and resultant numeral is 15 - 14 = 1\r\n\r\n  3. Add 1, C and carry(1). we get 14. Since 14 is greater \r\nthan 13, carry becomes 1 and resultant numeral is 14 - 14 = 0\r\n\r\nFinally, there is a carry, so 1 is added as leftmost numeral and the result becomes \r\n101D<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Implementation of Method 2:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23%20include%20%3Cstdio.h%3E%0A%23%20include%20%3Cstdlib.h%3E%0A%23%20define%20bool%20int%0A%20%0Aint%20getNumeralValue(char%20)%3B%0Achar%20getNumeral(int%20)%3B%0A%20%0A%2F*%20Function%20to%20add%20two%20numbers%20in%20base%2014%20*%2F%0Achar%20*sumBase14(char%20*num1%2C%20%20char%20*num2)%0A%7B%0A%20%20%20int%20l1%20%3D%20strlen(num1)%3B%0A%20%20%20int%20l2%20%3D%20strlen(num2)%3B%20%20%0A%20%20%20char%20*res%3B%20%0A%20%20%20int%20i%3B%0A%20%20%20int%20nml1%2C%20nml2%2C%20res_nml%3B%20%20%20%0A%20%20%20bool%20carry%20%3D%200%3B%0A%20%20%20%20%0A%20%20%20if(l1%20!%3D%20l2)%0A%20%20%20%7B%0A%20%20%20%20%20printf(%22Function%20doesn\u2019t%20support%20numbers%20of%20different%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20lengths.%20If%20you%20want%20to%20add%20such%20numbers%20then%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%20prefix%20smaller%20number%20with%20required%20no.%20of%20zeroes%22)%3B%20%0A%20%20%20%20%20getchar()%3B%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20assert(0)%3B%0A%20%20%20%7D%20%20%20%20%20%20%0A%20%0A%20%20%20%2F*%20Note%20the%20size%20of%20the%20allocated%20memory%20is%20one%20%0A%20%20%20%20%20more%20than%20i%2Fp%20lenghts%20for%20the%20cases%20where%20we%20%0A%20%20%20%20%20have%20carry%20at%20the%20last%20like%20adding%20D1%20and%20A1%20*%2F%20%20%0A%20%20%20res%20%3D%20(char%20*)malloc(sizeof(char)*(l1%20%2B%201))%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%2F*%20Add%20all%20numerals%20from%20right%20to%20left%20*%2F%0A%20%20%20for(i%20%3D%20l1-1%3B%20i%20%3E%3D%200%3B%20i\u2013)%0A%20%20%20%7B%0A%20%20%20%20%20%2F*%20Get%20decimal%20values%20of%20the%20numerals%20of%20%0A%20%20%20%20%20%20%20i%2Fp%20numbers*%2F%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20nml1%20%3D%20getNumeralValue(num1%5Bi%5D)%3B%0A%20%20%20%20%20nml2%20%3D%20getNumeralValue(num2%5Bi%5D)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%2F*%20Add%20decimal%20values%20of%20numerals%20and%20carry%20*%2F%0A%20%20%20%20%20res_nml%20%3D%20carry%20%2B%20nml1%20%2B%20nml2%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%2F*%20Check%20if%20we%20have%20carry%20for%20next%20addition%20%0A%20%20%20%20%20%20%20%20of%20numerals%20*%2F%0A%20%20%20%20%20if(res_nml%20%3E%3D%2014)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20carry%20%3D%201%3B%0A%20%20%20%20%20%20%20res_nml%20-%3D%2014%3B%0A%20%20%20%20%20%7D%20%20%20%0A%20%20%20%20%20else%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20carry%20%3D%200%3B%20%20%20%20%20%0A%20%20%20%20%20%7D%20%20%20%20%20%20%20%0A%20%20%20%20%20res%5Bi%2B1%5D%20%3D%20getNumeral(res_nml)%3B%0A%20%20%20%7D%0A%20%20%20%20%20%20%20%0A%20%20%20%2F*%20if%20there%20is%20no%20carry%20after%20last%20iteration%20%0A%20%20%20%20%20%20then%20result%20should%20not%20include%200th%20character%20%0A%20%20%20%20%20%20of%20the%20resultant%20string%20*%2F%0A%20%20%20if(carry%20%3D%3D%200)%0A%20%20%20%20%20return%20(res%20%2B%201)%3B%20%20%20%0A%20%0A%20%20%20%2F*%20if%20we%20have%20carry%20after%20last%20iteration%20then%20%0A%20%20%20%20%20result%20should%20include%200th%20character%20*%2F%0A%20%20%20res%5B0%5D%20%3D%20\u20191\u2019%3B%0A%20%20%20return%20res%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20get%20value%20of%20a%20numeral%20%0A%20%20For%20example%20it%20returns%2010%20for%20input%20\u2019A\u2019%20%0A%20%201%20for%20\u20191\u2019%2C%20etc%20*%2F%0Aint%20getNumeralValue(char%20num)%0A%7B%0A%20%20if(%20num%20%3E%3D%20\u20190\u2019%20%26%26%20num%20%3C%3D%20\u20199\u2032)%0A%20%20%20%20return%20(num%20-%20\u20190\u2032)%3B%0A%20%20if(%20num%20%3E%3D%20\u2019A\u2019%20%26%26%20num%20%3C%3D%20\u2019D\u2019)%20%20%0A%20%20%20%20return%20(num%20-%20\u2019A\u2019%20%2B%2010)%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%2F*%20If%20we%20reach%20this%20line%20caller%20is%20giving%20%0A%20%20%20%20invalid%20character%20so%20we%20assert%20and%20fail*%2F%20%0A%20%20assert(0)%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20get%20numeral%20for%20a%20value.%20%20%20%0A%20%20For%20example%20it%20returns%20\u2019A\u2019%20for%20input%2010%20%0A%20%20\u20191\u2019%20for%201%2C%20etc%20*%2F%0Achar%20getNumeral(int%20val)%0A%7B%0A%20%20if(%20val%20%3E%3D%200%20%26%26%20val%20%3C%3D%209)%0A%20%20%20%20return%20(val%20%2B%20\u20190\u2032)%3B%0A%20%20if(%20val%20%3E%3D%2010%20%26%26%20val%20%3C%3D%2014)%20%20%0A%20%20%20%20return%20(val%20%2B%20\u2019A\u2019%20-%2010)%3B%0A%20%20%20%20%20%0A%20%20%2F*%20If%20we%20reach%20this%20line%20caller%20is%20giving%20%0A%20%20%20%20invalid%20no.%20so%20we%20assert%20and%20fail*%2F%20%20%20%20%20%0A%20%20assert(0)%3B%0A%7D%0A%20%0A%2F*Driver%20program%20to%20test%20above%20functions*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20char%20*num1%20%3D%20%22DC2%22%3B%0A%20%20%20%20char%20*num2%20%3D%20%220A3%22%3B%0A%20%0A%20%20%20%20printf(%22Result%20is%20%25s%22%2C%20sumBase14(num1%2C%20num2))%3B%20%20%20%20%20%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>C Programming-program to add two numbers in base 14 &#8211; Mathematical algorithms &#8211; Just add the numbers in base 14 in same way we add in base 10. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[74356,74367,74347,74360,74343,74342,74371,74379,74378,70194,74357,74341,74345,74366,70223,74346,74352,74374,74361,74349,74353,74362,74337,74377,74364,74348,74358],"class_list":["post-25625","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-binary-language-translator","tag-binary-math","tag-binary-number-system-in-computer","tag-binary-number-system-in-hindi","tag-binary-numbers-definition","tag-binary-numbers-in-computer","tag-binary-numbers-table","tag-binary-subtraction-examples","tag-binary-subtraction-rules","tag-binary-system","tag-binary-system-uses-power-of","tag-computer-binary-numbers","tag-computer-number-system","tag-conversion-of-number-system","tag-dec-to-binary","tag-different-number-systems","tag-number-conversion-system","tag-number-system","tag-number-system-conversion","tag-number-system-in-computer","tag-number-system-in-maths","tag-number-system-math","tag-tonights-lucky-numbers","tag-what-is-a-binary-number","tag-what-is-binary","tag-what-is-binary-system","tag-what-is-number-system-in-maths"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25625"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25625\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}