0% found this document useful (0 votes)
16 views4 pages

Exp_007

This document is a lab manual for an experiment in the First Year Engineering department at ISBM College, focusing on writing a C program to compute the smallest divisor and the Greatest Common Divisor (GCD) of two user-input numbers. It outlines the objectives, theoretical concepts, steps involved in the program, and provides a complete C code example. The experiment emphasizes the use of loops and conditional statements without employing functions.

Uploaded by

kurhadeanjali54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Exp_007

This document is a lab manual for an experiment in the First Year Engineering department at ISBM College, focusing on writing a C program to compute the smallest divisor and the Greatest Common Divisor (GCD) of two user-input numbers. It outlines the objectives, theoretical concepts, steps involved in the program, and provides a complete C code example. The experiment emphasizes the use of loops and conditional statements without employing functions.

Uploaded by

kurhadeanjali54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Peoples Empowerment Group

ISBM COLLEGE OF ENGINEERING, NANDE, PUNE


DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25

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:

1. Start the Program:


o Initialize the program by including the necessary header files.
o Declare variables to store the input numbers and the results.
2. Accept User Input:

oPrompt the user to input two positive integers.


oStore the input values in the declared variables.
3. Compute the Smallest Divisor:

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 Start with the smallest of the two input numbers.


o Iterate downwards from this number to 1, checking if both numbers are divisible
by the current number.
Peoples Empowerment Group
ISBM COLLEGE OF ENGINEERING, NANDE, PUNE
DEPARTMENT OF FIRST YEAR ENGINEERING
Academic Year 2024-25
o The first number that divides both is the GCD.
5. Display the Results:

o Print the smallest divisor and the GCD of the two numbers.
6. End the Program:

o Successfully terminate the program

C Program :

#include <stdio.h>

int main() {

int num1, num2, i, smallestDiv1, smallestDiv2, gcd;

// Accept user input

printf("Enter two positive integers: ");

scanf("%d %d", &num1, &num2);

// Compute smallest divisor for num1

for (i = 2; i <= num1; i++) {

if (num1 % i == 0) {

smallestDiv1 = i;

break;

// Compute smallest divisor for num2

for (i = 2; i <= num2; i++) {

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

for (i = (num1 < num2 ? num1 : num2); i >= 1; i--) {

if (num1 % i == 0 && num2 % i == 0) {

gcd = i;

break;

// Display results

printf("Smallest divisor of %d is: %d\n", num1, smallestDiv1);

printf("Smallest divisor of %d is: %d\n", num2, smallestDiv2);

printf("GCD of %d and %d is: %d\n", num1, num2, gcd);

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy