Decisión + Iteración – Ejemplo 1

Enunciado

Leer un conjunto de n>0 números enteros. Informar los números que sean primos, la cantidad y su sumatoria. Un entero p es primo si, siendo distinto de 0 y de +/- 1, es divisible solamente por +/-1 y por +/- p.

Código

Python

def main():
    print( "Cantidad de números/amount of numbers: " )
    n = int( input() )
    s=0
    c=0
    print( "Ingresar", n, "números enteros/", end="" )
    print( "Enter", n, "integer numbers:\n" )
    for k in range ( 1, n+1 ):
        nro = int( input())
        print( str(k).rjust(10), str(nro).rjust(10), end="" )
        i = 2
        r = 1
        while r!=0 and i<nro:
            r = nro % i
            i += 1
        if r!=0 and nro!=1:
            print( " <--- Primo/Prime" )
            s += nro
            c += 1
        print()
        
    print()
    print( "Números primos/Prime numbers: ", c, "\n" )
    print( "Sumatoria/Summation: ", s, "\n" )

    input( "Presionar/Press Enter to exist " )  
main()

C++

#include <iostream>
#include <iomanip>
using namespace std ;
#include <conio.h>

int main()
{
   int n, i, k, c, r, nro ;
   float s ;

   cout << "Cantidad de n£meros/amount of numbers: " ;
   cin >> n ;
   s = c = 0 ;
   cout << "Ingresar " << n << " n£meros enteros/" ;
   cout << "Enter " << n << " integer numbers: " ;
   cout << endl ;
   for ( k=1 ; k<=n ; k++ )
   {
    	cin >> nro ;
      	cout << setw(10) << k << setw(10) << nro ;
      	i = 2 ; 
	r = 1 ;
      	while ( r!=0 && i<nro )
      	{
      	     r = nro % i ;
             i++ ;
      	}
      	if ( r!=0 && nro!=1 )
      	{
      	     cout << " <--- Primo/Prime" ;
             s += nro ;
             c++ ;
      	}
      	cout << endl ;
   }
   cout << endl ;
   cout << "N£meros primos/Prime numbers: " ;
   cout << c << endl ;
   cout << "Sumatoria/Summation: " << s << endl ;
	
   cout << "Presionar/Press Enter to exit " ;
   getch() ;
   return 0 ;
}

Pascal

Program Problema8_9 ;
var
   n, i, k, c, r : 0..50 ;
   nro : integer ;
   s : real ;

begin
   write( 'Cantidad de n£meros/amount of numbers: ' ) ;
   readln( n ) ;
   s := 0 ;
   c := 0 ;
   write( 'Ingresar ', n,' n£meros enteros/' ) ;
   writeln( 'Enter ', n, ' integer numbers: ' ) ;
   for k:=1 to n do
   begin
       readln( nro ) ;
       write( k:10) ; write( nro:10 ) ;
       i := 2 ;
       r := 1 ;
       while (r<>0) and (i<nro) do
       begin
           r := nro mod i ;
           i := i+1
       end ;
       if (r<>0) and (nro<>1) 
       then
       begin
           write( '  <----- Primo/Prime' ) ;
           s := s + nro ;
           c := c + 1 ;
       end ;
       writeln ;
   end ;
   writeln ;
   writeln( 'N£meros primos/Prime numbers: ', c ) ;
   writeln( 'Sumatoria/Summation: ', s:0:0 ) ;
     
   writeln( 'Presionar/Press Enter to exit' ) ;
   readln ;	
end.

Diagramas