0% found this document useful (0 votes)
18 views4 pages

# Primos de Una Lista

The document contains code to: 1) Define an array and populate it with user-input integers. 2) Print the array and search for a specific element input by the user. 3) Check each element to determine if it is a prime number by testing for factors, and print out primes. 4) An alternate method to only input and check for prime numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

# Primos de Una Lista

The document contains code to: 1) Define an array and populate it with user-input integers. 2) Print the array and search for a specific element input by the user. 3) Check each element to determine if it is a prime number by testing for factors, and print out primes. 4) An alternate method to only input and check for prime numbers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Console.

WriteLine("
*** matriz ***");
Console.WriteLine("
------");
int m, n, x, i;
Console.WriteLine();
Console.Write(">> dimension: m = ");
m = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine(">> elementos:");
int [] a;
a = new int[m];
Console.WriteLine();
for (x = 0; x < m; x++)
{
Console.Write("
* ({0}) = ", x+1);
a[x] = int.Parse(Console.ReadLine());
}
Console.WriteLine();
Console.WriteLine(">> lista 'a':");
Console.WriteLine();
for (x = 0; x < m; x++)
{
Console.WriteLine("\t{0}", a[x]);
}
Console.WriteLine();
Console.Write("buscar el elemento: ");
n = int.Parse(Console.ReadLine());
for (x = 0; x < m; x++)
{
if ((x+1) == n)
{
Console.WriteLine("el elemento {0} es: {1}", n, a[x]);
}
}

// numeros primos.
Console.WriteLine();
Console.WriteLine(">> numeros primos: ");
Console.WriteLine();
int c = 0;
for (x = 0; x < m; x++)
{
for (i = 1; i <= a[x]; i++)
{
if ((a[x]) % i == 0)
{
c++;
}
}
if (c <= 2)
{
Console.WriteLine("el elemento ({0}) = {1} es primo", x + 1, a[x]);
}
c = 0;
}

SOLO PRIMOS:
int m, n, x, i,c=0;
Console.WriteLine();
Console.Write(">> dimension: m = ");
m = int.Parse(Console.ReadLine());
Console.WriteLine(">> elementos:");
int[] a;
a = new int[m];
Console.WriteLine();
for (x = 0; x < m; x++)
{
Console.Write("
* ({0}) = ", x +1);
a[x] = int.Parse(Console.ReadLine());
}
for (x = 0; x < m; x++)
{
for (i = 1; i <= a[x]; i++)
{
if ((a[x]) % i == 0)
{
c++;
}
}
if (c <= 2)
{
Console.WriteLine("el elemento ({0}) = {1} es primo", x + 1, a[x]);
}
c = 0;
}

}
}

OTRA FORMA PARA PRIMOS:


int[] NUMEROS1;
int[] NUMEROSPRIMOS;
int x,a=0,i;
NUMEROS1 = new int[5];
NUMEROSPRIMOS = new int[5];
for (x = 0; x < 5; x++)
{
NUMEROS1[x] = int.Parse(Console.ReadLine());
}
for (x = 0; x < 5; x++)
{
for (i = 1; i <= NUMEROS1[x]; i++)
{
if (NUMEROS1[x] % i == 0)
{
a = a + 1;
}
}
if (a <= 2)
{
Console.WriteLine(" {0}", NUMEROS1[x]);
}
a = 0;
}

}
}

You might also like