sum of squares of alternate prime numbers. - CODER

Artikel Terbaru

test banner

Post Top Ad

Responsive Ads Here

sum of squares of alternate prime numbers.

Share This

   find the sum of squares of alternate prime number


   First we need to understand the question.              

 Hope you guyz understand but still this explanation fot those who didn't understand the question yet.

So lets suppose i want to find prime numbers betweent 1 to 10 ( NOTE : 1 IS NOT CONSIDER AS PRIME NUMBER)

The prime number is ( 2 ,3 ,5 ,7). 

What the question says we need to find the sum of squares of alternate prime number ( Sum means adding , square means multiplying by itself and alternate means skipping next number).

so our ans should be ...we need to take only 2 and 5 because we are starting from 2 and its alternate is 5( we skipped 3) and after that 11 is our alternate number as you know i only took till 10. so 11 is not considerable.

  now our answer is ....2*2+5*5= 4+25= 29 

29 should be our answer.

Here is the code:

import java.util.Scanner;

class  alternatePrimeNumbers {

public static void main(String[] args) {

int i,j,sts=0,mul=0,count=1;

System.out.println("Enter the n value ");
Scanner sc = new Scanner(System.in);
  int num=sc.nextInt();

for(j=2;j<=num;j++)
    {

        for(i=2;i<=j/2 && sts!=1;i++)
        {

            if(j%i==0)
            {

                sts=1;

            }
        }
        if(sts==0)
        {
          System.out.println(j);
  System.out.println();
         
if(count%2!=0)
              {mul=mul+j*j;}
              count++;
        }
        sts=0;
    }
System.out.println("your result is="+mul);
}


}

No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages