Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Console.WriteLine("This is a program that calculates the gcd of some typed number");
            int StartingNum = Convert.ToInt16(Console.ReadLine());
            int NumForCalculate = StartingNum;
            string gcd = ("");
            int gathering = 1;
            operation:
            int x = 2;
            operation2:
            if ((NumForCalculate / x) == :see below:)
            {
                if (gcd == "")
                    gcd = ("" + x);
                else
                    gcd = (gcd + "," + x);
                NumForCalculate = (NumForCalculate / x);
                gathering = gathering * x;
                if (NumForCalculate <= 1)
                    goto end;
                else
                    goto operation;
            }
            else
            {
                x++;
                goto operation2;
            }
            end:
            Console.WriteLine("gcd of integers {0} is {1}, and gathering this set of numbers is {2}", StartingNum, gcd, gathering);
            Console.ReadKey();


if i set starting nub as 20

if ((NumForCalculate / x) == 1)

it will be like this: gcd of integers 20 is 11, and gathering this set of numbers is 11

if ((NumForCalculate / x) == 0)

it will be like this: gcd of integers 20 is 21, and gathering this set of numbers is 21
Posted
Updated 6-Apr-13 9:11am
v2
Comments
Kenneth Haugland 6-Apr-13 14:38pm    
Not quite related to your question but have you seen this:
http://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.common.biginteger.gcd%28v=vs.93%29.aspx
Kenneth Haugland 6-Apr-13 14:41pm    
And please remove the GoTo statements, they are usually considered ancient relics in the modern era.
OriginalGriff 6-Apr-13 14:54pm    
Agreed - I'm not even looking at it with those in there...

1 solution

The problem of finding the GCD is usually done by using modular, i.a.
VB
if (20 Mod 2 = 0) then 

or C#
C#
if (20%2==0)

This allows you to check if they could be devisible by a number without any remander. I have implemented sort of the thing you are looking for here:
Fun with continued fractions[^]

As for the GoTo statments, they should be removed becouse the make the code completely unreadeble. I usually reffer to this kind of code as Spaghetti-code, as you cant find the start or end of anything. And they are sure to frustrate you or even worse, the person that would take over your code.
 
Share this answer
 
Comments
Kenneth Haugland 6-Apr-13 15:36pm    
From op:
nah same things happend

if i set starting nub as 20

if ((NumForCalculate / x) == 1)

it will be like this: gcd of integers 20 is 11, and gathering this set of numbers is 11

if ((NumForCalculate / x) == 0)

it will be like this: gcd of integers 20 is 21, and gathering this set of numbers is 21

just like it was before
Kenneth Haugland 6-Apr-13 15:39pm    
You didnt seem to understand me, use (NumForCalculate % x == 0). And this:
"it will be like this: gcd of integers 20 is 11, and gathering this set of numbers is 11"
seems that you are misunderstanding what GCD actually is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900