Challenge for Programmers!!
Write a program in Java to print the series
0,3,8,15,24.n,upto n terms without using logic :-
Don't use the logic square of number-1,use other logic.
Answers
Answered by
4
Answer:
The given code 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:
Refer to the attachment for output.
•••♪
Attachments:
Similar questions