🔒 Closed Help :d

Status
Not open for further replies.

OdenDi

Fanatic
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.
 
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
 
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.

About this Thread

  • 5
    Replies
  • 842
    Views
  • 3
    Participants
Last reply from:
codyscott

Trending Topics

Online now

Members online
917
Guests online
3,556
Total visitors
4,473

Forum statistics

Threads
2,306,443
Posts
29,171,755
Members
1,194,826
Latest member
rqhafjetj
Back
Top