answersLogoWhite

0

package javaapplication1;

public class JavaApplication1 {

public static boolean is_prime(int val){
if (val<0) val*=-1;
if (val<2) return false;
if (val%2==0) return val==2;
int max = (int)Math.sqrt(val)+1;
for (int div=3; divif (val%div==0)
return false;
return true;
}

public static int next_prime(int val) {
do {
++val;
} while (!(is_prime(val)));
return val;
}

public static int prev_prime(int val) {
do {
--val;
} while (!(is_prime(val)));
return val;
}

public static void main(String[] args) {
System.out.print ("List of primes from -50 to 50:\n");
for (int i=-50; i<=50; ++i){
if (is_prime(i)) {
System.out.print (i);
System.out.print (" is prime\n");
}
}

System.out.print ("The next prime after -42 is ");
System.out.println (next_prime(-42));
System.out.print ("The previous prime before -42 is ");
System.out.println (prev_prime(-42));
}
}
User Avatar

Wiki User

10y ago

What else can I help you with?

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

Related Questions

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.


Full form of prime in railway?

PRIME (Pay Roll and Independent Modules)


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

Write your own prime number program and find out.


Was 1 a prime number?

A prime number can be divided, without a remainder, only by itself and by 1. Zero and 1 are not prime numbers.


How do you check if a large number is prime or a composite number?

You divide the number by 2. If the number is able to divide WITHOUT a remainder, then it is a prime number. If you divide a number by 1, you will get the same number. That is not a prime number.


What number of prime number?

A prime number can be divided, without a remainder, only by itself and by 1 The question needs for information!


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


How do you figure the prime factor of a number?

the number can be divided without a remainder


What is the prime fartorization list?

You can't have prime factorization without having a number to factor.


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.