Program to Find GCD Two Numbers using For loop
- Write a program to Find GCD Two Numbers using For loop in C
- Write a program to Find GCD Two Numbers using For loop in C++
- Write a program to Find GCD Two Numbers using For loop in Python
- Write a program to Find GCD Two Numbers using For loop in PHP
- Write a program to Find GCD Two Numbers using For loop in Java
- Write a program to Find GCD Two Numbers using For loop in Java Script
- Write a program to Find GCD Two Numbers using For loop in C#
Explanation:
Using a for loop, the following reasoning may be used to get the GCD (Greatest Common Divisor) of two numbers:
- The GCD of two numbers is the largest number that divides both numbers without leaving a remainder.
- Start from 1 and iterate up to the smaller of the two numbers.
- Check if the current number divides both numbers perfectly.
- Keep track of the largest number that satisfies the condition.
Program to Find GCD Two Numbers using For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int main() { int a, b, gcd = 1; printf("Enter two numbers: "); scanf("%d %d", &a, &b); int min = (a < b) ? a : b; for (int i = 1; i <= min; i++) { if (a % i == 0 && b % i == 0) { gcd = i; } } printf("GCD of %d and %d is %d\n", a, b, gcd); return 0; }
#include <iostream> using namespace std; int main() { int a, b, gcd = 1; cout << "Enter two numbers: "; cin >> a >> b; int min = (a < b) ? a : b; for (int i = 1; i <= min; i++) { if (a % i == 0 && b % i == 0) { gcd = i; } } cout << "GCD of " << a << " and " << b << " is " << gcd << endl; return 0; }
a = int(input("Enter the first number: ")) b = int(input("Enter the second number: ")) gcd = 1 min_val = min(a, b) for i in range(1, min_val + 1): if a % i == 0 and b % i == 0: gcd = i print(f"GCD of {a} and {b} is {gcd}")
<?php $a = (int)readline("Enter the first number: "); $b = (int)readline("Enter the second number: "); $gcd = 1; $min = min($a, $b); for ($i = 1; $i <= $min; $i++) { if ($a % $i == 0 && $b % $i == 0) { $gcd = $i; } } echo "GCD of $a and $b is $gcd\n"; ?>
import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the first number: "); int a = sc.nextInt(); System.out.print("Enter the second number: "); int b = sc.nextInt(); int gcd = 1; int min = Math.min(a, b); for (int i = 1; i <= min; i++) { if (a % i == 0 && b % i == 0) { gcd = i; } } System.out.println("GCD of " + a + " and " + b + " is " + gcd); sc.close(); } }
let a = parseInt(prompt("Enter the first number: ")); let b = parseInt(prompt("Enter the second number: ")); let gcd = 1; let min = Math.min(a, b); for (let i = 1; i <= min; i++) { if (a % i === 0 && b % i === 0) { gcd = i; } } console.log(`GCD of ${a} and ${b} is ${gcd}`);
using System; class Program { static void Main() { Console.Write("Enter the first number: "); int a = int.Parse(Console.ReadLine()); Console.Write("Enter the second number: "); int b = int.Parse(Console.ReadLine()); int gcd = 1; int min = Math.Min(a, b); for (int i = 1; i <= min; i++) { if (a % i == 0 && b % i == 0) { gcd = i; } } Console.WriteLine($"GCD of {a} and {b} is {gcd}"); } }