adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to find HCF of two numbers using prime factorization method

Program in C and C++ to find HCF of two numbers using prime factorization method


C Program
#include<stdio.h>
#include<conio.h
 int HCF(int a,int b)
  {
    int i,hcf=1;
    for(i=2;i<=a&&i<=b;)
    {
        if(a%i==0&&b%i==0)
        {
            hcf*=i;
            a/=i;
            b/=i;
        }
        else
             i++;
     }
    return hcf;
  }
 int main()
  {

      int a,b,yes;
      start:
        printf("Enter two number :-");
        scanf("%d%d",&a,&b);
        printf("HCF = %d",HCF(a,b));
        printf("\nAgain calculate (1):- ");
        scanf("%d",&yes);
        if(yes==1)
         goto start;
      return 0;
  }
C++ Program
#include<iostream>
#include<conio.h>
 using namespace std;
 int HCF(int a,int b)
  {
    int i,hcf=1;
    for(i=2;i<=a&&i<=b;)
    {
        if(a%i==0&&b%i==0)
        {
            hcf*=i;
            a/=i;
            b/=i;
        }
        else
             i++;
     }
    return hcf;
  }
 int main()
  {
      int a,b,c,yes;
      start:
        cout<<"Enter two number :-";
       cin>>a>>b;
        cout<<"HCF = "<<HCF(a,b);
        cout<<"\nAgain calculate (1):- ";
        cin>>yes;
        if(yes==1)
         goto start;
      return 0;
  }
Output :-





Related Programs

Post a Comment

0 Comments