What's new

Closed Help :d

Status
Not open for further replies.

OdenDi

Honorary Poster
Joined
Jun 22, 2016
Posts
314
Reaction
61
Points
144
Age
26
Good Day mga Ka PH :)
How do I make a program alternative reverse in Java?


Ex.
input: 5
output: 5142332415

tnx :)
 
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.
 
Last edited:
try this
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--;
        }
    }
}
Output:
Enter Number: 5
5 - 1
4 - 2
3 - 3
2 - 4
1 - 5
Or you can use
Code:
System.out.printf("%d%d", t_num, i);
to change the output just like your example.
5142332415
 
Last edited:
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.

Tnx po sir salamat. :)
 
try this
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--;
        }
    }
}
Output:
Or you can use
Code:
System.out.printf("%d%d", t_num, i);
to change the output just like your example.

Salamat po sa tulong :)
 
OdenDi
Mag ingat lang kasi some of us here are more generous na mag bigay ng solutions than others.
Kung gusto mo solid ang iyong knowledge, do not copy and paste solutions and be done with it.
Pag-aralan mo ang concept and then make your own (there are many ways to a solutions in programming).
 
Status
Not open for further replies.
Back
Top