{"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 --&gt; 10\r\n   B --&gt; 11\r\n   C --&gt; 12\r\n   D --&gt; 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=&#8221;banner&#8221;]\n<p><strong>Implementation of Method 2:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\"># include &lt;stdio.h&gt;<br\/># include &lt;stdlib.h&gt;<br\/># define bool int<br\/> <br\/>int getNumeralValue(char );<br\/>char getNumeral(int );<br\/> <br\/>\/* Function to add two numbers in base 14 *\/<br\/>char *sumBase14(char *num1,  char *num2)<br\/>{<br\/>   int l1 = strlen(num1);<br\/>   int l2 = strlen(num2);  <br\/>   char *res; <br\/>   int i;<br\/>   int nml1, nml2, res_nml;   <br\/>   bool carry = 0;<br\/>    <br\/>   if(l1 != l2)<br\/>   {<br\/>     printf(&quot;Function doesn&#039;t support numbers of different&quot;<br\/>            &quot; lengths. If you want to add such numbers then&quot;<br\/>            &quot; prefix smaller number with required no. of zeroes&quot;); <br\/>     getchar();         <br\/>     assert(0);<br\/>   }      <br\/> <br\/>   \/* Note the size of the allocated memory is one <br\/>     more than i\/p lenghts for the cases where we <br\/>     have carry at the last like adding D1 and A1 *\/  <br\/>   res = (char *)malloc(sizeof(char)*(l1 + 1));<br\/>       <br\/>   \/* Add all numerals from right to left *\/<br\/>   for(i = l1-1; i &gt;= 0; i--)<br\/>   {<br\/>     \/* Get decimal values of the numerals of <br\/>       i\/p numbers*\/         <br\/>     nml1 = getNumeralValue(num1[i]);<br\/>     nml2 = getNumeralValue(num2[i]);<br\/>      <br\/>     \/* Add decimal values of numerals and carry *\/<br\/>     res_nml = carry + nml1 + nml2;<br\/>      <br\/>     \/* Check if we have carry for next addition <br\/>        of numerals *\/<br\/>     if(res_nml &gt;= 14)<br\/>     {<br\/>       carry = 1;<br\/>       res_nml -= 14;<br\/>     }   <br\/>     else<br\/>     {<br\/>       carry = 0;     <br\/>     }       <br\/>     res[i+1] = getNumeral(res_nml);<br\/>   }<br\/>       <br\/>   \/* if there is no carry after last iteration <br\/>      then result should not include 0th character <br\/>      of the resultant string *\/<br\/>   if(carry == 0)<br\/>     return (res + 1);   <br\/> <br\/>   \/* if we have carry after last iteration then <br\/>     result should include 0th character *\/<br\/>   res[0] = &#039;1&#039;;<br\/>   return res;<br\/>}<br\/> <br\/>\/* Function to get value of a numeral <br\/>  For example it returns 10 for input &#039;A&#039; <br\/>  1 for &#039;1&#039;, etc *\/<br\/>int getNumeralValue(char num)<br\/>{<br\/>  if( num &gt;= &#039;0&#039; &amp;&amp; num &lt;= &#039;9&#039;)<br\/>    return (num - &#039;0&#039;);<br\/>  if( num &gt;= &#039;A&#039; &amp;&amp; num &lt;= &#039;D&#039;)  <br\/>    return (num - &#039;A&#039; + 10);<br\/>         <br\/>  \/* If we reach this line caller is giving <br\/>    invalid character so we assert and fail*\/ <br\/>  assert(0);<br\/>}<br\/> <br\/>\/* Function to get numeral for a value.   <br\/>  For example it returns &#039;A&#039; for input 10 <br\/>  &#039;1&#039; for 1, etc *\/<br\/>char getNumeral(int val)<br\/>{<br\/>  if( val &gt;= 0 &amp;&amp; val &lt;= 9)<br\/>    return (val + &#039;0&#039;);<br\/>  if( val &gt;= 10 &amp;&amp; val &lt;= 14)  <br\/>    return (val + &#039;A&#039; - 10);<br\/>     <br\/>  \/* If we reach this line caller is giving <br\/>    invalid no. so we assert and fail*\/     <br\/>  assert(0);<br\/>}<br\/> <br\/>\/*Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>    char *num1 = &quot;DC2&quot;;<br\/>    char *num2 = &quot;0A3&quot;;<br\/> <br\/>    printf(&quot;Result is %s&quot;, sumBase14(num1, num2));     <br\/>    getchar();<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\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}]}}