Programa para imprimir números primos del 1 al N.

Dado un número N, la tarea es imprimir los números primos del 1 al N.
Ejemplos: 

Input: N = 10
Output: 2, 3, 5, 7

Input: N = 5
Output: 2, 3, 5 

Algoritmo:  

C++

// C++ program to display Prime numbers till N
#include <bits/stdc++.h>
using namespace std;
 
// function to check if a given number is prime
bool isPrime(int n)
{
    // since 0 and 1 is not prime return false.
    if (n == 1 || n == 0)
        return false;
 
    // Run a loop from 2 to n-1
    for (int i = 2; i < n; i++) {
        // if the number is divisible by i, then n is not a
        // prime number.
        if (n % i == 0)
            return false;
    }
    // otherwise, n is prime number.
    return true;
}
 
// Driver code
int main()
{
    int N = 100;
 
    // check for every number from 1 to N
    for (int i = 1; i <= N; i++) {
        // check if current number is prime
        if (isPrime(i))
            cout << i << " ";
    }
 
    return 0;
}

C

// C program to display Prime numbers till N
#include <stdbool.h>
#include <stdio.h>
 
// function to check if a given number is prime
bool isPrime(int n)
{
    // since 0 and 1 is not prime return false.
    if (n == 1 || n == 0)
        return false;
 
    // Run a loop from 2 to n-1
    for (int i = 2; i < n; i++) {
        // if the number is divisible by i, then n is not a
        // prime number.
        if (n % i == 0)
            return false;
    }
    // otherwise, n is prime number.
    return true;
}
 
