C # -- 2nd week experiment -- Task 5 -- compile a console application -- calculate the minimum public multiple and maximum public approx. Of two integers

Source: Internet
Author: User

/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: enter two integers to obtain the minimum public multiple and maximum public approx.
* Author: Lei hengxin
* Completion date: January 1, September 08, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Programming philosophy:
* Input description:
* Problem description:
* Program output:
* End the comment in the program Header
*/
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace ConsoleApplication_do_while
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("this is a program that inputs two integers to calculate the least common multiple of the two integers and the maximum common number ");
Console. Write ("Enter the first INTEGER :");
String first = Console. ReadLine ();
Console. Write ("enter the second INTEGER :");
String second = Console. ReadLine ();
 
Int x = int. Parse (first); // type conversion
Int y = int. Parse (second); // type conversion
 
Int Greatest_common_divisor = gcd (x, y );
Int Least_common_multiple = lcm (x, y );
Console. WriteLine ("The minimum public multiples of {0} and {1} are: {2} The maximum public approx. Is: {3}", x, y, Least_common_multiple, Greatest_common_divisor );
Console. ReadKey ();
}
 
// Calculate the maximum public approx.
Static int gcd (int m, int n)
{
Int I = 2; // define the cyclic control variable
Int Least_common_multiple = 1; // calculates the maximum common approx.
Int min1 = min (m, n );
While (I <= min1)
{
While (m % I = 0 & n % I = 0) // calculates the common divisor of the denominator.
{
M = m/I;
N = n/I;
Min1 = min (m, n );
Least_common_multiple = Least_common_multiple * I;
}
++ I;
}
Return Least_common_multiple;
 
}
 
// Calculate the minimum public multiple
Static int lcm (int m, int n)
{
Int Greatest_common_divisor = gcd (m, n); // calculates the maximum public approx.
Int Least_common_multiple = (m/Greatest_common_divisor) * (n/Greatest_common_divisor) * Greatest_common_divisor; // The minimum public multiple has a certain relationship with the maximum public approx.
Return Least_common_multiple;
}
 
// Calculate the maximum and minimum values of two numbers
Static int min (int m, int n)
{
Int min;
If (m> n)
{
Min = n;
}
Else
{
Min = m;
}
Return min;
 
}
}
}

 
Running result:
 

 

 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.