import java.util.Scanner;
public class AlternativeReverse {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = in.nextInt();
int t_num = num;
for (int i = 1; i <= num; i++) {
System.out.printf("%-5d - %5d %n", t_num, i);
t_num--;
}
}
}
Or you can useEnter Number: 5
5 - 1
4 - 2
3 - 3
2 - 4
1 - 5
System.out.printf("%d%d", t_num, i);
5142332415
hint:
Sa array, you access values by their INDEX....right?
- from index ZERO pataas for Asccending
- from HIGHEST index down to ZERO for Descending
- from middle down to zero...or middle up to highest index
Halimbawa:
[5,1,4,2,3]
index 0 = 5
index 1 = 1
index 2 = 4
index 3 = 2
index 4 = 3
clue: use "for loop"
Subukan mo, man.
try this
Output:Code:import java.util.Scanner; public class AlternativeReverse { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter Number: "); int num = in.nextInt(); int t_num = num; for (int i = 1; i <= num; i++) { System.out.printf("%-5d - %5d %n", t_num, i); t_num--; } } }
Or you can use
to change the output just like your example.Code:System.out.printf("%d%d", t_num, i);