HCF/GCD of Two numbers Using Python

HCF/GCD of Two numbers Using Python

CodeWithHarry

3 года назад

66,551 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@AmaanKhan-cb4rn
@AmaanKhan-cb4rn - 09.09.2023 17:54

❤❤

Ответить
@priyadubey9643
@priyadubey9643 - 22.08.2023 20:14

Sir last print ki bad ( ) me f ka ya Matlab hai

Ответить
@ashokwagle2487
@ashokwagle2487 - 16.08.2023 18:42

thank you and sorry both
I will say the reason for sorry if you reply me

Ответить
@VishalKumar-fe4wx
@VishalKumar-fe4wx - 21.06.2023 14:01

Aap achhe se Nahi samjha pate. Ho. Sirf bol dete ho Ki . I hope aap samjh gye . But students Nahi samjh pata h

Ответить
@SnehaR380
@SnehaR380 - 27.04.2023 13:59

Anyone please help me out
I think that for loop must be run in reverse order in order to get highest value otherwise code would just return I=1 because ultimately that's the first value which satisfies if condition
So I think code she be like this
def hcf(a, b):

smaller = min(a, b)
hcf = 1
for i in range(smaller,0,-1):
if a%i == 0 and b%i == 0:
hcf = i
break
return hcf

a=int(input("1st no.\n"))
b=int(input("2nd no.\n"))
print(hcf(a,b))

Ответить
@bikrampatowary9838
@bikrampatowary9838 - 28.03.2023 19:27

Last line
print(f"The gcd of these two number is {hcf}")
Why do u use' f ' key

Ответить
@jitendradashora8956
@jitendradashora8956 - 31.12.2022 14:58

def hcf(a, b):
highest_common_factor = 1
for numbers in range(2, max(a, b)):
if a % numbers == 0 and b % numbers == 0:
highest_common_factor = numbers
return highest_common_factor

Ответить
@mdafroj8546
@mdafroj8546 - 24.11.2022 19:49

Thank you sir 😎😍💝😍

Ответить
@29ibrahimsayed95
@29ibrahimsayed95 - 31.10.2022 11:05

What a lovely explanation

Ответить
@abhayparihar6802
@abhayparihar6802 - 22.10.2022 06:41

Thank you so much bhai

Ответить
@mayanksandal
@mayanksandal - 02.10.2022 11:21

I have made my own code to find the HCF/GCD using sets in python.

Ответить
@omlanguagestudiogermanlang6260
@omlanguagestudiogermanlang6260 - 08.08.2022 22:08

thanks

Ответить
@anshumanchoudhary2658
@anshumanchoudhary2658 - 22.07.2022 07:06

import math

math.gcd(40,12)

output--4

this is right for gcd or not and why we are not using this , what is difference ?

Ответить
@studies3943
@studies3943 - 01.06.2022 22:48

# Here is a simpler way:
a = int(input("Enter first:"))
b = int(input("Enter second:"))

while a % b != 0:
remainder = a % b
a = b
b = remainder

print("HCF is:", b)

Ответить
@mdfarman9786
@mdfarman9786 - 19.04.2022 13:38

I can get LCM numbers from input using python

Ответить
@devrajbhattacharya7912
@devrajbhattacharya7912 - 30.03.2022 16:50

harry bhai thanku

Ответить
@Shivanilodhi31
@Shivanilodhi31 - 15.03.2022 08:52

thank you very helpful

Ответить
@someshsangwan5302
@someshsangwan5302 - 20.11.2021 11:20

this code will shoe error if we takes input (0,b) here b is any number :
def gcd(self, A, B):
if A > B:
smaller = B
bigger=A
else:
smaller = A
bigger=B
if smaller==0:
return bigger
for i in range(1, smaller+1):


if((A % i == 0) and (B % i == 0)):
k=i

return k

Ответить
@ganeshmirisker4173
@ganeshmirisker4173 - 12.10.2021 15:38

super explanation bro ,maza aagaya

