I still have errors. Can you PLEASE correct my code?!
An Emirp (prime spelled backward) is a prime number whose reversal is also a prime. For example, 17 is a prime 71 is a prime. So 17 and 71 are emirps. Write a program that displays the first 100 emirps. Display 10 numbers per line and align the numbers properly.
Code Java:
import java.util.*;
public class Exercise05_27{
public static void main(String[] args){
{int number=12,number2,upto=0,i;
int[] prime=new int[100];
while(upto<100)
{if(!check(number,prime,upto))
if(isPrime(number))
{number2=reverse(number);
if(number2!=number)
if(!check(number2,prime,upto))
if(isPrime(number2))
upto=insert(number,number2,prime,upto);
}
number++;
}
sort(prime,upto);
System.out.println("The first 100 EMIRP numbers are:");
for(i=0;i
{if(i%10==0)
System.out.println();
System.out.printf("%7d",prime[i]);
}
}
}
public static void sort(int a[],int n)
{int i,j,t;
for(i=0;i
for(j=i;j
if(a[i]>a[j])
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
public static boolean check(int n,int a[],int c)
{int i;
for(i=0;i
if(a[i]==n)
return true;
return false;
}
public static int insert(int n,int m,int a[],int i)
{
a[i++]=n;
a[i++]=m;
return i;
}
public static int reverse(int n)
{int newnum=0;
while(n>0)
{newnum=newnum*10+n%10;
n=n/10;
}
return newnum;
}
public static boolean isPrime(int n)
{int i;
for(i=2;i
if(n%i==0)
false;
Re: I have the program written, but I still have errors. Can you PLEASE correct my co
In the future could you post your code you code in the body of the text rather than putting it into an attachment? Many people are wary of opening attachments. Posting your code in the body also makes reading the code in context a lot easier. Also, please format your text in a more readable fashion. If you're unsure of how to do this, I would suggest reading Java Code Conventions.
Many popular IDE's have tools which will automatically format your code for you.
What problems are you running into (compile errors, runtime errors, wrong results, etc.)? Could you post the outputs (including any error messages)?
Re: I still have errors. Can you PLEASE correct my code?!
After looking over your code, there are quite a lot of errors.. The main issues are with your for loops.
I suggest you read - The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: I still have errors. Can you PLEASE correct my code?!
Besides, choose a better coding style!
Your code follows no indentations!
Re: I still have errors. Can you PLEASE correct my code?!
Quote:
Originally Posted by
benglish
Besides, choose a better coding style!
Your code follows no indentations!
Yes, this was stated as the first response.
@OP:
Quote:
Originally Posted by
JavaPF