English

Write a program in C++ to find Greatest Common Devisor (GCD) of two inputted number. - Computer Science 1

Advertisements
Advertisements

Question

Write a program in C++ to find Greatest Common Divisor (GCD) of two inputted number.

Code Writing

Solution

#include<iostream·h> 
void main()
{
        int a, b, i; 
        count <<"Enter Two Numbers";
        cin >>a>>b; 
        int gcd = 1, min;
        if (a > b)
        {
               min = a;
        }
        else
               min = b;
        for (i = 1, i<=min; i++)
        {
        if (a % i == 0 && b % i == 0)
              gcd = i;
        }
              cout <<"GCD="<<gcd;
}
shaalaa.com
  Is there an error in this question or solution?
2023-2024 (March) Official
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×