Ответить
@radhapatel8536
@radhapatel8536 - 29.09.2021 06:04

My program:
a = int(input("Enter n: " ))
l1 = [ ]
for i in range(1,a+1):
b = int(input("Enter the number: "))
l1.append(b)
c = int(min(l1))
# HCF
for i in range(1, c+1):
for numbers in l1:
if numbers%i == 0:
HCF = i

print("The HCF is " , HCF)

Ответить
@ankanderia4999
@ankanderia4999 - 24.09.2021 14:25

def GCD(a,b):
minNum = min(a,b)
maxNum = max(a,b)
rem = 1
while True:
rem = maxNum % minNum
if(rem == 0):
break
maxNum = minNum
minNum = rem

return minNum


a = int(input("Enter first number : "))
b = int(input("Enter second number : "))

result = GCD(a,b)
print(result)



# we can do like that ...!!

Ответить
@Pramod-wq7ef
@Pramod-wq7ef - 19.09.2021 20:45

Bro I have another approach, instead of looping from 1 till min number and updating hcf every time, we can loop from the min of two numbers till 1 and if we find a factor for both, we assign it as hcf and break the loop. I guess this approach might reduce the num of iterations.

Ответить
@bbtech4473
@bbtech4473 - 05.09.2021 23:37

# this is program to get gcd of two numbers
# author ♠♠kartik♠♠
lst = [1, ]
def gcd(num1 , num2):

if num2 > num1:
upperbound = num1
else:
upperbound = num2

for i in range(2 , upperbound+1):
if num1%i == 0 and num2%i == 0:
lst.append(i)
else:
pass
if _name_ == '__main__':

num1 = int(input("Enter the number1\n"))
num2 = int(input("Enter the number1\n"))
gcd(num1,num2)
print(f"Commom factor of '{num1}' and '{num2}' is {lst}")
greatest_gcd = lst[-1]
print(f"The GCD of the number '{num1}' and '{num2}' is '{greatest_gcd}'")
###########################☻R##################
import math
gcd_number = math.gcd(num1 , num2)
print(f"GCD of the number is {gcd_number}")

Ответить
@techmate8316
@techmate8316 - 04.09.2021 08:47

Please also teach block based coding

Ответить
@sudarshanmhaisdhune1039
@sudarshanmhaisdhune1039 - 21.08.2021 10:55

Great one vro!=

Ответить
@anokhautomation4453
@anokhautomation4453 - 06.08.2021 18:01

Very very useful 👍👍👍🎉🎉 Thanks

Ответить
@abhibiranje9517
@abhibiranje9517 - 01.07.2021 14:00

Sir your method is good but I have better method with time complexity
num1=int(input("Enter firat number\n"))
num2=int(input("Wnter second number\n"))

if num1>num2:
mn=num2

else:
mn=num1

for i in range(1,int((mn/2)+1)):
if num1%i==0 and num2%i==0:
hcf=i

if num2%num1==0:
hcf=num1

print(f"Your HCF is {hcf}")

Ответить
@jagmohansaini6234
@jagmohansaini6234 - 03.06.2021 19:06

Thanks sir

Ответить
@APStatus
@APStatus - 26.05.2021 06:56

# Find HCF Function
def HCF(n1,n2):
min = n1
while (True):
if (n1 % min == 0 and n2 % min == 0):
return min
min -= 1

# Main function
if __name__=="__main__":
n1 = int(input("Enter The First Number : "))
n2 = int(input("Enter The Second Number : "))
n1, n2 = min(n1, n2), max(n1, n2)
hcf = HCF(n1, n2)
print(f"{n1} & {n2} HCF is {hcf}")

# Abhishek Sharma

Ответить
@chahatvamdev4088
@chahatvamdev4088 - 20.05.2021 17:48

VERY INTERSTING SIR

Ответить
@sauravpokhrel7335
@sauravpokhrel7335 - 20.05.2021 16:42

