Question 36 : Write a program in Java to find the Greatest Common Divisor (G.C.D.) of two numbers entered by the user :
Java Program :
import java.io.*;
class hcf
{
static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,i;
int d=1;
int c=0;
System.out.print("Enter the first number : ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter the second number : ");
b=Integer.parseInt(br.readLine());
if (a>b)
c=a;
else
c=b;
for(i=1;i<=c;i++)
{
if (a%i==0 && b%i==0 && i>d)
d=i;
}
System.out.println("The H.C.F. = "+d);
}
}
Java Program :
import java.io.*;
class hcf
{
static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,i;
int d=1;
int c=0;
System.out.print("Enter the first number : ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter the second number : ");
b=Integer.parseInt(br.readLine());
if (a>b)
c=a;
else
c=b;
for(i=1;i<=c;i++)
{
if (a%i==0 && b%i==0 && i>d)
d=i;
}
System.out.println("The H.C.F. = "+d);
}
}
Thanks for your program.I could not understand why
ReplyDeleted=1.If i want to take GCD of 6 and 12 then the common factors will be 1,2,3,6.WHY COMPUTER is taking 6 as answer when d=1 as i is greater than 1
This program is created to find out the largest common factor,not all the factors.
DeleteGCD = greatest common factor...simple.
Thanks for the program.
ReplyDeleteGood work but pretty tough for me to understand
ReplyDeleteUse the comment lines. Its is hard to understand for a begineer.... btw nice blog... :)
ReplyDeleteif (a%i==0 && b%i==0 && i>d) i need more ang wider explanation about this code.
ReplyDeletea%i == 0 && b%i==0 checks for common factors. i>d ensures it is not 1.but still we can skip i > d. as it is in loop so d would store the GCD
DeleteGood one but unnecessary processing.
ReplyDelete" if(a>b)
c=a;
else
c=b;
for(i=1;i<=c;i++) "
why not go from 1 to the (smaller number)/2 (instead of greater) since HCF can never be greater than half of the smaller number
"HCF can never be greater than half of the smaller number" is a wrong statement as HCF of 100 and 50 is 50.
ReplyDeletebut still why should we take greater of both . why not the smaller on
Deleteone
Deletethanks
ReplyDeleteHello... why is " if (a%i==0 && b%i==0) " used.
ReplyDeletewhat is the meaning of this statement...
Could anyone help me out????
Check my reply to the Anonymous 10 July 2013
DeleteThe condition checks if the number I divides both the numbers a and b or not ! A gcd should divide both the numbers !
Deletewithout knowing what is GCD..how can u write the code..First learn what GCD is & then try to code.
ReplyDeleteThanx for d program....its very helpful....@ blogger...
ReplyDeleteEk dam madherchod ho
ReplyDeleteComputing the Greatest Common Divisor (GCD) or Highest Common Factor (HCF) of two numbers Fix Device Streaming is a fundamental mathematical task.
ReplyDeleteizmir
ReplyDeleteErzurum
Diyarbakır
Tekirdağ
Ankara
RV770
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
VUİO
ankara evden eve nakliyat
ReplyDeletemalatya evden eve nakliyat
antep evden eve nakliyat
giresun evden eve nakliyat
kayseri evden eve nakliyat
DAVN6
67FAF
ReplyDeleteNevşehir Şehir İçi Nakliyat
Manisa Şehirler Arası Nakliyat
Çerkezköy Parke Ustası
Silivri Cam Balkon
Çerkezköy Ekspertiz
Kocaeli Şehirler Arası Nakliyat
Çanakkale Şehirler Arası Nakliyat
Niğde Şehirler Arası Nakliyat
Rize Lojistik
C17E1
ReplyDeleteÇerkezköy Cam Balkon
Ağrı Evden Eve Nakliyat
Çerkezköy Koltuk Kaplama
Çerkezköy Petek Temizleme
Denizli Şehir İçi Nakliyat
Yalova Lojistik
Giresun Şehir İçi Nakliyat
Kastamonu Şehirler Arası Nakliyat
Mamak Boya Ustası
56B98
ReplyDeleteVan Evden Eve Nakliyat
Isparta Şehirler Arası Nakliyat
Arbitrum Coin Hangi Borsada
Çankırı Parça Eşya Taşıma
Mercatox Güvenilir mi
Erzurum Lojistik
Isparta Parça Eşya Taşıma
Trabzon Şehirler Arası Nakliyat
Denizli Şehir İçi Nakliyat
287D4
ReplyDeleteindirim kodu %20
9F361
ReplyDeletebinance indirim
216F3
ReplyDeleteyabancı sohbet
istanbul sohbet sitesi
siirt mobil sesli sohbet
bilecik bedava sohbet chat odaları
afyon mobil sesli sohbet
canlı sohbet uygulamaları
parasız görüntülü sohbet
eskişehir görüntülü sohbet siteleri
görüntülü sohbet
5BDD9
ReplyDeleteAntalya Kadınlarla Sohbet Et
uşak rastgele sohbet
erzincan ucretsiz sohbet
telefonda sohbet
Samsun Kızlarla Rastgele Sohbet
sohbet uygulamaları
ücretsiz sohbet uygulamaları
kocaeli canlı sohbet siteleri ücretsiz
amasya telefonda kızlarla sohbet
FDC9F
ReplyDeleteBitcoin Para Kazanma
Görüntülü Sohbet
Görüntülü Sohbet
Onlyfans Takipçi Hilesi
Coin Nasıl Oynanır
Tiktok Takipçi Satın Al
Bee Coin Hangi Borsada
Loop Network Coin Hangi Borsada
Bitcoin Kazanma
429C3
ReplyDeleteBinance Neden Tercih Edilir
Kripto Para Madenciliği Siteleri
Coin Nasıl Üretilir
Bitcoin Nasıl Para Kazanılır
Ceek Coin Hangi Borsada
Spotify Dinlenme Hilesi
Twitch İzlenme Satın Al
Btcturk Borsası Güvenilir mi
Tiktok Beğeni Hilesi
598B8
ReplyDeleteDefiLlama
dexview
pancakeswap
uwulend finance
poocoin
pinksale
avalaunch
thorchain
zkswap
Respect and that i have a nifty provide: How Long Renovate House house renovation shows
ReplyDelete6B8849BA15
ReplyDeletegalxe stake
moonbeam
puffer finance
tokenfi
emoji coin
mitosis
emojicoin
rocketpool
bitget
129D092BE5
ReplyDeletetakipçi satın al ucuz
886D86CFF6
ReplyDeletetakipçi satın al türk
E9E3002B24
ReplyDeletetwitter takipçi satın al
30FF8426F5
ReplyDeleteucuz takipçi satın al
BCB3038A83
ReplyDeleteinstagram takipçi alma