Write the program in Java to display the first ten terms of the following series: 1, 4, 9, 16,

Categories: Core Java Java 8(JDK1.8) Interview questions and answers Experienced Freshers Java Lambda NCERT Series Programming

package r4rin.com.Series;

/*

 * Write the program in Java to display the first ten terms of the following series:

1, 4, 9, 16,

 */

import java.util.stream.Stream;

public class FirstTenTerms {

public static void main(String[] args) {

// First ways before jdk 1.8

for(int i=1;i<=10;i++) {

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

}

System.out.println();//New Line

//Second ways

//After Jdk 1.8 Using Stream APIs

//Stream.iterate(initial value, next value)

Stream.iterate(1,n->n+1).limit(10).forEach(i->System.out.print(i*i+" "));

}

}

Output:-

1 4 9 16 25 36 49 64 81 100 

1 4 9 16 25 36 49 64 81 100 

Note:-

  1. Here we use two ways to generate series.
  2. You can use any one But the best is using Stream API It will work JDK 1.8 or after all versions of java.
  3. It will not work JDK 1.7 or before versions.

Top articles
Core Java Indroductions Published at:- Features of Java Published at:- Difference between Java and C Published at:- Difference between Java and C++ Published at:- Advantage and Disadvantage of Java Published at:- What is Java Published at:- Why Java is not Pure Object Oriented Language Published at:- Where and why Java is used Published at:- Java has Following Features Characteristics Published at:- Common Misconception about Java Published at:- 3 most commonly used tools are Published at:- Stream Creation-Jav 8.0(JDK1.8) Published at:- Data Type in Java-Size in Bytes & Bits . Published at:- Operators in Java-Assignment, increment and decrement, equality, arithmetic ,relational , logical, bit-wise, compound assignment Published at:- Java Control Flow of Statements Published at:- Java switch case and conditional operator Selection Statement Published at:- for and while Loop-Java Published at:- Java General Interview Questions and Answer Published at:- Write a program in Java to display the first 10 terms of the following series: 10, 20, 30, 40, …….. Published at:- Write the program in Java to display the first ten terms of the following series: 1, 4, 9, 16, Published at:- Write a program in Java to find the sum of the given series: Published at:- Write a program in Java to find the sum of the given series: Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.