The following code shows how to Find the Greatest Common Divisor (GCD) of Two Numbers in C.
// Find Greatest Common Divisor of Two Numbers
#include <stdio.h>
int main()
{
int first, second, i, gcd, last;
printf("Enter First Number: ");
scanf("%d", &first);
printf("Enter second Number: ");
scanf("%d", &second);
if(first<second)
last=second/2;
else
last=first/2;
for(i=last;i>=1;i--)
{
if((first%i==0) && (second%i==0))
{
gcd=i;
break;
}
}
printf("GCD of %d and %d is %d", first, second, gcd);
return 0;
}
Output
Enter First Number: 945
Enter second Number: 30
GCD of 945 and 30 is 15
Further Reading
50+ C Programming Interview Questions and Answers
C Program to Find Factorial of a Number
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML