Program to find greatest prime number in n numbers?
Answers were Sorted based on User's Feedback
Answer / rajani nagandla
Sorry. initialize i=2 directly as 2 is the first prime
number.. no need of checking for 1.
| Is This Answer Correct ? | 10 Yes | 3 No |
Answer / manikandan
<----------------- Import appropriate pkgs ------------->
public class Mainmain
{
public static void main(String[] args) throws IOException
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the no of nos");
int n=Integer.parseInt(br.readLine());
int[] a= new int[n];
System.out.println("Enter the nos");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
Arrays.sort(a);
LOOP: for(int i=n-1;i>=0;i--)
{
int b=2;
boolean c=true;
while(b<a[i])
{
if((a[i]%b)==0)
{
c=false;
continue LOOP;
}
b++;
}
if(c)
{
System.out.println("The largest prime no among the
list is "+a[i]);
break;
}
}
}
}
<----------------- It Works -------------------------->
| Is This Answer Correct ? | 9 Yes | 2 No |
Answer / rajani nagandla
1.Read n
2.lets take variables of type int (i=1,j=1,temp=0,count=0);
3. for (;i<n;i++){
count=0;
while(j<=i){
if((i%j)==0){
count+=1;
j++;
if(count>2)break;
}
}
if(count==2)
temp=i;
}
4.the value in temp after for loop execution will be the
Greatest Prime Number
| Is This Answer Correct ? | 6 Yes | 5 No |
What will be the output of the program? public class Test { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); list.add("2"); list.add("3"); list.add("4"); list.add("5"); System.out.println("size :"+list.size()); for(int i=0;i<list.size();i++) { list.remove(i); } System.out.println("size after:"+list.size()); } }
Which method cannot be overridden in java?
relation between list and linked list
as we know a static method could access static data and static method only. then how could main method call the object of aclass which is not static and other non static data
What are the limitations of procedural programming approach?
What initialize variables?
What is the difference between static and non-static variables?
Explain the difference between scrollbar and scrollpane?
What are the default and parameterized constructors?
what is polymorphism?
How do you invoke a method?
What does mean in regex?