🔒 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
  • 830
    Views
  • 3
    Participants
Last reply from:
codyscott

Trending Topics

Online now

Members online
1,062
Guests online
1,571
Total visitors
2,633

Forum statistics

Threads
2,285,480
Posts
29,030,540
Members
1,218,488
Latest member
gervgerv
Back
Top