C++ Program to Find GCD - Programiz

文章推薦指數: 80 %
投票人數:10人

Example 2: Find GCD/HCF using while loop ... In the above program, the smaller number is subtracted from the larger number and that number is stored in place of ... CourseIndex ExploreProgramiz Python JavaScript C C++ Java Kotlin Swift C# DSA StartLearningC++ ExploreC++Examples PopularTutorials C++if...elseStatement C++forLoop ArraysinC++ StringsinC++ C++Class&Objects PopularExamples Createasimplecalculator Checkprimenumber PrinttheFibonaccisequence Checkifanumberispalindromeornot Programtomultiplymatrix ReferenceMaterials iostream cmath cstring ctime Viewall Python JavaScript C C++ Java Kotlin Swift C# DSA StartLearningC++ PopularTutorials C++if...elseStatement C++forLoop ArraysinC++ StringsinC++ C++Class&Objects Viewalltutorials ReferenceMaterials iostream cmath cstring ctime Viewall Python JavaScript C C++ Java Kotlin ExploreC++Examples PopularExamples Createasimplecalculator Checkprimenumber PrinttheFibonaccisequence Checkifanumberispalindromeornot Programtomultiplymatrix Viewallexamples C++Examples CheckWhetherNumberisEvenorOdd CheckWhetheracharacterisVowelorConsonant. FindLargestNumberAmongThreeNumbers FindAllRootsofaQuadraticEquation CalculateSumofNaturalNumbers CheckLeapYear FindFactorial GenerateMultiplicationTable RelatedTopics FindLCM CheckWhetheraNumbercanbeExpressasSumofTwoPrimeNumbers C++whileanddo...whileLoop CheckWhetheraNumberisPrimeorNot C++forLoop DisplayPrimeNumbersBetweenTwoIntervals C++ProgramtoFindGCD ExamplesondifferentwaystocalculateGCDoftwointegers(forbothpositiveandnegativeintegers)usingloopsanddecisionmakingstatements. Tounderstandthisexample,youshouldhavetheknowledgeofthefollowingC++programming topics:C++if,if...elseandNestedif...elseC++forLoopC++whileanddo...whileLoop ThelargestintegerwhichcanperfectlydividetwointegersisknownasGCDorHCFofthosetwonumbers. Forexample,theGCDof 4 and 10 is2 sinceitisthelargestintegerthatcandivideboth 4 and 10.  Example:1.FindHCF/GCDusingfor loop #include usingnamespacestd; intmain(){ intn1,n2,hcf; cout<>n1>>n2; //swappingvariablesn1andn2ifn2isgreaterthann1. if(n2>n1){ inttemp=n2; n2=n1; n1=temp; } for(inti=1;i<=n2;++i){ if(n1%i==0&&n2%i==0){ hcf=i; } } cout< usingnamespacestd; intmain(){ intn1,n2; cout<>n1>>n2; while(n1!=n2){ if(n1>n2) n1-=n2; else n2-=n1; } cout<n2 n1-=n2 n2-=n1 n1!=n2 16 76 false - 60 true 16 60 false - 44 true 16 44 false - 28 true 16 28 false - 12 true 16 12 true 4 - true 4 12 false - 8 true 4 8 false - 4 false Here,theloopterminateswhenn1!=n2becomesfalse. Afterthefinaliterationoftheloop,n1=n2=4.ThisisthevalueoftheGCD/HCF sincethisisthegreatestnumberthatcandivideboth16and76. WecanalsofindtheGCDoftwonumbersusingfunctionrecursion. Shareon: Didyoufindthisarticlehelpful? Sorryaboutthat. Howcanweimproveit? Feedback* Leavethisfieldblank RelatedExamplesC++ExampleFindLCMC++ExampleCheckWhetheraNumbercanbeExpressasSumofTwoPrimeNumbersC++ExampleFindLargestNumberAmongThreeNumbersC++ExampleCheckWhetheraNumberisPrimeorNot GetApp GetC++App



請為這篇文章評分?