gcd of two numbers in c | C Programming by TechWithCode

 GCD of Two Numbers in C

Hello and Welcome to www.TechWithCode.com

The World of Computer Science and Technology


Today we will learn about how to write a C Program to find the GCD of Two Numbers. Before moving forward, we must have knowledge about what GCD is?

What is GCD?

GCD stands for  Greatest Common Divisor

GCD of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For two integers x, y, the greatest common divisor of x and y is denoted {gcd(x,y)}. 

For example, the GCD of 8 and 12 is 4, that is, {gcd(8,12)=4}


In order to find GCD, two scenarios occurred,

  1. Find GCD of two numbers

  2. Find GCD of Multiple Numbers (n Numbers)

Today we will write c programming code for both the scenarios one by one

To understand these Programs, you should have knowledge of the following C programming topics:

  1. C Functions
  2. C User-defined functions
  3. C Recursion
  4. C Lopping
  

1. Find GCD of two numbers

Problem Statement⤵️

Write a C program that calculates the GCD of two numbers, this number should be taken as input from the keyboard.

Source code⤵️



#include <stdio.h>
int hcf(int n1, int n2);
int main() {
    int n1, n2;
    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);
    printf("G.C.D of %d and %d = %d", n1, n2, hcf(n1, n2));
    return 0;
}

int hcf(int n1, int n2) {
    if (n2 != 0)
        return hcf(n2, n1 % n2);
    else
        return n1;
}

output⤵️

gcd of two numbers,c program to find gcd of two numbers,program to find gcd of two numbers in c,gcd of two numbers in c,gcd program in c,gcd,c program to find gcd of two numbers using recursion,program to find gcd of two numbers using recursion,c program to find gcd of n numbers,gcd of two numbers in c program,gcd of two numbers in c language,gcd and lcm of two numbers in c,how to find gcd of two numbers in c,wap to find gcd of two numbers in c,find lcm and gcd of two numbers in c



2. Find GCD of Multiple Numbers (n Numbers)

Problem Statement⤵️

Write a C program that calculates the GCD of Multiple Numbers or N numbers, this number should be taken as input from the keyboard.

Source code⤵️


  
  #include<stdio.h>

int main()
{
   
    int x, y =- 1;
    printf("Enter numbers. To Calculate Enter 0\n");
    while(1)    // infinite loop to take input
    {
        scanf("%d", &x);
        if(x < 1)
            break;
        else if(y ==- 1)    // only 1 number entered, its GCD is itself
            y = x;
        else if(x < y)
            y = gcd(x, y);
        else
            y = gcd(y, x);
    }
    printf("\n\n\nGCD of all the entered number is: %d", y);
    
    return 0;
}

// GCD of 2 numbers is calculated at a time
int gcd(int a, int b) 
{
    int i;
    /*
        a is the smallest of the two numbers 
        of which GCD is to be calculated
    */
    for(i = a; i >= 1; i--) 
    {
        // Greatest number that divides both the numbers
        if(a%i == 0 && b%i == 0) 
            break;  // exits the loop
    }
    return i;
}
  
  

output⤵️



gcd of n numbers in c,gcd of two numbers in c,gcd of n numbers in python,gcd of two numbers in c program,gcd of n numbers,c program to find gcd of n numbers,program to find gcd of two numbers in c,gcd of two numbers in c using recursion,sum of squares of n natural numbers in c,sum of n natural numbers in c using recursion,python gcd of n number,find sum of n number in c programme,sum of numbers from 1 to 100 in c,c program to find gcd of two numbers,gcd of two numbers





1 comment:

  1. Completed as separate transactions, the CCK household of firms added OSDA, a high-tech quick-turn prototype PCBA shop in Milford, CT properly as|in addition to} Argo EMS, an agile LVHM business in Clinton, CT. The firm launched its second program, devoted to press brake operation, about two Shower Curtains For Kids Bathrooms years in the past. Like in the machining program, the three levels of press brake operator classifications comply with that chef metaphor.

    ReplyDelete

Do not add any link in the comments.
For backlink, contact us.

Powered by Blogger.