Program Discussion :: Basics
314 / 279
Write a program using command line arguments to input two number and find the GCD/HCF of the number.
Condition:
If the entered number is less than 2 print the message and exit.
Answer:
#include
int main(int a,char *b[])
{
int num1,num2,min,i,gcd=1;
if(a!=3){
printf("Enter two argument\n");
exit(1);
}
num1=atoi(b[1]);
num2=atoi(b[2]);
min=(no1
Asked In ::
Language:
Sumit
27 Oct, 2017 12:42 AM
#include<stdio.h>
int main(int a,char *b[])
{
int num1,num2,min,i,gcd=1;
if(a!=3){
printf("Enter two argument\n");
exit(1);
}
num1=atoi(b[1]);
num2=atoi(b[2]);
min=(no1<no2)?no1:no2;
for(i=1;i<=min;i++)
{
if((num1%i)==0 && (num2%i)==0)
gcd=i;
}
printf("GCD of two number %d",gcd);
return 0;
}
Language:
Om Prakesh
28 Oct, 2017 4:48 PM
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int m,x,y,i;
if (argc == 1 || argc > 3)
{
printf("Enter Two Number\r\n");
exit(0);
}
x=atoi(argv[1]);
y=atoi(argv[2]);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d\r\n",i) ;
break;
}
}
return 0;
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class GCD
{
public static void main(String []args)
{
int r=0;
System.out.println("Enter 2 numbers: ");
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int x=(a>b)?a:b;
int y=(a<b)?a:b;
r=y;
while(x%y!=0)
{
r=x%y;
x=y;
y=r;
}
System.out.println("GCD of the 2 numbers is: "+r);
}
}