PRIME NUMBER
************************************************************************
W.A.P TO GENERATE A LIST OF PRIME NUMBERS
************************************************************************
JAVA Program
import java.util.Scanner;class Prime{
Scanner sc= new Scanner(System.in);
public void p1ton()
{
int i=0;
int num=0;
System.out.println("\nEnter upper limit: ");
int ul=sc.nextInt();
String primeNumbers ="";
for( i=1; i<=ul; i++){
int counter=0;
for (num=i; num>=1; num--){
if(i%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("\nPrime numbers from 1 to 100 are :\n");
System.out.println(primeNumbers);
}
public void usern()
{
int num=0;
int counter=0;
System.out.println("\nEnter the number ");
int temp=sc.nextInt();
for (num=temp; num>=1; num--){
if(temp%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
System.out.println("\nPrime number");
}
else{
System.out.println("\nNot a Prime number");
}
}
public static void main(String[] args){
Scanner s= new Scanner(System.in);
Prime obj = new Prime();
System.out.println("\n1. 1 to n Prime Number ");
System.out.println("2. Prime Number check");
System.out.println("\nEnter your choice: ");
int ch=s.nextInt();
if (ch==1)
obj.p1ton();
else if (ch==2)
obj.usern();
else
System.out.println("\nTry Again.");
}
}
Online compiler
Output:
************************************************************************
C Program
// Online C compiler to run C program online
#include <stdio.h>
void p1ton()
{
int i=0,counter;
int num=0,ul;
printf("\nEnter upper limit: ");
scanf("%d",&ul);
printf("\nPrime numbers from 1 to %d are :\n",ul);
for( i=1; i<=ul; i++){
counter=0;
for (num=i; num>=1; num--){
if(i%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
printf("%d ",i);
}
}
}
void usern()
{
int num=0,temp;
int counter=0;
printf("\nEnter the number ");
scanf("%d",&temp);
for (num=temp; num>=1; num--){
if(temp%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
printf("\nPrime number");
}
else{
printf("\nNot a Prime number");
}
}
int main() {
int ch;
printf("\n1. 1 to n Prime Number ");
printf("2. Prime Number check");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if (ch==1)
p1ton();
else if (ch==2)
usern();
else
printf("\n Try Again.");
return 0;
}
Online Compiler
************************************************************************
C++ Program
// Online C++ compiler to run C++ program online
#include <iostream>
void p1ton()
{
int i=0,counter;
int num=0,ul;
std::cout<<"\nEnter upper limit: ";
std::cin>>ul;
std::cout<<"\nPrime numbers from 1 to "<<ul<<" are :\n";
for( i=1; i<=ul; i++){
counter=0;
for (num=i; num>=1; num--){
if(i%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
std::cout<<" "<<i;
}
}
}
void usern()
{
int num=0,temp;
int counter=0;
std::cout<<"\nEnter the number ";
std::cin>>temp;
for (num=temp; num>=1; num--){
if(temp%num==0)
{
counter = counter +1 ;
}
}
if (counter == 2)
{
std::cout<<"\nPrime number";
}
else{
std::cout<<"\nNot a Prime number";
}
}
int main() {
int ch;
std::cout<<"\n1. 1 to n Prime Number ";
std::cout<<"2. Prime Number check";
std::cout<<"\nEnter your choice: ";
std::cin>>ch;
if (ch==1)
p1ton();
else if (ch==2)
usern();
else
std::cout<<"\n Try Again.";
return 0;
}
Online Compiler
************************************************************************
C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace HelloWorld
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
Hello World!
************************************************************************
PHP Program
<?php
function palindrome($n){
$num = $n;
$sum = 0;
while (floor($num)){
$rem= $num %10;
$num = $num /10;
$sum = $sum * 10 + $rem;
}
return $sum;
}
$num = (int)readline('Enter the Number: ');
$n = palindrome($num);
if($num == $n){
echo "$num is a palindrome number";}
else{
echo "$num is not a palindrome number";}
?>
Php is a serverside scripting language.
************************************************************************
JAVASCRIPT Program
function isPrime(num) {
for(var i = 2; i < num; i++)
if(num % i === 0) return false;
return num > 1;
}
// console.log(isPrime(499))
console.log("1 to n Prime Number")
// var s = window.prompt("Enter starting value: ");
// var e = window.prompt("Enter ending value: ");
// for(var n=s; n<=e; n++) replace this with next line
for(var n=1; n<=499; n++)
if (isPrime(n))
console.log(n)
console.log("Prime Numbre Checker")
// var val = window.prompt("Enter value: "); console.log(isPrime(val));
console.log(isPrime(11)) // skip with previous code
check this output in a separate file in .html code, and remove // and understand where put // and run this program.
************************************************************************
PYTHON Program
def p1ton():
start = int(input("Enter starting value: "))
end = int(input("Enter ending value: "))
for i in range(start, end+1):
if i>1:
for j in range(2,i):
if(i % j==0):
break
else:
print(i)
def usern():
counter=0;
num=int(input("Enter the number: "))
flag = False
if num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
ch=int(input("\n1. 1 to n Prime Number \n2. Prime Number check\nEnter your choice: "))
if ch==1:
p1ton();
elif ch==2:
usern();
else:
print("\n Try Again.");
************************************************************************
Comments
Post a Comment