Computer Science, asked by BrainlyProgrammer, 2 months ago

What's the error in the given códe:-


//WAP to store first 35 odd numbers in an array then print only primes which are between 25 and 50 and 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);
}
}

_
Kindly do not spam.​

Answers

Answered by atrs7391
2

Your Rectified Program:

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++){

               //Exception was coming due to the above line

               //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);

       }

   }

P.S: The program you gave had a problem which I solved but still wasn't giving the expected output, so instead of again going through the program, I simply wrote a program that solves it. Hope this isn't a scam for you!

Program that Solves the the Question:

public class Test {

   public static void main(String[] args) {

       int[] odds = new int[36];

       int i = 0;

       int num = 0;

       while(i <= 35) {

           if(num%2 == 1) {

               odds[i] = num;

               i++;

           }

           num++;

       }

       int out_of_range_prime = 0;

       for (int odd : odds) {

           int prime_checker = 0;

           for (int j = 1; j <= odd; j++) {

               if(odd%j == 0) {

                   prime_checker = prime_checker+1;

               }

           }

           if(prime_checker == 2 && odd>25 && odd<50){

               System.out.println(odd);

           }

           else if(prime_checker==2){

               out_of_range_prime = out_of_range_prime+1;

           }

       }

       System.out.println("Prime Numbers out of Range: " + out_of_range_prime);

   }

}

AND YES ON SOLOLEARN IT'S ME.... JUST I CAN'T DM.... :)

Answered by Anonymous
0

Your Rectified Program:

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++){

              //Exception was coming due to the above line

              //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);

      }

  }

P.S: The program you gave had a problem which I solved but still wasn't giving the expected output, so instead of again going through the program, I simply wrote a program that solves it. Hope this isn't a scam for you!

Program that Solves the the Question:

public class Test {

  public static void main(String[] args) {

      int[] odds = new int[36];

      int i = 0;

      int num = 0;

      while(i <= 35) {

          if(num%2 == 1) {

              odds[i] = num;

              i++;

          }

          num++;

      }

      int out_of_range_prime = 0;

      for (int odd : odds) {

          int prime_checker = 0;

          for (int j = 1; j <= odd; j++) {

              if(odd%j == 0) {

                  prime_checker = prime_checker+1;

              }

          }

          if(prime_checker == 2 && odd>25 && odd<50){

              System.out.println(odd);

          }

          else if(prime_checker==2){

              out_of_range_prime = out_of_range_prime+1;

          }

      }

      System.out.println("Prime Numbers out of Range: " + out_of_range_prime);

  }

}

Attachments:
Similar questions