Java Program to Print the Series 24 99 224 399 624 899 …N

In the previous article we have discussed about Java Program to Find the Sum of Series 1 + (1+2) + (1+2+3) + … + (1+2+3+4…+N)

In this article we are going to see how to print the series 24 99 224 399 624 899 …N by using Java programming language.

Java Program to Print the Series 24 99 224 399 624 899 …N

On observing the pattern carefully, we can see that first term is ((square of 5) -1) after that every term is incremented by 5 like

1st term = 5^2 – 1 = 24

2nd term = 10^2 – 1 = 99

3rd term = 15^2 – 1 = 224

Example:

Suppose value of n = 4
Then the term is-
24, 99, 224, 399

Let’s see different ways to print the series 24 99 224 399 624 899 …N.

Method-1: Java Program to Print the Series 24 99 224 399 624 899 …N By Using For Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Declare an integer variable say ‘count‘ and initialize it to 1.
  • Use a for loop from i=1 to i<=100 (incremented by 5)
  • Declare an integer variable say ‘term‘ inside the for loop which will hold the value of (Math.pow(i, 2) - 1) for every iteration.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of 'n' ");
        int n = s.nextInt();
        //for loop to print the series
         int count = 1;
        for (int i = 5; i <= 100; i = i + 5) 
        {
            int term = (int)(Math.pow(i, 2) - 1);
            System.out.print(term + ", ");
            if(count == n)
                break;
                count++;
        }
    }
}
Output:

Enter the value of 'n' 
3
24, 99, 224,

Method-2: Java Program to Print the Series 24 99 224 399 624 899 …N By Using While Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Declare an integer variable say ‘count‘ and initialize it to 1.
  • Declare and initialize an integer variable i=1.
  • Then continue a while loop till i<=100 (incremented by 5)
  • Declare an integer variable say ‘term‘ inside the for loop which will hold the value of (Math.pow(i, 2) - 1) for every iteration.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of 'n' ");
        int n = s.nextInt();
        // while loop to print the series
        int count = 1;
       int i = 5;
       while(i <= 100)
       {  
            int term = (int)(Math.pow(i, 2) - 1);
            System.out.print(term + ", ");
            if(count == n)
                  break;
            count++;
            i=i+5;
        }
    }               
}
Output:

Enter the value of 'n' 
3
24, 99, 224,

Method-3: Java Program to Print the Series 24 99 224 399 624 899 …N By Using User Defined Method

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Call a user defined method say printSeries() by passing n as parameter.
  • Then inside method declare an integer variable say ‘count‘ and initialize it to 1.
  • Use a for loop from i=1 to i<=100 (incremented by 5)
  • Declare an integer variable say ‘term‘ inside the for loop which will hold the value of (Math.pow(i, 2) - 1) for every iteration.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
   {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of 'n' ");
        int n = s.nextInt();
       // calling printSeries() method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        //for loop to print the series
         int count = 1;
        for (int i = 5; i <= 100; i = i + 5) 
        {
            int term = (int)(Math.pow(i, 2) - 1);
            System.out.print(term + ", ");
            if(count == n)
                  break;
            count++;
        }
    }
}
Output:

Enter the value of 'n' 
4
24, 99, 224, 399,

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: