C Program to find GCD of two Integers
Contents
Introduction
The C programming language is a procedural programming language. Between 1969 and 1973, Dennis Ritchie worked on the project. It was created primarily as a system programming language for developing operating systems.
In order to understand the code given below you’ve got to have prior knowledge and or understanding of the following topics:
If you have a sound knowledge and understanding of what the functions of the concepts are then there will be no difficulty in understanding the given codes below. With all that said lets get coding!
Why Learn C Programming Language?
Because it mixes the characteristics of high-level languages with the functionalism of assembly language, C is often referred to as a middle-level computer language. The manipulation of bits, bytes, and addresses in C gives the programmer more control over how the program will behave and more direct access to the underlying hardware mechanisms.
Working programmers influenced, influenced, and field-tested C. As a result, C provides the programmer with exactly what he or she want. C++ is a more advanced version of the C programming language. C++ incorporates all of the features of C, as well as support for object-oriented programming (OOP). Furthermore, C++ includes numerous enhancements and features that make it a “better C,” regardless of whether or not it is used for object-oriented programming.
GCD of Two Integers
The greatest common divisor (GCD) is the largest positive integer that is a common divisor for all positive integers in a given set. The greatest common factor (GCF) and highest common factor (HCF) is another name for the greatest common divisor (GCD). We’ll go through how to find the greatest common divisor in depth in this session.
The greatest common divisor of a pair of positive integers (a, b) is defined as the greatest positive number that is a common factor of both positive integers (a, b). The least positive integer common to any two integers is always 1, hence the GCD of any two numbers is never negative or zero.
To find the GCF of a pair of numbers (say, 35 and 15):
a) Make a list of each number’s prime factors.
b) Identify the components that are present in both numbers.
c) To find the Greatest Common Divisor (GCD), multiply all of the common elements from step b.
Each number’s prime factors will be determined. As a result,
35 = 5 x 7
15 = 5 x 3
As a result,
There is only one thing that they all have in common. That brings us to number five. As a result, GCD = 5.
GCD of Two Integers using C
The best way to learn is to do. Hence, what better way to learn C programming language (if you know the basics) than to code your first mini project. In this tutorial we are going to build a GCD finder of two integers in c programming language. Keep in mind this is a mini project for absolute beginners and intermediate programmers as well.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int num1, num2, i, gcd;
printf("Please Enter Your two integer number: ");
scanf("%d %d", &num1, &num2);
for(i=1; i <= num1 && i <= num2; ++i)
{
// First Check whether i is factor of both integers or not
if(num1%i==0 && num2%i==0)
gcd = i;
}
printf("Here is the GCD of %d and %d is %d", num1, num2, gcd);
return 0;
}