Jump to content

[C#] Cel mai mare divizor comun dintre 2 numere


zergitHD
 Share

Recommended Posts

Si daca e 50124532. Parcurgi tu toate numerele de la 1 la 50124532?

 

Codul care e mult mai bun:

 

 

            var ok = true;
            int a, b;
            a = b = 0; // da eroare de compilare ca nu am dat assign la var
            do
            {
                ok = true;
                Console.WriteLine("Introdu primul numar.");
                try
                {
                    a = Int32.Parse(Console.ReadLine()); // Daca introduce ceva ce nu poate fi facut int, asta da exception
                }
                catch (Exception)
                {
                    Console.WriteLine("Introdu un numar");
                    ok = false;
                }
            } while (ok != true); // Repetam pana baga corect primul numar

            do
            {
                ok = true;
                Console.WriteLine("Introdu cel de-al doilea numar.");
                try
                {
                    b = Int32.Parse(Console.ReadLine()); // Daca introduce ceva ce nu poate fi facut int, asta da exception
                }
                catch (Exception)
                {
                    Console.WriteLine("Introdu un numar");
                    ok = false;
                }
            } while (ok != true); // Repetam pana baga corect primul numar

            // Acum practic incepem sa facem cmmdc

            int rest;
            while (b != 0)
            {
                rest = a % b;
                a = b;
                b = rest;
            }
            // acum in variabila "a" este cmmdc dintre a si b
            Console.WriteLine(a);

 

Edited by Chase.

Cel mai developer

Link to comment
Share on other sites

  • 4 weeks later...
  • 3 weeks later...
  • 2 weeks later...
  • 5 months later...
  • 5 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...