Computer Science, asked by RikCena, 1 year ago

WAP to print the series in JAVA:
1,-3,5,-7,9,-11,..............(+/-)n.

Answers

Answered by QGP
70
import java.util.Scanner;
public class Series
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter the value of n: ");
        int n = sc.nextInt();
        int k=-1;
        
        for(int i=1;i<=n;i+=2)
        {
            k *= -1;
            System.out.print((k*i)+" ");
        }
    }
}

RikCena: this is technically incorrect.
Answered by franktheruler
10

Answer:

import java . util . * ;

class series

{

     Scanner sc = new Scanner ( System . in ) ; // creating a object of class                                                                                        .                                                                                 scanner

     System. out. print ( " Enter the value of n: " ) ;

       int n = sc . nextInt ( ) ;

       int k = -1 ;

       void calculation ( )

      {  

       for ( int i = 1 ; i <= n ; i + = 2)  // for loop

       {

           k * = -1 ;

           System. out. print ( ( k * i ) + " ");

       }

   }

}

public class Main_class

{

 public static void main ( string args [ ] )

   {

       series obj = new series ( ) ; // creating a object of class series

        obj . calculation ( ) ;

   }

}

Similar questions