Question

Sum of Series 2+3+5+7+11+13+….+n

				
					ENTER THE Nth TERM
10
SUM OF SERIES=129
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SumSeries
{
    public static void main()
    {
        int n=0,i=0,j=0,flag=0;
        long sum=0,num=1;
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE Nth TERM");
        n=sc.nextInt();
        for(i=1;i<=n;i++)
        {
            do
            {
                num++;
                flag=0;
                for(j=2;j< num;j++)
                {

                    if(num%j==0)
                    {
                        flag=1;
                        break;
                    }
                }

            }while(flag==1);

            sum=sum+num; 

        }
        System.out.println("SUM OF SERIES="+sum);
    }
}
				
			

Leave a Reply

Your email address will not be published. Required fields are marked *