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 three numbers using prime factorization method

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


C Program
#include<stdio.h>
#include<conio.h
 int HCF(int a,int b,int c)
  {
    int i,hcf=1;
    for(i=2;i<=a&&i<=b&&i<=c;)
    {
        if(a%i==0&&b%i==0&&c%i==0)
        {
            hcf*=i;
            a/=i;
            b/=i;
            c/=i;
        }
        else
             i++;
     }
    return hcf;
  }
 int main()
  {
      int a,b,c,yes;
      start:
        printf("Enter three numbers :-");
        scanf("%d%d%d",&a,&b,&c);
        printf("HCF = %d",HCF(a,b,c));
        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 c)
  {
    int i,hcf=1;
    for(i=2;i<=a&&i<=b&&i<=c;)
    {
        if(a%i==0&&b%i==0&&c%i==0)
        {
            hcf*=i;
            a/=i;
            b/=i;
            c/=i;
        }
        else
             i++;
     }
    return hcf;
  }
 int main()
  {
      int a,b,c,yes;
      start:
        cout<<"Enter three numbers :-";
        cin>>a>>b>>c;
        cout<<"HCF = "<<HCF(a,b,c);
        cout<<"\nAgain calculate (1):- ";
        cin>>yes;
        if(yes==1)
         goto start;
      return 0;
  }
Output :-





Related Programs

Post a Comment

0 Comments