InterviewSolution
| 1. |
Write a program in Java to display the first 10 terms of the following series i) 24,99,224,399,..... ii) 2,5,10,17,...... |
|
Answer» :CLASS series_1{PUBLIC static void main(String args[]){String c1="4";String c2="99";for(int i=1;i<=10;i++){if(i%2!=0){System.out.print("2"+c1+" , ");c1="2"+c1;}else{System.out.print(c2+" , ");c2="3"+c2;}}//for loop}//main}//classCODE 2:class series_2{public void main(String args[]){for(int i=1;i<=10;i++){System.out.print((i*i+1)+" , ");}}}LOGIC :First series : for odd positions , 2 is added every time before the number.For even positions , 3 is added before the number .__________________________________________Second series :1² + 1 = 22² + 1 = 4 + 1 = 53² + 1 = 9 + 1 = 104² + 1 = 16 + 1 = 17.....Hope it helps !_________________________________________________________ |
|