Here the code for get three digit value 'n' and calculate r=2^n+1 then find that value 'r' is a prie or not, here i like store the integer value 'r' without exponential function and big Integer, please make a great help to me.
thanks in advance..


import java.*;
import java.io.*;
import java.io.IOException.*;
import java.math.*;


public class primenumber extends power{

public static void main(String a[])throws IOException {
//get input
power ob=new power();
DataInputStream dis=new DataInputStream(System.in);
System.out.println(" Enter three digit number n:");
String nu=dis.readLine();
int n=Integer.parseInt(nu);
long nvalue=ob.powerr(n);


//find prime
long r;
int primevalue=0,i,j=0;
long factors[]=new long[100];
for(i=2;i<=nvalue;i++)
{r=nvalue%i;

if(r==0)
{
if(i!=nvalue)
{factors[j]=i;
j++;
primevalue=1;
//System.out.println("not prime");
}//inner if
else
{
//System.out.println("prime");
}
}//outer if
}//for ends
System.out.println("Given number is"+n+"");
System.out.println("Prime calculation for"+nvalue+"");
System.out.println("power value is"+nvalue+"");

if(primevalue!=1){
System.out.println("The number "+nvalue+" is prime");
}
else{
System.out.println("The number "+nvalue+" is not a prime");
System.out.println("factors");
for(int k=0;k<=j-1;k++)
System.out.print("," +factors[k]);
}//primevalueif


}//main end here
}//class end here


class power {
//find power
final int two=2;
long powvalue=1 , i;
long powerr(int x){
int n=x;
for(i=1;i<=n+1;i++)
{powvalue*=two;
}
return powvalue;
};
}

Recommended Answers

All 6 Replies

I'm a bit confused here.
don't really see a question in your post, what is it you're having trouble with?

please try this with the three digit input ..

try what? what are you stuck with? what is it supposed to do and what is it (not) doing?

could you be a bit more specific about these things?

Sample output:1
Enter three digit number n:
Given number is13
Prime calculation for16384
power value is16384
The number 16384 is not a prime
factors
,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192
BUILD SUCCESSFUL (total time: 9 seconds)

Sample output:2
run:
Enter three digit number n:
Given number is26
Prime calculation for134217728
power value is134217728
The number 134217728 is not a prime
factors
,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864
BUILD SUCCESSFUL (total time: 9 seconds)


if you give 31 (or) any big value as a input, system could not give output.Because prime calculation number "nvalue" will be a very big integer.

i gave 31 as an input and i got bug form system, please go through ..
Sample output:3
run:
Enter three digit number n:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at primenumber.main(primenumber.java:40)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 minutes 2 seconds)

if you are asked: enter a three-digit number, why do you only test with two-digit numbers? that Exception says that on line 40 of the code in your main class, you are dividing something by zero, which causes your exception

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.