Electrofriends

C program to calculate the GCD of a number

by Ranjith | April 4th, 2010.

Write a program to calculate the Greatest Common Divisor(GCD) of a number

#include<stdio.h>
#include<conio.h>
#include<process.h>
int gcd(int m,int n)
{
	int rem;
	while(n!=0)
	{
		rem=m%n;
		m=n;
		n=rem;
	}
	return(m);
}
main()
{
	int num1,num2,num3,gcd1,gcd2;
	clrscr();
	printf("Enter three positive integers");
	scanf("%d%d%d",&num1,&num2,&num3);
	if(num1==0 && num2==0 && num3==0)
	{
		printf("\n Invalid number");
		exit(0);
	}
	gcd1=gcd(num1,num2);
	gcd2=gcd(num3,gcd1);
	printf("\n GCD of %d %d %d is : %d\n",num1,num2,num3,gcd2);
	getch();
}

Share and Enjoy:
  • Digg
  • Technorati
  • StumbleUpon
  • Twitter
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Mixx
  • Yahoo! Buzz
  • LinkedIn
  • NewsVine
  • RSS
  • email
Similar Posts:

Leave a Reply

Share and enjoy

    • Digg
    • Facebook
    • Technorati
    • StumbleUpon
    • Twitter
    • Reddit
    • del.icio.us
    • Yahoo! Buzz
Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya