-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathAlternateTenSeries.java
More file actions
39 lines (35 loc) · 837 Bytes
/
AlternateTenSeries.java
File metadata and controls
39 lines (35 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.java.series;
/*
* Write the program to print the following series
*
* 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10
*
* Idea is very simple
* Have only one for loop
*
* variable starts with 1
* one variable increment by 1 in every iteration
*
* variable starts with 10
* another variable decrement by 1 in every iteration
*/
public class AlternateTenSeries {
public static void main(String[] args) {
int a = 10;
int b = 1;
System.out.print("The Series is : ");
for(int i=0;i<10;i++,a--,b++)
System.out.print(a+" "+b+" ");
System.out.print("\nAlternate approach : ");
a = 10;
for(int i=0;i<10;i++)
System.out.print((a-i)+" "+(i+1)+" ");
}
}
/*
OUTPUT
The Series is :
10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10
Alternate approach :
10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10
*/