answersLogoWhite

0


Best Answer

/* 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
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

/*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");

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

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");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

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

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

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.");

}

}

This answer is:
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(); }


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; }


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

Related questions

A function prime that returns if its argument is a prime no or returns zero?

prime number


A program to find that the input number is prime or not?

Use Wolfram|Alpha... go to the related link below, Wolfram|Alpha, and type in (is __ (number) prime) and then the program will compute that and tell you if it is prime or composite.


Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


What is the largest prime no that is stored in 8 bit pattern?

Write your own prime number program and find out.


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Prime numbers between 1 to 10 in microprocessor 8085?

program to find prime number in 8085 microprocessor


Write a java program to display the number is prime or not?

Simply use a for loop (i) that runs from 2 to N-1. Checking if N % i 0 then its a prime number.


With a given big integer number which is the product of two prime numbers how do you find them?

First write a program to generate the prime number. After one prime number was generated, divide the big int number by the prime number. If the remainder is zero then quotient is the second prime number ( also it is important to check whether the quotient is prime number or not because sometimes you will get wrong answer). Repeat the process until you get the result.


What is the Program for composite and prime numbers in c?

#include&lt;iostream.h&gt; #include&lt;conio.h&gt; int main() { int i,n; clrscr(); cout&lt;&lt;"PROGRAM TO CHECK IF THE NUMBER IS PRIME OR NOT:"&lt;&lt;endl; cout&lt;&lt;"Enter a number:"; cin&gt;&gt;n; for(int i=2;i&lt;n;i++) { if(n%i==0) cout&lt;&lt;"\nTHE NUMBER IS COMPOSITE"&lt;&lt;endl; else cout&lt;&lt;"\nTHE NUMBER IS PRIME"&lt;&lt;endl; } return 0; }


Shell program to generate prime number between 1 and 50?

There are several shell programs available for download on the Internet that will generate prime numbers. The best way to find a prime number is through calculation, however.


How do you program if it is a prime or composite?

The easiest, but not the most efficient way, to program this is to take your number "n" and test all the numbers from 2 to n-1 to see if they divide n, if none of them do, then n is prime.


How can I write a program to display prime numbers from 1 to 100?

Write a function that implements an algorithm that checks to see if a particular integer is prime (returning a boolean). Write a program that uses that function on each number from 1 to 100, and if true, displays that number.