n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))
num_list = [n1,n2]
maximum_number = max(num_list)
# print(maximum_number)
num = 0
list_n1_from_loop = []
list_n2_from_loop = []
for i in range(maximum_number):
num = num + 1
if n1 % num == 0:
list_n1_from_loop.insert(0,num)
max_list_n1 = max(list_n1_from_loop)
if n2 % num == 0:
list_n2_from_loop.insert(0,num)
max_list_n2 = max(list_n2_from_loop)

convertset_n1 = set(list_n1_from_loop)
convertset_n2 = set(list_n2_from_loop)
intersection = convertset_n1.intersection(convertset_n2)
# print(sorted(intersection))
convert_intersection_list = list(intersection)
max_intersection = max(convert_intersection_list)
print(f"The maximum and similar dividable number of {n1} and {n2} with remainder 0 is: {max_intersection}\nTHANK YOU!!!!!!!!!")

Ответить
@switchroms1659
@switchroms1659 - 20.04.2021 17:37

2 se zyada numbers ka kya karoge?

Ответить
@HARSHPIPAL
@HARSHPIPAL - 30.11.2020 17:53

Instead of updating the maximum value in for loop...we can simply run a loop in reverse direction from smaller number to 1.... correct?

Ответить
@sarcarpit
@sarcarpit - 07.10.2020 12:48

sir i tried it myself and i came up with a different logic . Please check if I m correct?
a = int(input("Enter the first number : "))
b = int(input("Enter the second number : "))
hcf=1
i=min(a,b)
while i>1:
if a%i==0 and b%i ==0:
hcf=i
break
i=i-1

print(f"The HCF of {a} & {b} is : {hcf} ")
I have learnt Python from your channel only!!
you're the BEST !!

Ответить
@RahulSingh911
@RahulSingh911 - 15.09.2020 20:34

Available with 3.9 and dict union ... beautiful feature, faster than previous

Ответить
@vaibhav3852
@vaibhav3852 - 12.09.2020 10:02

guys u dont need to to take out the max number. you can just range from 1 to any of the two number and it will still work. example
for i in range(1, num1):
if num1%i==0 and num2%i==0:
hcf=i;

Ответить
@digitalkashur1538
@digitalkashur1538 - 12.09.2020 06:17

Bro we r nt getting it properly because we r nt able to see this lecture from outside home because of lecture is on dark display so plzz attention in next lecture

Ответить
@sonisinha8912
@sonisinha8912 - 12.09.2020 05:40

Sir please make video series on unity game engine for game development and for game programming.....plz reply

Ответить
@prince6727
@prince6727 - 12.09.2020 03:21

Sir Hardy Ramanujan number ke liye program banayi please

Ответить
@RohanSharma-tf3rw
@RohanSharma-tf3rw - 11.09.2020 18:21

Harry bhai laptop bataiye hp 15s du2067tu plz bataiye kaisa h student or coding ke liye

Ответить
@avibro7454
@avibro7454 - 11.09.2020 18:05

Plz make a advance video for problem solving and making psudo code plzplzplzplzllz🙏🙏🙏🙏🙏

Ответить
@avibro7454
@avibro7454 - 11.09.2020 17:29

Plz like everyone

Ответить
@nareshgoyal1994
@nareshgoyal1994 - 11.09.2020 13:51

Harry bhai GCD ko Euclid division wale method se krke samjha do bhai.. Bahut problem aa rahi hai smjhne me

Ответить
@PrakashKumar-ky5uo
@PrakashKumar-ky5uo - 11.09.2020 12:46

Bhaiya by learning python how much salary job we can get .

Ответить
@prozeangamer4726
@prozeangamer4726 - 11.09.2020 12:24

Herry bhai you are super 😎😎😎

Ответить
@ryxnroger6030
@ryxnroger6030 - 11.09.2020 12:12

Bhai ek baar heart de do

Ответить
@devashishprajapati9860
@devashishprajapati9860 - 11.09.2020 12:10

Sir ji e commerces website per series banao

Ответить