Subscribe

RSS Feed (xml)

Wednesday, July 29, 2009

Write a C Program to find HCF (GCD) between two numbers using Goto statement

#include< stdio.h>
#include< conio.h>
int main( )
{
int a, b, temp, rem,
clrscr( );
printf("Enter the required two input values:");
scanf(" %d%d",&a, &b);
if(a==0)
{ printf("\nHCF of number is : %d", b );
goto last;
}
if(b==0)
{ printf("\nHCF of number is : %d", a );
goto last;
}

// Here i'm taking a empty 'for' loop
// means no initial, final and incre/decrement

for( ; ; )
{
rem=a %b;
if(rem==0) break;
a= b;
b= rem;
}
printf("\nHCF of Number is : %d", b);

last:
getch( );
return 0;
}

Output:-
Enter the required two input values: 49
47
HCF of number is: 1

9 comments:

  1. I have copied your entire program into my software when i compile it and entered the two input values
    49 47 i was getting the answer as 1 ,,,... plz tell me why is it so.

    ReplyDelete
  2. can i do it using for loop?????

    ReplyDelete
  3. HEy dude.. try working the program for numbers 10 and 12

    ReplyDelete
  4. #include
    #include
    void main()
    {
    clrscr();
    int a,b,c,z,ans;
    printf("enter any value");
    scanf("%d",&a);
    printf("enter second value");
    scanf("%d",&b);
    printf("enter third value");
    scanf("%d",&c);
    printf("the common factors of these values are\n");
    ans=1;
    for(;ans<=a+b+c;ans++)
    {
    if(a%ans==0&&b%ans==0&&c%ans==0)
    {
    printf("%d\n",ans);
    z=ans;
    }
    }
    printf("the highest common factor is %d",z);
    getch();
    }

    ReplyDelete
  5. easy program to find HCF (GCD) between two numbers without using GOTO

    #include
    int main()
    {
    int a,b,c,d,temp,j,hcf;
    printf("enter two numbers\n");
    scanf("%d%d",&a,&b);
    if(a>b)
    temp=a;
    else
    temp=b;


    for(j=1;j<=temp;j++)
    {
    c = a%j;
    d = b%j;
    if(c==0 && d==0)
    hcf=j;
    }

    printf("HCF is %d",hcf);
    return 0;
    }

    ReplyDelete
  6. Simple HCF...program

    http://123techguide.blogspot.com/2012/01/find-hcf-using-c-program.html

    ReplyDelete
  7. easy program to find HCF (GCD) between two numbers without using GOTO
    #include
    main()
    {
    int a,b,x,y;
    printf("enter two numbers\n");
    scanf("%d%d",&a,&b);
    x=a,y=b;
    while(x!=0)
    {
    if(x>y)
    x=x-y;
    else
    y=y-x;
    }
    printf("HCF of %d and %d is %d",a,b,x);
    }

    ReplyDelete