Program to Find GCD Two Numbers using While loop
- Write a program to Find GCD Two Numbers using While loop in C
- Write a program to Find GCD Two Numbers using While loop in C++
- Write a program to Find GCD Two Numbers using While loop in Python
- Write a program to Find GCD Two Numbers using While loop in PHP
- Write a program to Find GCD Two Numbers using While loop in Java
- Write a program to Find GCD Two Numbers using While loop in Java Script
- Write a program to Find GCD Two Numbers using While 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 While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
printf("GCD is %d\n", a);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
cout << "GCD is " << a << endl;
return 0;
}
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
while b != 0:
a, b = b, a % b
print(f"GCD is {a}")
<?php
$a = intval(readline("Enter first number: "));
$b = intval(readline("Enter second number: "));
while ($b != 0) {
$temp = $b;
$b = $a % $b;
$a = $temp;
}
echo "GCD is $a\n";
?>
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println("GCD is " + a);
}
}
let a = parseInt(prompt("Enter first number: "));
let b = parseInt(prompt("Enter second number: "));
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
console.log("GCD is " + a);
using System;
class Program {
static void Main() {
Console.Write("Enter first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
int b = int.Parse(Console.ReadLine());
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
Console.WriteLine("GCD is " + a);
}
}