
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to find GCD of numbers using non-recursive function
Problem
Find the greatest common divisor (GCD) for the given two numbers by using the non-recursive function.
Solution
It is explained below how to find the greatest common divisor (GCD) for the given two numbers by using the non-recursive function.
Algorithm
Refer an algorithm given below to find the greatest common divisor (GCD) for the given two numbers by using the non-recursive function.
Step 1 − Start
Step 2 − Read the integers a and b
Step 3 − Call the function G=GCD(a,b) step 6
Step 4 − Print G value
Step 5 − Stop
Step 6 − Called function: GCD(a,b)
a. Initialize the i=1, j, remainder b. Remainder=i-(i/j*j) c. Remainder=0 return j else goto step 4 d. GCD(G,remainder) return to main program
Flowchart
Given below is a flowchart for an algorithm to find the greatest common divisor (GCD) for the given two numbers by using the non-recursive function.
Example
Following is the C program to find the greatest common divisor (GCD) for the given two numbers by using the non-recursive function −
#include<stdio.h> #include<conio.h> #include<math.h> int gcdnonR(int i,int j){ int rem; rem=i-(i/j*j); if(rem==0) return j; else gcdnonR(j,rem); } void main(){ int a,b; printf("enter the two numbers:"); scanf("%d%d",&a,&b); printf("GCD of %d",gcdnonR(a,b)); getch(); }
Output
When the above program is executed, it produces the following result −
enter the two numbers:10 30 GCD of 10
- Related Articles
- C program to find GCD of numbers using recursive function
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Haskell Program to find the GCD using library function
- Program to find GCD of floating point numbers in C++
- Recursive program to print formula for GCD of n integers in C++
- Java Program to Find GCD of two Numbers
- Swift Program to Find GCD of two Numbers
- Kotlin Program to Find GCD of two Numbers
- C++ Program to Find the GCD and LCM of n Numbers
- Program to find GCD or HCF of two numbers in C++
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- Haskell Program to find the GCD of two given numbers using recursion
- Swift program to find the GCD of two given numbers using recursion
- C Program to reverse a given number using Recursive function
- Haskell program to find the gcd of two numbers
