share this page !

search

Monday, June 7, 2010

JAVA : Greatest Common Division (GCD) or the Highest Common Factor (HCF) of the given two numbers, using while loop


only for presentation purposes
Here are the Mumbai University First Year Engineering sem-2 Computer programing 2 (CP-2) programs.These are tried and tested and work just fine.Njoy!
Please refer this to all your pals on all your social networks..thank you !
The grey color is only for the background it should cause no problems.
-----------------------------------------------------------------------------------------------------------
/* Program of finding out the Greatest Common Division (GCD) or the Highest Common Factor (HCF) of the given two numbers, using while loop*/

import java.io.*;
class GCD
{
public static void main(String args[]) throws IOException
{

DataInputStream buf = new DataInputStream(System.in);
int n1,n2;
System.out.println("Enter 2nums");
n1=Integer.parseInt(buf.readLine());
n2=Integer.parseInt(buf.readLine());
System.out.print("The GCD of "+n1+" and "+n2);

while ((n1!=0) && (n2!=0))
{
if (n1 > n2)
n1 = n1 - n2 ;
else
n2 = n2 - n1 ;
}

System.out.print(" is "+n1);
try
{
System.out.println(" LCM is "+n1/n2);
}

catch (Exception e)
{
System.out.println("error :"+e+" idiot!, by 0?");
}
}
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Followers

 

Mumbai University Engineering. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com