InterviewSolution
| 1. |
Write a java program to print the sum of the following series. 0, 3, 8, 15 …. Upto n terms. |
|
Answer» a program for the above question is as follows: Explanation: public class Series // Class definition { public static void main(String[] ARGS) // Main FUNCTION declaration { int i=0,series=0,s=0; // variable declaration while(i<10) // while loop to REPEAT the process 10 times. { System.out.print(series+", "); // Print the series series=series+3+s; // Make the series i=i+1; // INCREMENT operator s=s+2; // increase for series } } } Output: The user get "0, 3, 8, 15, 24, 35, 48, 63, 80, 99," as output because it is a series which is demand by the question. CODE Explanation: The above series is like this-- 0, 0+3, (0+3)+3+2, (0+3+3+2)+3+2 and so on. Then the above program declares a while loop which executes 10 times. Then it takes one variable which prints the value after adding the previous value and 3 and the s variable ( which starts from 0 and adds 2 value in previous value in every iteration of while loop). |
|