answersLogoWhite

0

/* Program to print prime numbers up to given number */

import java.util.*;

public class Prime

{

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number : (it prints prime numbers upto your given number)");

int n=sc.nextInt();

System.out.println("The Prime number upto "+n+" are : ");

while (n>=0)

{

int count=0;

for (int i=1;i<=n ;i++)

{

if (n%i==0)

{

count++;

}

}

if (count==2)

{

System.out.print(n+" ");

}

n--;

}

}

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
JudyJudy
Simplicity is my specialty.
Chat with Judy
More answers

/*To find whether a number is prime or not (simplest program)*/

import java.io.*;

class PrimeOrNot

{

public static void main() throws IOException

{

InputStreamReader ir = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(ir);

System.out.println("Enter the number");

double n = Double.parseDouble(br.readLine());

System.out.println();

int k, l=0;

for (k=1; k<=n; k++)

{

if(n%k==0)

{

l=l+1;

}

}

if(l==2)

{

System.out.println();

System.out.println(n+" is a prime number");

}

else

{

System.out.println();

System.out.println(n+" is NOT a prime number");

}

}

}

User Avatar

Wiki User

11y ago
User Avatar

import java.util.*;

class Prime

{

public static void main(String s[])

{

int a,c=0;

Scanner sc=new Scanner(System.in);

System.out.println("enter a number");

a=sc.nextInt();

for(int i=1;i<=a;i++)

if(a%i==0)

c++;

if(c==2)

System.out.println("prime number");

else

System.out.println("not a prime number");

}

}

User Avatar

Wiki User

9y ago
User Avatar

class Prime{

public static void main(String args[]){

int flag=0;

int n=Integer.parseInt(args[0]);

for(int i=1;i<=n;i++){

if(n%i==0)

flag++;

}

if(flag==2)

System.out.println("It is prime");

else

System.out.println("It is not prime");

}

}

User Avatar

Wiki User

12y ago
User Avatar

int n=<desired number>;

int i=2; // using 2 because every no is divisible by 1

boolean prime=false;

loop : while(i<n){

if(n%i==0){

prime = true;

break loop;

}

i++;

}

System.out.println(prime); // prints true if no is prime, false if it's not

User Avatar

Wiki User

12y ago
User Avatar

A single input (the number) would look something like...

public static void main(String args) {

try {

// Get an integer

int num = Integer.parseInt(args)

// Must divide by something... start at two

int denom = 2;

// Keep dividing by larger numbers until there's no remainder

// or there's nothing left to try.

// Could probably stop even sooner, but just a sample answer

while(num % denom != 0 && denom < num){

denom++;

}

// If the while loop finished early, it's Optimus!

if(denom < num) {

System.out.println("We have a prime!");

} else {

System.out.println("Not prime... must be Megatron");

}

} catch (ParseException pe) {

System.out.println("Not prime; only integers can be prime. Input an integer number.");

}

}

User Avatar

Wiki User

9y ago
User Avatar

Add your answer:

Earn +20 pts
Q: In java using command line argument write prime number program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a c program to find out the prime numbers between 1 to 500?

To write a C program to find prime numbers between 1 to 500, you can use a nested loop structure. In the outer loop, iterate from 2 to 500, and in the inner loop, check if the number is divisible by any number from 2 to the square root of the number. If it is not divisible by any number other than 1 and itself, then it is a prime number. Print out all prime numbers found within the specified range. Remember to include necessary header files, such as &lt;stdio.h&gt;, and use appropriate logic to implement the program efficiently.


How can you Write a suitable pseudo code to check whether a number is prime or not?

Begin Read num for(i=2; i&lt;num; i++) if(num%2==0) then print "the number is not a prime no."; else if print "the number is prime"; end if Stop


Write a c program to check whether a no is prime or not?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; int i=1, count=0,n; clrscr(); printf("Enter Any Number"); scanf("%d", &amp;n); while(i&lt;n) if(n%i==0) { count++; i++; } if(count==2) printf("Given Number is Prime"); else printf("Given Number is not Prime"); getch(); }


Shell program for finding prime number?

#!/bin/sh i=2 rem=1 echo -e "Enter a number: \c" read num if [ $num -lt 2 ]; then echo -e "$num is not prime\n" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ]; then echo -e "$num is prime\n" else echo -e "$num is not prime\n" fi


Prime number program in C using recursion?

//Program to check number is prime or not using recursive function #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; void prime(int num,int count) { if(count&lt;num) { if(num%count==0) { printf("%d is not Prime Number\n",num); goto exit; } count += 1; if(count&lt;num) { prime(num,count); } } if(num==count) { printf("%d is a Prime Number\n",num); } exit: return 0; } int main() { system("cls"); int gvar; printf("Enter the number = "); scanf("%d",&amp;gvar); prime(gvar,2); printf("\nashokakhil@gmail.com\n"); system("PAUSE"); return 0; } I think this can be another solution #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int prime(int); main() { int i=1,r; clrscr(); r=prime(i); if(r==1) printf("\n\n\tNo is prime "); getch(); } int prime(int i) { int n=1,ans,flag=1; i++; ans=n%i; if(ans==0) { printf("\t\t\n\nNo is not prime"); flag=0; return flag; } if((i!=n-1)&amp;&amp;(n!=1)) flag=prime(i); return flag; }