Home > Net >  C # small white for guidance
C # small white for guidance

Time:10-01

In the same class defined in different ways, to achieve the following functions:
(1) design a method of calculation of 1-100 between the sum of all the multiples of 3 Numbers, and returns the value;
(2) for a given any two integers, calculate the number of the two least common multiple, and the number 3 output;
(3) for a given any two integers, calculate the number of the two greatest common divisor, and the number 3 output;
In the main () method, the implementation of the above three methods calls,
(note: it is recommended that their own independent thinking and exhaustive method implementation, if the other related reference online calculation method, to write in the comments of the program, the process of computing the meaning of each step)

The zha yao do, ask the great spirit guide

CodePudding user response:

Exhaustive method is as follows:
 using System; 
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;

The namespace ConsoleApplication4
{
Class Program
{
The static void Main (string [] args)
{
Int sum=BaseClass. Sum ();
Console. WriteLine (sum);
BaseClass. The GCD (12, 2);
BaseClass. LCM (4, 6);
}
}

Public class BaseClass
{
//for multiple of 3 and
Public static int the Sum ()
{
Int sum=0;
For (int I=1; I & lt;=100; I++)
{
If (I % 3==0)
{
The sum +=I;
}
}
Return the sum.
}

//the greatest common divisor, generally for two positive integers
Public static void the GCD (int numOne, int numTwo)
{
Int min=numOne & gt; NumTwo? NumTwo: numOne;
If (numOne==0 | | numTwo==0 | | numOne & lt; 0 | | numTwo & lt; 0)
{
Throw new Exception ();
}

//exhaustive method traversal
Int the GCD=1;
For (int I=min; I & gt;=1; I -)
{
If (numOne % I==0 & amp; & NumTwo % I==0)
{
The GCD=I;
break;
}
}

//output the three number
Console. WriteLine (" the first number is: {0} ", numOne);
Console. WriteLine (" the second number is: {0} ", numTwo);
Console. WriteLine (" the greatest common divisor is: {0} ", GCD);
}

//LCM, generally for two positive integers
Public static void LCM (int numOne, int numTwo)
{
Int Max=numOne & gt; NumTwo? NumOne: numTwo;
If (numOne==0 | | numTwo==0 | | numOne & lt; 0 | | numTwo & lt; 0)
{
Throw new Exception ();
}

//exhaustive method traversal
Int LCM=Max;
For (int I=Max. I & lt;=numOne * numTwo; I++)
{
If (I % numOne==0 & amp; & I % numTwo==0)
{
LCM=I;
break;
}
}

//output the three number
Console. WriteLine (" the first number is: {0} ", numOne);
Console. WriteLine (" the second number is: {0} ", numTwo);
Console. WriteLine (" the least common multiple of them are as follows: {0} ", LCM);
}


}
}

CodePudding user response:

For any two Numbers, how to change

CodePudding user response:

reference 1st floor HerryDong response:
exhaustive method is as follows:
 using System; 
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;

The namespace ConsoleApplication4
{
Class Program
{
The static void Main (string [] args)
{
Int sum=BaseClass. Sum ();
Console. WriteLine (sum);
BaseClass. The GCD (12, 2);
BaseClass. LCM (4, 6);
}
}

Public class BaseClass
{
//for multiple of 3 and
Public static int the Sum ()
{
Int sum=0;
For (int I=1; I & lt;=100; I++)
{
If (I % 3==0)
{
The sum +=I;
}
}
Return the sum.
}

//the greatest common divisor, generally for two positive integers
Public static void the GCD (int numOne, int numTwo)
{
Int min=numOne & gt; NumTwo? NumTwo: numOne;
If (numOne==0 | | numTwo==0 | | numOne & lt; 0 | | numTwo & lt; 0)
{
Throw new Exception ();
}

//exhaustive method traversal
Int the GCD=1;
For (int I=min; I & gt;=1; I -)
{
If (numOne % I==0 & amp; & NumTwo % I==0)
{
The GCD=I;
break;
}
}

//output the three number
Console. WriteLine (" the first number is: {0} ", numOne);
Console. WriteLine (" the second number is: {0} ", numTwo);
Console. WriteLine (" the greatest common divisor is: {0} ", GCD);
}

//LCM, generally for two positive integers
Public static void LCM (int numOne, int numTwo)
{
Int Max=numOne & gt; NumTwo? NumOne: numTwo;
If (numOne==0 | | numTwo==0 | | numOne & lt; 0 | | numTwo & lt; 0)
{
Throw new Exception ();
}

//exhaustive method traversal
Int LCM=Max;
For (int I=Max. I & lt;=numOne * numTwo; I++)
{
If (I % numOne==0 & amp; & I % numTwo==0)
{
LCM=I;
break;
}
}

//output the three number
Console. WriteLine (" the first number is: {0} ", numOne);
Console. WriteLine (" the second number is: {0} ", numTwo);
Console. WriteLine (" the least common multiple of them are as follows: {0} ", LCM);
}


}
}

For any two Numbers, how to change

CodePudding user response:

Really doubt you have earnest learning c #

CodePudding user response:

Before the class didn't understand haven't tube, ready to start again

CodePudding user response:

In that case, these subjects should do it yourself, there is a problem to ask you
  •  Tags:  
  • C#
  • Related