22.-
parte del 23.- funciona con 2 números.
using System;
namespace a4._22
{
class Program
{
//Descomponemos en factores primos.
static int factor(int x)
{
int factor;
//buscamos numeros primos
for (int i = 2; i <= x; i++)
{
int contador = 0;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
contador++;
}
}
//si el número es primo
if (contador == 0)
{
//y divisible por el numero
if (x % i == 0)
{
//obtenemos el factor primo
return factor = i;
}
}
}
return 1;
}
static int multiplo(int x, int y)
{
int total = 1, igual = 1, distinto1 = 1, distinto2 = 1;
if (x == y)
{
igual = x;
return total = total * x;
}
else if (x != y)
{
distinto1 = x;
distinto2 = y;
return total = total * x * y;
}
if (igual == distinto1)
{
return total = total / distinto1;
}
else if (igual == distinto2)
{
return total = total / distinto2;
}
return 1;
}
static int divisor(int x, int y)
{
int total = 1, igual = 1, distinto1 = 1, distinto2 = 1;
if (x == y)
{
igual = x;
return igual;
}
else if (x != y)
{
distinto1 = x;
distinto2 = y;
}
if (igual == distinto1)
{
return total =distinto1;
}
else if (igual == distinto2)
{
return total = distinto2;
}
return 1;
}
static void Main(string[] args)
{
int x, y, total = 1, fx, fy, aux = 0, menu;
while (aux == 0)
{
Console.WriteLine("Menú:");
Console.WriteLine("1. mcm");
Console.WriteLine("2. mcd");
Console.WriteLine("3. salir");
menu = int.Parse(Console.ReadLine());
if (menu == 1)
{
Console.WriteLine("Número 1:");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Número 2:");
y = int.Parse(Console.ReadLine());
while (x != 1 && y != 1)
{
fx = factor(x);
fy = factor(y);
total = total * multiplo(fx, fy);
x = x / fx;
y = y / fy;
}
Console.WriteLine("el mcm es:");
Console.WriteLine(total);
}
else if (menu == 2)
{
Console.WriteLine("Número 1:");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Número 2:");
y = int.Parse(Console.ReadLine());
while (x != 1 && y != 1)
{
fx = factor(x);
fy = factor(y);
total = total * divisor(fx, fy);
x = x / fx;
y = y / fy;
}
Console.WriteLine("el mcd es:");
Console.WriteLine(total);
}
else if (menu == 3)
{
Console.WriteLine("bye, bye...");
aux = 1;
}
else
{
Console.WriteLine("Opción incorrecta");
}
Console.ReadKey();
Console.Clear();
}
}
}
}
0 comentarios:
Publicar un comentario