Coursera's Algorithmic toolbox assignment solutions( Greatest Common Divisor, Least Common Multiple ) :

Problem 3: Greatest Common Divisor

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
int main()
{
int x,y;
cin>>x>>y;
cout<<gcd(x,y);
}

This code is simple. There's no need for any explanation.
 

 

 

 

Problem 4: Least Common Multiple

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}
int main()
{
long long x,y;
cin>>x>>y;
cout<<(long long)((x*y)/gcd(x,y)); //since LCM = x*y/GCD(x,y)
}

This code is simple. There's no need for any explanation.
 

 

 

Guys , if you have any queries related to a question or need more explanation, comment down below! 

Comments

  1. This blog post was extremely informative and easy to understand. I really liked the way the topic was explained with clear examples and practical insights that can benefit many students. Academic subjects like law often require detailed research and proper understanding, which is why many learners search for reliable services such as law assignments help to manage their studies effectively. Your article provides valuable guidance and keeps readers engaged from start to finish. Thank you for sharing such high-quality and useful content with your audience.

    ReplyDelete

Post a Comment

Popular posts from this blog

HackerEarth's basic programming solutions( Ali and Helping innocent people, Duration, Book of Potion making ) :

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Life the Universe and Everything, Ladderophilia, Char Sum ) :