// Driver code
int main()
{
    int N = 100;
 
    // check for every number from 1 to N
    for (int i = 1; i <= N; i++) {
        // check if current number is prime
        if (isPrime(i))
            printf("%d ", i);
    }
 
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Java

// Java program to display Prime numbers till N
class GFG
{
      //function to check if a given number is prime
     static boolean isPrime(int n){
          //since 0 and 1 is not prime return false.
          if(n==1||n==0)return false;
   
          //Run a loop from 2 to n-1
          for(int i=2; i<n; i++){
            // if the number is divisible by i, then n is not a prime number.
                if(n%i==0)return false;
          }
          //otherwise, n is prime number.
          return true;
    }
     
 
    // Driver code
    public static void main (String[] args)
    {
        int N = 100;
        //check for every number from 1 to N
        for(int i=1; i<=N; i++){
            //check if current number is prime
            if(isPrime(i)) {
                System.out.print(i + " ");
            }
        }
 
    }
}

Python3

# Python3 program to display Prime numbers till N
 
#function to check if a given number is prime
def isPrime(n):
  #since 0 and 1 is not prime return false.
  if(n==1 or n==0):
    return False
   
  #Run a loop from 2 to n-1
  for i in range(2,n):
    #if the number is divisible by i, then n is not a prime number.
    if(n%i==0):
      return False
   
  #otherwise, n is prime number.
  return True
 
 
 
# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
  #check if current number is prime
  if(isPrime(i)):
    print(i,end=" ")

C#

// C# program to display Prime numbers till N
using System;
     
class GFG
{
   
     //function to check if a given number is prime
     static bool isPrime(int n){
        //since 0 and 1 is not prime return false.
        if(n==1||n==0) return false;
 
        //Run a loop from 2 to n-1
        for(int i=2; i<n; i++) {
            // if the number is divisible by i, then n is not a prime number.
            if(n%i==0) return false;
        }
      //otherwise, n is prime number.
      return true;
    }
 
    // Driver code
    public static void Main (String[] args)
    {
        int N = 100;
        //check for every number from 1 to N
        for(int i=1; i<=N; i++) {
          //check if current number is prime
            if(isPrime(i)) {
              Console.Write(i + " ");
            }
        }
 
    }
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
 
// JavaScript program to display Prime numbers till N
 
// function to check if a given number is prime
function isPrime( n)
{
      // since 0 and 1 is not prime return false.
      if(n == 1 || n == 0) return false;
   
      // Run a loop from 2 to n-1
      for(var i = 2; i < n; i++)
      {
       
        // if the number is divisible by i, then n is not a prime number.
        if(n % i == 0) return false;
      }
      // otherwise, n is prime number.
      return true;
}
 
 
// Driver code
var N = 100;
 
// check for every number from 1 to N
  for(var i = 1; i <= N; i++)
  {
      // check if current number is prime
      if(isPrime(i)) {
        console.log( i );
      }
}
 
// This code is contributed by ukasp.
</script>

C++

// C++ program to display Prime numbers till N
#include <bits/stdc++.h>
using namespace std;
 
//function to check if a given number is prime
bool isPrime(int n){
    //since 0 and 1 is not prime return false.
    if(n==1||n==0) return false;
 
    //Run a loop from 2 to n/2.
    for(int i=2; i<=n/2; i++) {
          // if the number is divisible by i, then n is not a prime number.
          if(n%i==0) return false;
    }
    //otherwise, n is prime number.
    return true;
}
 
 
// Driver code
int main()
{
    int N = 100;
 
    //check for every number from 1 to N
      for(int i=1; i<=N; i++){
        //check if current number is prime
        if(isPrime(i)) {
          cout << i << " ";
        }
    }
 
    return 0;
}

Java

// Java program to display
// Prime numbers till N
class GFG
{
     //function to check if a given number is prime
     static boolean isPrime(int n){
          //since 0 and 1 is not prime return false.
          if(n==1||n==0) return false;
 
        //Run a loop from 2 to n-1
        for(int i=2; i<=n/2; i++){
            // if the number is divisible by i, then n is not a prime number.
            if(n%i==0)return false;
        }
        //otherwise, n is prime number.
        return true;
    }
     
 
    // Driver code
    public static void main (String[] args)
    {
        int N = 100;
        //check for every number from 1 to N
        for(int i=1; i<=N; i++){
            //check if current number is prime
            if(isPrime(i)) {
              System.out.print(i + " ");
            }
        }
 
    }
}

Python3

# Python3 program to display Prime numbers till N
 
#function to check if a given number is prime
def isPrime(n):
  #since 0 and 1 is not prime return false.
  if(n==1 or n==0):
    return False
   
  #Run a loop from 2 to n/2
  for i in range(2,(n//2)+1):
    #if the number is divisible by i, then n is not a prime number.
    if(n%i==0):
      return False
   
  #otherwise, n is prime number.
  return True
 
 
 
# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
  #check if current number is prime
  if(isPrime(i)):
    print(i,end=" ")

C#

// C# program to display
// Prime numbers till N
using System;
     
class GFG
{
   
 //function to check if a given number is prime
 static bool isPrime(int n){
      //since 0 and 1 is not prime return false.
     if(n==1||n==0)return false;
   
      //Run a loop from 2 to n/2.
      for(int i=2; i<=n/2; i++){
        // if the number is divisible by i, then n is not a prime number.
        if(n%i==0)return false;
      }
  //otherwise, n is prime number.
  return true;
}
 
// Driver code
public static void Main (String[] args)
{
    int N = 100;
    //check for every number from 1 to N
      for(int i=1; i<=N; i++){
      //check if current number is prime
      if(isPrime(i)) {
        Console.Write(i + " ");
      }
    }
     
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
// Javascript program to display Prime numbers till N
 
// function to check if a given number is prime
function isPrime(n)
{
 
    // since 0 and 1 is not prime return false.
    if(n == 1 || n == 0) return false;
 
    // Run a loop from 2 to n/2.
    for(let i = 2; i <= n / 2; i++)
    {
          // if the number is divisible by i, then n is not a prime number.
          if(n % i == 0) return false;
    }
     
    // otherwise, n is prime number.
    return true;
}
 
 
// Driver code
let N = 100;
 
// check for every number from 1 to N
for(let i = 1; i <= N; i++)
{
    // check if current number is prime
    if(isPrime(i))
    {
        document.write(i + " ");
    }
}
 
// This code is contributed by shubham348.
</script>

C++

// C++ program to display Prime numbers till N
#include <bits/stdc++.h>
using namespace std;
 
//function to check if a given number is prime
bool isPrime(int n){
  //since 0 and 1 is not prime return false.
  if(n==1||n==0)return false;
   
  //Run a loop from 2 to square root of n.
  for(int i=2; i*i<=n; i++){
    // if the number is divisible by i, then n is not a prime number.
    if(n%i==0)return false;
  }
  //otherwise, n is prime number.
  return true;
}
 
 
// Driver code
int main()
{
    int N = 100;
 
    //check for every number from 1 to N
      for(int i=1; i<=N; i++){
      //check if current number is prime
      if(isPrime(i)) {
        cout << i << " ";
      }
    }
 
    return 0;
}

Java

// Java program to display
// Prime numbers till N
class GFG
{
  //function to check if a given number is prime
 static boolean isPrime(int n){
  //since 0 and 1 is not prime return false.
  if(n==1||n==0)return false;
   
  //Run a loop from 2 to square root of n
  for(int i=2; i*i<=n; i++){
    // if the number is divisible by i, then n is not a prime number.
    if(n%i==0)return false;
  }
  //otherwise, n is prime number.
  return true;
}
     
 
// Driver code
public static void main (String[] args)
{
    int N = 100;
        //check for every number from 1 to N
      for(int i=1; i<=N; i++){
      //check if current number is prime
      if(isPrime(i)) {
        System.out.print(i + " ");
      }
    }
     
}
}

Python3

# Python3 program to display Prime numbers till N
 
#function to check if a given number is prime
def isPrime(n):
  #since 0 and 1 is not prime return false.
  if(n==1 or n==0):
    return False
   
  #Run a loop from 2 to square root of n.
  for i in range(2,int(n**(1/2))+1):
    #if the number is divisible by i, then n is not a prime number.
    if(n%i==0):
      return False
   
  #otherwise, n is prime number.
  return True
 
 
 
# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
  #check if current number is prime
  if(isPrime(i)):
    print(i,end=" ")

C#

// C# program to display
// Prime numbers till N
using System;
     
class GFG
{
   
 //function to check if a given number is prime
 static bool isPrime(int n){
      //since 0 and 1 is not prime return false.
     if(n==1||n==0)return false;
   
      //Run a loop from 2 to square root of n.
      for(int i=2; i*i<=n; i++){
        // if the number is divisible by i, then n is not a prime number.
        if(n%i==0)return false;
      }
  //otherwise, n is prime number.
  return true;
}
 
// Driver code
public static void Main (String[] args)
{
    int N = 100;
    //check for every number from 1 to N
      for(int i=1; i<=N; i++){
      //check if current number is prime
      if(isPrime(i)) {
        Console.Write(i + " ");
      }
    }
     
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
// JavaScript program to display Prime numbers till N
 
// function to check if a given number is prime
const isPrime = (n) => {
 
    // since 0 and 1 is not prime return false.
    if(n === 1||n === 0)return false;
 
    // Run a loop from 2 to square root of n.
    for(let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
    {
     
      // if the number is divisible by i, then n is not a prime number.
      if(n % i == 0)return false;
    }
     
    // otherwise, n is prime number.
    return true;
  }
   
   
  // Driver code
   
  let N = 100;
   
  // check for every number from 1 to N
  for(let i=1; i<=N; i++)
  {
   
      // check if current number is prime
      if(isPrime(i)) {
          document.write(i);
      }
  }
   
  // This code is contributed by shinjanpatra
  </script>

Publicación traducida automáticamente

Artículo escrito por Code_r y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *