Computer Programming C Laboratory: Basic C Programs

Write a C program to find the GCD of two given numbers

Solving simple scientific and statistical problems - Basic C Program

Solving simple scientific and statistical problems


Write a program to find the Greatest Common Division (GCD) (Highest Common Factor) of two given numbers.

Let the numbers be I and J, and I be the bigger number and J be the smaller number. Suppose, we want to find the GCD(HCF) of two numbers 36 and 54. The bigger number 54 must assigned to I and 36 to J.

 I=54, J=36

Divide I by J, the remainder when 54 is divided by 36 is 18, So, I=18, J=36

Divide I by J and find the remainder, when 36 is divided by 18, the remainder is 0, so, I=0, J=18.

Interchanging I and J we get I=18, J=0. Here J=0, hence I is the GCD, so, GCD=18. The Greatest Common Division of 54 and 36 is 18.

/* Program to find ged of two numbers */

#include <stdio.h>

main()

{ /* Local definitions */

    int i,j,k;

    printf("Enter Two numbers.....");

    scanf("%d %d", &i, &j);

    if(j>i)

    {

        k=i;

        i=j;

        j=k;

    } /* if */

step1:

    if(j==0)

        goto step2;

    else

    {

        k=i%j;

        i=j;

        j=k;

    } /* if else */

step2:

    printf("The GCD is..... %d",j);

} /* main */

OUTPUT

Enter Two numbers.....54 36

The GCD is.. 18

EXPLANATION: Read the values i and j, the if statement checks the condition j>i, if condition is true, then the value of i and j is interchanged using third variable k. Otherwise take modular division between i, j. If k becomes zero then prints the value of GCD through the variable j as a output.

 

Computer Programming C Laboratory: Basic C Programs : Tag: C Programming : - Write a C program to find the GCD of two given numbers


Computer Programming C Laboratory: Basic C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation