Exp_007
Exp_007
LAB MANUAL
Experiment/Program 7
To accept two numbers from user and compute smallest divisor and Greatest Common
Divisor of these two numbers.
Objective:
To write a C program that accepts two numbers from the user and computes both the smallest di-
visor and the greatest common divisor (GCD) without using functions.
Theory:
Smallest Divisor: The smallest divisor of a number is the smallest positive integer
greater than 1 that divides the number without leaving a remainder.
Greatest Common Divisor (GCD): The GCD of two numbers is the largest positive in-
teger that divides both numbers without leaving a remainder. The GCD is also known as
the greatest common factor (GCF).
Steps Involved:
oFor each number, start checking divisibility from 2 up to the number itself.
oThe first number that divides the input number without leaving a remainder is the
smallest divisor.
o If no divisor other than 1 and the number itself is found, the smallest divisor is the
number itself (indicating the number is prime).
4. Compute the GCD:
o Print the smallest divisor and the GCD of the two numbers.
6. End the Program:
C Program :
#include <stdio.h>
int main() {
if (num1 % i == 0) {
smallestDiv1 = i;
break;
if (num2 % i == 0) {
Peoples Empowerment Group
ISBM COLLEGE OF ENGINEERING, NANDE, PUNE
DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25
smallestDiv2 = i;
break;
// Compute GCD
gcd = i;
break;
// Display results
return 0;
Explanation:
1. User Input:
Peoples Empowerment Group
ISBM COLLEGE OF ENGINEERING, NANDE, PUNE
DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25
o The program prompts the user to enter two integers.
o These integers are stored in the variables num1 and num2.
2. Smallest Divisor Calculation:
oThe program checks each number starting from 2 to find the smallest divisor for
both num1 and num2.
o The first divisor found is stored in smallestDiv1 and smallestDiv2, respect-
ively.
3. GCD Calculation:
o The GCD is calculated by checking divisibility starting from the smaller of the
two numbers and moving downwards.
o The first common divisor found is the GCD.
4. Output:
o The smallest divisors and the GCD are printed to the console.
Conclusion:
The experiment demonstrates how to calculate the smallest divisor and GCD of two numbers us-
ing basic loops and conditional statements in C without using functions.