• This is default featured slide 1 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 2 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 3 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 4 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

  • This is default featured slide 5 title

    Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by NewBloggerThemes.com.

Mostrando entradas con la etiqueta columnas. Mostrar todas las entradas
Mostrando entradas con la etiqueta columnas. Mostrar todas las entradas

Primos en un arreglo

Ahora buscaremos numeos primos en un arreglo es sencillo ya rapido solo refrescaremos que primo es aquel divisible sobre si y el 1.
/*
 * Realizar el programa que asigne a un arreglo lineal n numeros enteros
 * se pide calcular e imprimir cuantos numeros primos existen en el arreglo.
 */
package arreglos;

/**
 *
 * @author Jonathan
 */
public class arreglos1{

    public static void main(String[] args) {
        int n, i, cnp = 0, res;
        n = (int) (Math.random() * 100);
        int num[] = new int[n + 1];
        for (i = 0; i < n; i++) {
            num[i] = (int) (Math.random() * 100);
            res = 0;
            for (int j = 1; j <= num[i]; j++) {
                if (num[i] % j == 0) {
                    res++;
                }
            }
            if (res == 2) {
                cnp++;
                System.out.println("numero primo =" + num[i]);
            }

        }
        System.out.println("La cantidad de numeros primos es :" + cnp);
    }
}
Ya vimos un unidimensional tenemos la idea apliquemos al bidimensional.
/* 
* Realizar el programa que asigne aleatoriamente a un arreglo bidimensional 
* de mxn elementos datos de tipo entero, se pide calcular e imprimir cuantos 
* nùmeros primos existen en cada fila y en cada columna del arreglo. 
*/ 
package arreglos;

public class arreglos2{

    public static void main(String[] args) {
        int m, n, i, j, cnp, res;
        m = (int) (Math.random() * 10 + 2);
        n = (int) (Math.random() * 10 + 2);
        int beta[][] = new int[m + 1][n + 1];
        cnp = 0;
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                beta[i][j] = (int) (Math.random() * 100);
                res = 0;
                for (int k = 1; k <= beta[i][j]; k++) {
                    if (beta[i][j] % k == 0) {
                        res++;
                    }
                }
                if (res == 2) {
                    cnp++;
                    System.out.println("numero primo =" + beta[i][j]);
                }

            }
        }//Fin de la generacion de filas
        System.out.println("La cantidad de numeros primos en la fila " + i + " son:" + cnp);
        for (j = 0; j < n; j++) {
            for (i = 0; i < m; i++) {
                beta[i][j] = (int) (Math.random() * 100);
                res = 0;
                for (int k = 1; k <= beta[i][j]; k++) {
                    if (beta[i][j] % k == 0) {
                        res++;
                    }
                }
                if (res == 2) {
                    cnp++;
                    System.out.println("numero primo =" + beta[i][j]);
                }

            }
        } //Fin de la generacion de columnas

        System.out.println("La cantidad de numeros primos en la columna " + j + " son:" + cnp);
    }
}
http://www.mediafire.com/?mgtybdwnjmb
Share:

Visitantes

Flag Counter

Popular Posts

Etiquetas

Recent Posts