Computer Science, asked by kamalrajatjoshi94, 2 months ago

Challenge for Programmers!!
Write a program in Java to print the series
0,3,8,15,24.n,upto n terms without using logic :-
 {a}^{2}  - 1
Don't use the logic square of number-1,use other logic.

Answers

Answered by anindyaadhikari13
4

Answer:

The given co‎de is written in Java.

import java.util.*;

public class Series {

   public static void main(String args[])  {

       int i,n,a=0,b=3;

       System.out.print("Enter limit - ");

       n=(new Scanner(System.in)).nextInt();

       for(i=1;i<=n;i++,a+=b,b+=2)

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

   }

}

Logic ?

  • Logic for the series is very easy. The difference between each term in this series is - 3,5,7,9 and so on. Also, the difference between each term is in an A.P.
  • So, here, I have declared two variables a and b. a is the term of the series and b is the difference between each term.
  • Initially, a = 0 as first term is 0 and b = 3 as first difference is 3.
  • After each iteration of for-loop, b is added to a and value of b is incremented by 2 so as to get the desired result.

Variable Description:

\boxed{\begin{array}{c|c|c}\tt\underline{{N}ame}:&amp;\tt \underline{Type}:&amp;\tt\underline{Function}:\\ \tt i&amp;\tt int&amp;\tt {L}oop\: Variable.\\ \tt n&amp;\tt int&amp;\tt Stores\: limit.\\ \tt a&amp;\tt int&amp;\tt Term\:of\:the\:series.\\ \tt b&amp;\tt int&amp;\tt Difference\: between\:terms.\\\end{array}}

Refer to the attachment for output.

•••♪

Attachments:
Similar questions