Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Program to find GCD or HCF of two numbers javascript

//<script>
// Javascript program to find GCD of two numbers
var dp = new Array(1001);
 
// Loop to create 2D array using 1D array
for (var i = 0; i < dp.length; i++) {
    dp[i] = new Array(1001);
}
 
// Function to return gcd of a and b
function gcd(a, b)
{
 
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
 
    // base case
    if (a == b)
        return a;
     
    // if a value is already
    // present in dp
    if(dp[a][b] != -1)
        return dp[a][b];
 
    // a is greater
    if (a > b)
        dp[a][b] = gcd(a-b, b);
     
    // b is greater
    else
        dp[a][b] = gcd(a, b-a);
     
    // return dp
    return dp[a][b];
}
 
// Driver program to test above function
    let a = 98, b = 56;
     
    for(let i = 0; i < 1001; i++) {
        for(let j = 0; j < 1001; j++) {
            dp[i][j] = -1;
        }
    }
    document.write("GCD of "+ a + " and " + b + " is " + gcd(a, b));
     
// This code is contributed by Samim Hossain Mondal
 
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: react double render 
Javascript :: jquery in javascript 
Javascript :: React Native drawer navigation screen header title and buttons 
Javascript :: how to open a tcp connection in javascript 
Javascript :: javascript addeventlistener click only works once 
Javascript :: how to use axios 
Javascript :: javascript pipe function 
Javascript :: js for i in html collection 
Javascript :: NextJS add lang attribute to HTML tag 
Javascript :: jquery dom traversal parent 
Javascript :: how to write a range of numbers in if condition js 
Javascript :: case switch javascript 
Javascript :: javscript loop array 
Javascript :: how ot send user agent in nodejs https header 
Javascript :: exclude vales from array in js 
Javascript :: obtener primer elemento de un array javascript 
Javascript :: react native radio buttons 
Javascript :: two days before in moment 
Javascript :: inline style to change background color js 
Javascript :: redux-logger 
Javascript :: what is form data in javascript 
Javascript :: e parameter in javascript 
Javascript :: what is axios used for 
Javascript :: js object to c# object 
Javascript :: javascript strings are immutable 
Javascript :: prop types in react 
Javascript :: display fetch response js 
Javascript :: create random password 
Javascript :: difference between single quotes and double quotes in javascript 
Javascript :: how to get child element in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =