TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the first 10 prime numbers

Program to print the first 10 prime numbers on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to print the first 10 prime numbers

Prime Numbers

Prime numbers are the natural numbers that can be divided by their self or by 1 without any remainder.

For example: 2, 3, 5, 7, 11, 13, 17 etc.

NOTE: 2 is the only even prime number.

In this program, we need to print the first 10 prime numbers: 2,3,5,7,11,13,17,19,23,29.

Algorithm

  • STEP 1: START
  • STEP 2: SET ct =0, n =0, i= 1, j=1
  • STEP 3: REPEAT STEP 4 to 12 UNTIL n<10
  • STEP 4: j =1
  • STEP 5: ct =0
  • STEP 6: REPEAT STEP 7 to 9 UNTIL j<=i
  • STEP 7: if i%j==0 then
  • STEP 8: ct = ct+1
  • STEP 9: j =j+1
  • STEP 10: if ct==2 then PRINT i
  • STEP 11: n =n+1
  • STEP 12: i = i+1
  • STEP 13: END

Java Program

public class Prime
{
public static void main(String[] args) 
{
int ct=0,n=0,i=1,j=1;
while(n<10)
{
j=1;
ct=0;
while(j<=i)
{
if(i%j==0)
ct++;
j++; 
}
if(ct==2)
{
System.out.printf("%d ",i);
n++;
}
i++;
}
}
}

Output:

2,3,5,7,11,13,17,19,23,29

C Program

#include <stdio.h>
int main()
{
    int ct=0,n=0,i=1,j=1;
	while(n<10)
	{
		j=1;
		ct=0;
		while(j<=i)
		{
			if(i%j==0)
			ct++;
			j++; 
		}
		if(ct==2)
		{
			printf("%d ",i);
			n++;
		}
		i++;
}
}

Output:

2,3,5,7,11,13,17,19,23,29

Python Program

r=30
for a in range(2,r+1):
    k=0
    for i in range(2,a//2+1):
        if(a%i==0):
            k=k+1
    if(k<=0):
        print(a,end=" ")

Output:

2,3,5,7,11,13,17,19,23,29

C# Program

using System; 
class Prime_Number
{
   public static void Main()
    {
    int ct=0,n=0,i=1,j=1;
	while(n<10)
	{
		j=1;
		ct=0;
		while(j<=i)
		{
			if(i%j==0)
			ct++;
			j++; 
		}
		if(ct==2)
		{
			Console.Write(i);
			Console.Write(" ");
			n++;
		}
		i++;
}
}}

Output:

2,3,5,7,11,13,17,19,23,29

PHP Program

<?php
$ct=0;
$n=0;
$i=1;
$j=1;
while($n<10)
{
$j=1;
$ct=0;
while($j<=$i)
{
if($i%$j==0)
$ct++;
$j++; 
}
if($ct == 2)
{
echo $i;
echo " ";
$n++;
}
$i++;
}
?>

Output:

2,3,5,7,11,13,17,19,23,29
Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf