Pascal, C++, JavaScript, PowerShell, C# – Return Prime Numbers

email me

Return prime numbers from 1-100 in different programming languages. All written and tested by me.

Prime Numbers

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

Pascal

program OptimusPrime;

{set our constant--this is max number}
const
maxNumber=100;

{set variables--both are integers}
var
x, y:integer;

{set number range to scan}
begin
for x := 2 to maxNumber do
begin
for y := 2 to x do
if (x mod y)=0 then
{break if not prime}
break;
{display prime}
if(y = x) then
write(x, ' ');
end;
end.

Online Pascal compiler

 

C++

#include <iostream>
#include <string>
#include <vector>

int main()
{
// max number to extract primes
int maxNumber = 100;

//set up array
std::vector<int> OptimusPrime = { 2 };
// start number, then increment
for (int x = 2; x <= maxNumber; ++x) 
for (int y = 2; y < x; ++y) {
if (x % y == 0)
break;
if (y == x - 1)
// move to new element
OptimusPrime.push_back(x);
}

// outputs prime num with space
for (int z: OptimusPrime)
std::cout << z << ' ';
return 0;
}

Online C++ compiler

 

JavaScript

var maxNumber = 100;

// run function
OptimusPrime(maxNumber);

// interates through specified number
function OptimusPrime(number) {
var array = [2];
for ( var i = 3; i < number; i+=2 ) {
if ( CheckPrime(i) ) {
//push element
array.push(i);
}
}
// output whole array
document.write(array);
}

// checks if input is prime
function CheckPrime(input) {
for ( var i = 2; i < input; i++ ) {
if ( input % i === 0 ) {
return false;
}
}
return true;
}

Online JavaScript editor

 

PowerShell

Clear-Host
 
$maxNumber = 100
$counter = 0
$intInput = ''
$intDivisor = ''
$boolPrime = ''

# cycle through range 
2..$maxNumber | foreach {
 
    # set input to number in range
    $intInput = $_

    # perform calculation for divisor
    $intDivisor = [math]::Sqrt($intInput)
    $boolPrime = $true
 
    # compare divisor with zero
    2..$intDivisor | foreach {        
        
        if ($intInput % $_ -eq 0) {
        
            $boolPrime = $false
        }
    }
 
    # if prime true, output to screen
    If ($boolPrime) {
        
        Write-Host -NoNewline  $intInput.ToString().PadLeft(3)
        
        # step counter
        $counter++
        
        If ($counter % 10 -eq 0) {            
        }
    }
 
}
 
# clear session
 
$maxNumber = ''
$counter = ''
$intInput = ''
$intDivisor = ''
$boolPrime = ''
 
Write-Host ""
Write-Host ""

Online PowerShell editor

 

C#

// created in Visual Studio 2017, 15.3.2

using System;

namespace Primes
{
    class OptimusPrime
    {
        static void Main(string[] args)
        {
            // set max value
            int maxNumber = 100;

            // default value
            bool Prime = true;            

            for (int x = 2; x <= maxNumber; x++)
            {
                Prime = true;

                for (int y = 2; y <= maxNumber; y++)
                {
                    // Perform calculation for divisor and compare with zero
                    if (x != y && x % y == 0)
                    {
                        Prime = false;
                        break;
                    }
                }
                
                // if prime true, output to console
                if (Prime)                {
                    
                    Console.Write("  " + x);
                }                
            }

            // wait for key press
            Console.ReadKey();
        }
    }
}

Online C# compiler

 

Fortran

see http://eddiejackson.net/wp/?p=20363

Online Fortran compiler

 

JAVA

see: http://eddiejackson.net/wp/?p=18617

 

Notes

Learn more, Pascal language

Learn more, Fortran language