C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print all prime numbers between 1 and 100Prime 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 prime numbers between 1 and 100 only. Algorithm
Java Programpublic class Prime { public static void main(String[] args) { int ct=0,n=0,i=1,j=1; while(n<25) { 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 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 C progam#include <stdio.h> int main() { int ct=0,n=0,i=1,j=1; while(n<25) { 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 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Python Programr=100 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 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 C# Programusing System; public class Prime_Number { public static void Main() { int ct=0,n=0,i=1,j=1; while(n<25) { 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 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 PHP Program<?php $ct=0; $n=0; $i=1; $j=1; while($n<25) { $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 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Next Topic#
|