{"id":1153,"date":"2020-06-11T10:24:43","date_gmt":"2020-06-11T10:24:43","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1153"},"modified":"2023-07-21T07:07:47","modified_gmt":"2023-07-21T07:07:47","slug":"gcd-of-two-very-large-numbers","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/","title":{"rendered":"How to Find GCD of two very large numbers?"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\" alt=\"\" \/><\/p>\n<p>In this article, we will delve deep into the topic of finding the greatest common divisor, GCD of two numbers. Understanding how to find the GCD of two numbers not only improves your fundamental logical and coding skills but also enhances your career prospects. Regular practice with coding questions, such as determining the GCD of two numbers, is an excellent habit that prepares you for technical interviews with various organizations. Additionally, the GCD is a key concept used to solve many complex problems. Whether you are just starting out in coding or are already on a coding career path, grasping the basics of finding the GCD of two numbers is essential. Let&#8217;s now proceed to explore and solve the problem of finding the GCD of two numbers.<\/p>\n<h2>How to Find GCD of Two Numbers:<\/h2>\n<p>For given two large numbers M and N, find their gcd(M,N).<br \/>\nGCD(M,N) is the largest number possible which divides both.<br \/>\nFinding a GCD of two numbers is not a complicated task. From basic mathematics you can solve how to find GCD of two numbers.<\/p>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/sorting\/LRGEGCD\" title=\"\"><\/a><br \/>\n<strong>Test Case:<\/strong><\/p>\n<p><strong>Input:<\/strong> 15 130<br \/>\n<strong>Output:<\/strong> 5<\/p>\n<p><strong>Explanation:<\/strong><br \/>\n5 divides 15 and 130 both completely and there exists no number greater than 5 which divides both of them completely.<\/p>\n<h2>Solving Approach How To Find GCD Of Two Numbers :<\/h2>\n<ol>\n<li>For provided numbers, we know N is of size less than or equal to 100, so we would need to store this number in a string.<\/li>\n<li>We know we get the remainder less than N always if we divide any number by N.<\/li>\n<li>To get the remainder by dividing a number stored String by Number, we calculate the remainder digit by digit. We update the remainder at every digit until all strings are iterated.<\/li>\n<li>In each iteration, we take the digit and make it greater than our current remainder by multiplying it by 10 and adding it to the digit, now we take the mod of digit by N and store it as our current remainder. After the whole string is iterated, we get our remainder.<\/li>\n<li>After we divide M by N and extract it\u2019s remainder, now both of them are in the Long Integer range, and we can carry with us our Long Division Method for getting gcd(M,N) where M is our new smaller number.<\/li>\n<li>Long Division Method: We repeatedly store remainder i.e (Larger number % Smaller number) and update Larger number by Smaller number until smaller number completely divides the larger number. Last Smaller Number after all steps, is our gcd.<br \/>\n<strong>Example:<\/strong><\/li>\n<\/ol>\n<p>We\u2019ll be given two numbers out of which one is a string and one is a long number. Hence we find the remainder of string number and long number.<br \/>\nWe can dry run the above mentioned technique,let\u2019s say given numbers are 143254 and 3, so first we convert 143254 to a long number which will be 143254 % 3. We take mod=0, which will store our final value, So,<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646650895227-Gcd%20of%20Two%20Very%20Large%20Numbers-01.png\" alt=\"\" \/><\/p>\n<p>As we have got our values in integer form, we can now use the long division method, to find out the gcd of both numbers that is gcd(n,mod).<br \/>\nLet\u2019s take 25 and 135 for example, we find gcd of both using the Long Division Method.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2020\/06\/prntgcd-1.png\" alt=\"\" \/><\/p>\n<p>As soon as our remainder becomes 0, dividend is our gcd(a,b). Let\u2019s see all the solutions for finding gcd of two numbers in (c, java, Python).<\/p>\n<p><strong>Solutions:<\/strong><br \/>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1156 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_1156 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_1156 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1156 .wpsm_nav-tabs > li.active > a, #tab_container_1156 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1156 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_1156 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_1156 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1156 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1156 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1156 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1156 .wpsm_nav-tabs > li > a:hover , #tab_container_1156 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_1156 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_1156 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_1156 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_1156 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_1156 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_1156 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_1156 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1156 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_1156 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1156 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_1156 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_1156\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1156\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1156_1\" aria-controls=\"tabs_desc_1156_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1156_2\" aria-controls=\"tabs_desc_1156_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1156_3\" aria-controls=\"tabs_desc_1156_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>JAVA<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_1156\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_1156_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include&lt;string.h&gt;\r\n\r\nlong long modBigNumber(char num[], long long m) \r\n{ \r\n    long long mod = 0; \r\n\r\n    for (int i = 0; i &lt; strlen(num); i++) { \r\n\r\n        int digit = num[i] - '0'; \r\n\r\n        mod = mod * 10 + digit; \r\n\r\n        mod = mod % m;         \r\n    }\r\n\r\n    return mod; \r\n}\r\n\r\nint main()\r\n{\r\n  int test;\r\n  scanf(\"%d\",&amp;test);\r\n  while(test--){\r\n\r\n    long long small;\r\n    long long largeF;\r\n    char large[2001];\r\n\r\n    scanf(\"%lld%s\",&amp;small,large);\r\n    largeF = modBigNumber(large,small);\r\n\r\n    if(largeF==0){\r\n      printf(\"%lld&#92;n\",small);\r\n    }\r\n    else{\r\n      int temp = small;\r\n      small = largeF;\r\n      largeF = temp;\r\n\r\n      while(1){\r\n        if(largeF%small==0){\r\n        printf(\"%lld&#92;n\",small);\r\n          break;\r\n        }\r\n        int temp = small;\r\n        small = largeF%small;\r\n        largeF = temp;\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1156_2\">\r\n\t\t\t\t\t\t\t\t\r\n<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nlong long modBigNumber(string num, long long m) \r\n{ \r\n    long long mod = 0; \r\n\r\n    for (int i = 0; i &lt; num.size(); i++) { \r\n\r\n        int digit = num[i] - '0'; \r\n\r\n        mod = mod * 10 + digit; \r\n\r\n        mod = mod % m;         \r\n    }\r\n\r\n    return mod; \r\n}\r\n\r\nint main()\r\n{\r\n  int test;\r\n  cin&gt;&gt;test;\r\n\r\n  while(test--){\r\n\r\n    long long small;\r\n    long long largeF;\r\n    string large;\r\n\r\n    cin&gt;&gt;small&gt;&gt;large;\r\n    largeF = modBigNumber(large,small);\r\n\r\n    if(largeF==0){\r\n      cout&lt;&lt;small&lt;&lt;endl;\r\n    }\r\n    else{\r\n      int temp = small;\r\n      small = largeF;\r\n      largeF = temp;\r\n\r\n      while(true){\r\n        if(largeF%small==0){\r\n          cout&lt;&lt;small&lt;&lt;endl;\r\n          break;\r\n        }\r\n        int temp = small;\r\n        small = largeF%small;\r\n        largeF = temp;\r\n      }\r\n    }\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1156_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n  static long modBigNumber(String num, long m){\r\n    long mod = 0; \r\n\r\n    for (int i = 0; i &lt; num.length(); i++) { \r\n\r\n        int digit = num.charAt(i) - '0'; \r\n\r\n        mod = mod * 10 + digit; \r\n\r\n        mod = mod % m;         \r\n    }\r\n\r\n    return mod; \r\n  }\r\n\r\n  public static void main(String args[]) throws IOException {\r\n      Scanner sc = new Scanner(System.in);\r\n      int test = sc.nextInt();\r\n\r\n      while(test!=0){\r\n\r\n        long small = sc.nextLong();\r\n        long largeF;\r\n        String large = sc.next();\r\n\r\n\r\n        largeF = modBigNumber(large,small);\r\n\r\n        if(largeF==0){\r\n          System.out.println(small);\r\n        }\r\n        else{\r\n          long temp = small;\r\n          small = largeF;\r\n          largeF = temp;\r\n\r\n          while(true){\r\n            if(largeF%small==0){\r\n              System.out.println(small);\r\n              break;\r\n            }\r\n            temp = small;\r\n            small = largeF%small;\r\n            largeF = temp;\r\n          }\r\n        }\r\n        test--;\r\n      }\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_1156 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_1156 a\"),jQuery(\"#tab-content_1156\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>In conclusion, we have explored various methods to find the greatest common divisor (GCD) of two numbers. Understanding how to calculate the GCD is an essential skill that improves your logical thinking and problem-solving capabilities. It is a fundamental concept used in many complex coding problems and can significantly contribute to your success in technical interviews and competitive programming. Whether you are starting your coding journey or already pursuing a coding career, mastering the GCD computation will undoubtedly strengthen your skills and make you a more proficient coder.<\/p>\n<h2>Frequently Asked Questions (FAQs) related to GCD of two numbers<\/h2>\n<p>Some FAQs related to GCD of two numbers:<\/p>\n<p><strong>Q1. Why is finding the GCD important in programming?<\/strong><br \/>\nFinding the GCD is crucial in programming because it is used in various algorithms and mathematical computations. It helps in simplifying fractions, solving modular arithmetic problems, and optimizing algorithms like the Euclidean algorithm.<\/p>\n<p><strong>Q2. What are the different methods to find the GCD of two numbers?<\/strong><br \/>\nThere are several methods to find the GCD of two numbers, including the Euclidean algorithm, backtracking, prime factorization, and binary GCD algorithm. Each method has its advantages and may be suitable for different scenarios.<\/p>\n<p><strong>Q3. Can the GCD of two numbers be larger than the numbers themselves?<\/strong><br \/>\nNo, the GCD of two numbers can never be larger than the numbers themselves. The GCD will always be less than or equal to the smaller of the two numbers.<\/p>\n<p><strong>Q4. How does the Euclidean algorithm work for finding the GCD?<\/strong><br \/>\nThe Euclidean algorithm uses repeated division to find the GCD of two numbers. It continuously divides the larger number by the smaller number and assigns the remainder as the new dividend until the remainder becomes zero. The GCD is then the last non-zero remainder obtained in the process.<\/p>\n<p><strong>Q5. Is the GCD of two numbers unique?<\/strong><br \/>\nYes, the GCD of two numbers is unique and remains the same regardless of the order of the two numbers.<\/p>\n<p><strong>Q6. Can the GCD of two numbers be negative?<\/strong><br \/>\nThe GCD is defined as a positive integer, so it is always non-negative. However, if one or both of the input numbers are negative, the absolute values are used to calculate the GCD.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will delve deep into the topic of finding the greatest common divisor, GCD of two numbers. Understanding how to find the GCD of two numbers not only improves your fundamental logical and coding skills but also enhances your career prospects. Regular practice with coding questions, such as determining the GCD of [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[92],"tags":[],"class_list":["post-1153","post","type-post","status-publish","format-standard","hentry","category-sorting-interview-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Programs to Find GCD of Two Numbers<\/title>\n<meta name=\"description\" content=\"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Programs to Find GCD of Two Numbers\" \/>\n<meta property=\"og:description\" content=\"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-11T10:24:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-21T07:07:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"How to Find GCD of two very large numbers?\",\"datePublished\":\"2020-06-11T10:24:43+00:00\",\"dateModified\":\"2023-07-21T07:07:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\"},\"wordCount\":940,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\",\"articleSection\":[\"Sorting interview programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\",\"name\":\"Programs to Find GCD of Two Numbers\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\",\"datePublished\":\"2020-06-11T10:24:43+00:00\",\"dateModified\":\"2023-07-21T07:07:47+00:00\",\"description\":\"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sorting interview programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/sorting-interview-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Find GCD of two very large numbers?\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programs to Find GCD of Two Numbers","description":"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/","og_locale":"en_US","og_type":"article","og_title":"Programs to Find GCD of Two Numbers","og_description":"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.","og_url":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-11T10:24:43+00:00","article_modified_time":"2023-07-21T07:07:47+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"How to Find GCD of two very large numbers?","datePublished":"2020-06-11T10:24:43+00:00","dateModified":"2023-07-21T07:07:47+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/"},"wordCount":940,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png","articleSection":["Sorting interview programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/","url":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/","name":"Programs to Find GCD of Two Numbers","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png","datePublished":"2020-06-11T10:24:43+00:00","dateModified":"2023-07-21T07:07:47+00:00","description":"Know how to find the GCD of two number with the algorithm and explanation. Also check program in C, C++ and Java.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645099173846-Article_340.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/gcd-of-two-very-large-numbers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Sorting interview programming","item":"https:\/\/prepbytes.com\/blog\/category\/sorting-interview-programming\/"},{"@type":"ListItem","position":3,"name":"How to Find GCD of two very large numbers?"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=1153"}],"version-history":[{"count":18,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1153\/revisions"}],"predecessor-version":[{"id":17306,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1153\/revisions\/17306"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}