# Field Manual for Hackers

## java - Write a program to compute the sum of first ‘N’ terms (n>=1) of the series 1-3+5-7+9……………………

Write a program to compute the sum of first ‘N’ terms (n>=1) of the series
1-3+5-7+9……………………

Hint:   k=-1    pow(k,i+1)

1. import java.util.Scanner;

class Test{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
int num,sum=0,k=1;
System.out.println("Enter Number");
num=sc.nextInt();

for(int i=1;i<=num;i+=2){

if(k%2==0){sum-=i;System.out.printf("-%d",i);}
else{sum+=i;System.out.printf("+%d",i);}
k++;
}

System.out.printf(" = %d",sum);

}
}

2. import java.util.Scanner;

class Test
{

public static void main(String ar[])
{

int flag=0,i=1,j=3,limit,sum=1;
System.out.print("Enter the limit: ");
Scanner w=new Scanner(System.in);
limit=w.nextInt();
System.out.print("1");
for(i=1;i<limit;i++,j+=2)
{
if(flag==0)
{
flag=1;
System.out.print("-"+j);
sum-=j;
}
else
{
flag=0;
System.out.print("+"+j);
sum+=j;
}
}
System.out.print("\n The sum is: "+sum);

}
}

3. import java.util.Scanner;

class SeriesN{

public static void main(String ar[])
{

Scanner sc=new Scanner(System.in);
int Number,Total=0,a=1;
System.out.println("Enter Number");
Number=sc.nextInt();

for(int i=1;i<=Number;i=i+2){

if(a%2==0)
{
Total=Total-i;
System.out.print("-"+i);
}
else
{
Total=Total+i;
System.out.print("+"+i);
}
a++;
}

System.out.print("="+Total);

}
}

4. import java.util.Scanner;

public class series {
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int n;
System.out.println("enter the value of n upto which u wnt sum");
n = scan.nextInt();
int sum=0;
for(int i=0;i<=n;i++)
{
if(i%2!=0)
sum+=i;
}
System.out.println("the sum of the series is : "+sum);
}
}

5. package forloop;
import java.util.*;
public class Nterms
{
public static void main(String args[])
{
int i,n,sum=0;
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(i=1;i<=n;i+=2)
{
sum=sum+i/2;
}
System.out.println(sum);
}
}

6. import java.util.Scanner;
class Series{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int i,num,sum=0,sum2=0,j;
System.out.println("enter the number");
num=obj.nextInt();
for(i=1;i<=2*(num)-1;i=i+4)
sum=sum+i;

for(j=3;j<=2*(num)-1;j=j+4)
sum2=sum2+j;
System.out.println("sum is "+(sum-sum2));
}

}

7. import java.util.Scanner;
class Series{
public static void main(String ar[]){
Scanner s=new Scanner(System.in);
int n,pos,sum=0,i=1;
System.out.print("enter the numer of terms:");
n=s.nextInt();

for(pos=1;pos<=n;pos++)
{
if(pos%2==0)
{
sum=sum-i;
}
else
{
sum=sum+i;
}
i+=2;
}
System.out.print("sum: "+sum);
}
}

8. import java.util.Scanner;
class Series{
public static void main(String args[]){
int n,i=0,sum=0,j=1;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the value");
n=scan.nextInt();
for(i=1;i<=n;i++)
{sum=0;
if(i%2==0)
{
sum=sum-j;
System.out.print(+sum);
}
else if(j!=1)
{
sum=sum+j;
System.out.print("+"+sum);
}
else
{
sum=sum+j;
System.out.print(+sum);
}
j+=2;

}
}
}

9. import java.util.Scanner;

public class SUM{
public static void main(String args[]){

int n,i=1,sum=0;
Scanner SUM=new Scanner(System.in);
System.out.println("Enter Valu of n");
n=SUM.nextInt();

for(int pos=1;pos<=n;pos+=2){

if(pos%2==0){

sum=sum-i;
}else{

sum=sum+i;
}

}

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

}