Computer Science, asked by akshisaha06, 1 year ago

Print the following series using scanner class in java :-

S= 1-3+5-7+9-11.......................n

Answers

Answered by Aadrika2003
2
mport java.util.Scanner;
public class OddSeries 
{
 public static void main(String args[]) 
  {
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter the number of terms: ");
  int n = sc.nextInt();
  int i = 1, c, f = 1;                            
  for (c = 1; c <= n; c++) {
   if (f % 2 == 0) {
    System.out.print(-i + " ");
   } else {
    System.out.print(i + " ");
   }
   i += 2;
   f++;
  }                                                  
 }
}


akshisaha06: Thanks a Lot !!!
Similar questions