-4

I have a file and it has about 20 lines and in each line, I have to numbers and it gets split by "," and I want to know their GCD and LCM so I don't know the algorithms 128,96 11,3 18,40 6,12 14,16 18,20 30,45 20,50 100,38 131,90 800,200 45,18 39,13 50,20 80,32 75,20 they are the numbers in my txt file and I want to know their LCM and GCM and first, that program should read the txt file and second calculate each line and numbers and third export it as an output txt file in my windows. I want to do it all in C# of console or windows form or python

Hossein
  • 33
  • 5

2 Answers2

1

You can convert lines to list with this code:

if your text file is similar this

128,96
11,3
18,40
6,12
14,16
18,20
30,45
20,50
100,38
131,90
800,200
45,18
39,13
50,20
80,32
75,20

please run this code

List<string> allLinesText = File.ReadAllLines(@"FileAddress").ToList();
List<int> myNumbers = new List<int>();

foreach (var stringLine in allLinesText)
{
    foreach (var numbers in stringLine.Split(',').ToList())
    {
        myNumbers.Add(int.Parse(numbers));
    }
}

and do your calculations.

but if your text file is similar this

128,96 11,3 18,40 6,12 14,16 18,20 30,45 20,50 100,38 131,90 800,200 45,18 39,13 50,20 80,32 75,20

please run this code

string txtFile = File.ReadAllText(@"I:\Test.txt");
    
foreach (var groupNumber in txtFile.Split(' '))
{
   foreach (var number in groupNumber.Split(','))
   {
       myNumbers.Add(int.Parse(number));
   }
}

I hope it helps you.

Akbar Asghari
  • 613
  • 8
  • 19
  • Thanks for your answer but when I run this codes it didn't run anything please guide me – Hossein Jul 09 '20 at 07:28
  • Thanks for your answer so where are GCD and LCM I think you forgot the important things its just list the numbers – Hossein Jul 09 '20 at 19:26
  • please check out this https://stackoverflow.com/questions/18541832/c-sharp-find-the-greatest-common-divisor – Akbar Asghari Jul 09 '20 at 19:28
  • I knew that but I want to use between these codes it gets difficult and I have the numbers from txt file so I want the numbers of txt file get GCD, it's not I use my own number or i insert numbers its automatic Numbers and it could be 100 line instead of 20 lines of numbers – Hossein Jul 09 '20 at 19:33
1

you could use this piece of code to calculate GCD and LCM:

//FYI ->   num1 * num2 = GCD(num1, num2) * LCM(num1, num2)

    static int GetGCD(int num1, int num2)
    {
        while (num1 != num2)
        {
            if (num1 > num2)
                num1 = num1 - num2;

            if (num2 > num1)
                num2 = num2 - num1;
        }
        return num1;
    }

    static int GetLCM(int num1, int num2)
    {
        return (num1 * num2) / GetGCD(num1, num2);
    }

Read fileinput.txt, and display result in fileouput.txt

        List<string> allLinesText = File.ReadAllLines(@"d:\\fileinput.txt").ToList();

        using (StreamWriter writer = new StreamWriter("d:\\fileouput.txt"))
        {

            foreach (var stringLine in allLinesText)
            {
                var numbers = stringLine.Split(',');
                var num1 = Int32.Parse(numbers[0]);// use Int32.TryParse to trap possible error
                var num2 = Int32.Parse(numbers[1]);
                var gcd = GetGCD(num1, num2);
                var lcm = GetLCM(num1, num2);

                writer.WriteLine($"num1: {num1,5} num2: {num2,5}  LCM: {lcm,7}  GCD: {gcd,7}");

            }
        }

result in fileouput.txt:

num1:   128 num2:    96  LCM:     384  GCD:      32
num1:    11 num2:     3  LCM:      33  GCD:       1
num1:    18 num2:    40  LCM:     360  GCD:       2
num1:     6 num2:    12  LCM:      12  GCD:       6
num1:    14 num2:    16  LCM:     112  GCD:       2
num1:    18 num2:    20  LCM:     180  GCD:       2
num1:    30 num2:    45  LCM:      90  GCD:      15
num1:    20 num2:    50  LCM:     100  GCD:      10
num1:   100 num2:    38  LCM:    1900  GCD:       2
num1:   131 num2:    90  LCM:   11790  GCD:       1
num1:   800 num2:   200  LCM:     800  GCD:     200
num1:    45 num2:    18  LCM:      90  GCD:       9
num1:    39 num2:    13  LCM:      39  GCD:      13
num1:    50 num2:    20  LCM:     100  GCD:      10
num1:    80 num2:    32  LCM:     160  GCD:      16
num1:    75 num2:    20  LCM:     300  GCD:       5

fileinput.txt

128,96
11,3
18,40
6,12
14,16
18,20
30,45
20,50
100,38
131,90
800,200
45,18
39,13
50,20
80,32
75,20
Frenchy
  • 16,386
  • 3
  • 16
  • 39