Series 1

Series 1


Write a program to print the following series: 1,4,9,16,25.......... up to n terms.

/* The given series can be generated by calculating the
 squares of natural numbers.*/

import java.io.*;

class Series1{

public static void main(String args[])throws IOException{

//declare all the variables at the beginning
int n,i,a;

    BufferedReader br = new BufferedReader( 
                new InputStreamReader(System.in));
    System.out.println("Enter the number of terms : ");
    n=Integer.parseInt(br.readLine());

    //loop to generate the number of terms in the series
      for(i=1;i<=n;i++){

        a=i*i;

        System.out.print(a+" ");

    }//end of loop

  }//end of main
}//end of class        

Have something to say? Log in to comment on this post.

0 comments