Sunday 8 September 2013

Program to find HCF in C

         WAP to find HCF(highest common factor) in C

Before you start: To learn how to compile a c program in C click here or click here


#include"stdio.h"

int main()
int num1, num2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
 for(i=1; i<=num1 || i<=num2; ++i)
 {
   if(num1%i==0 && num2%i==0) 
// Checking whether i is a factor of both number   
   hcf=i; 
  }
printf("H.C.F of %d and %d is %d \n", num1, num2, hcf);
printf("\nBy Kushagra Sharma");
return 0;
 }

Output:

hacker@ubuntu:~$ cd Desktop
hacker@ubuntu:~/Desktop$ gcc kush.c
hacker@ubuntu:~/Desktop$ gcc kush.c -o kush
hacker@ubuntu:~/Desktop$ ./kush
Enter two integers: 24
5
H.C.F of 24 and 5 is 1

By Kushagra Sharmahacker@ubuntu:~/Desktop$ ^C
hacker@ubuntu:~/Desktop$


To learn more in C click here

No comments:

Post a Comment