What is the error in the given code? 🤔 | Sololearn: Learn to code for FREE!

+5

What is the error in the given code? 🤔

//WAP to store first 35 odd numbers in an array then print only primes which are between 25 and 50band also print how many primes are present whivh are not between out of range given above. public class Program { public static void main(String[] args) { int a[]=new int [35],c=1; for(int i=0;i<35;i++){ a[i]=c; c+=2; } int k=0,p=0; for(int i=0;i<=35;i++){ for(int j=1;j<=a[i];j++){ if(a[i]%j==0) p++; } if((p==2)&&((a[i]>25)&&(a[i]<50))) System.out.println(a[i]); else if((p==2)&&(!(a[i]>25)&&(a[i]<50))) k++; } System.out.println("No. of primes out of range:-"+k); } }

3/22/2021 1:26:53 PM

⠀⠀⠀ [Inactive]

5 Answers

New Answer

+4

public class Main { public static void main(String[] args) { int a[]=new int[35]; int i,count=0; a[0]=1; for(i=1;i<a.length;i++) { a[i]=a[i-1]+2; } System.out.print("Prime numbers between 25 and 50 are: "); for(int j : a) { int c=0; for(i=1;i<=j;i++) { if(j%i==0) c++; } if(c==2) { if(j>25&&j<50) System.out.print(j+" "); else count++; } } System.out.print("\nPrime numbers not in range: "+count); } } Mr.AnonmyousProgrammer Try this. Previous code has some logical errors.

+2

Just change this line for (int i=0;i<35;i++)

+2

What was the error bro.. except I<35 In Second loop??

+2

On the line 14 : After if statement there are no curly braces. Maybe I was incorrect. Bcuz I am not that expert in java

0

Mr.AnonmyousProgrammer How many primes are in and out of the range 25 ~ 50?