Skip to content

Commit fb7e4d6

Browse files
committed
feat: Reverse copy an array by iterating from end of source to beginning
🧠 Logic: - Given array `A` is copied into array `B` in reverse order. - Loop runs from end of `A` (`A.length-1`) to start, while inserting into `B` from index 0 onward. - Demonstrates how to reverse an array during the copy process. - Final result is printed using an enhanced for-loop. Signed-off-by: Somesh diwan <[email protected]>
1 parent 5a2ea17 commit fb7e4d6

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed
Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
1-
//Program to Print First 10 Natural Numbers in Reverse Order Using while Loop\
2-
3-
//public class ReverseNumberPrint {
4-
// public static void main(String[] args)
5-
// {
6-
// int i = 10; // Initialize the counter to 10
7-
// while (i >= 1)
8-
// {
9-
// System.out.println(i); // Print the number
10-
// i--; // Decrement the counter
11-
// }
12-
// }
13-
//}
14-
151
import java.util.Scanner;
162

173
public class ReverseNumberPrint {
18-
public static void main(String[] args)
19-
{
4+
public static void main(String[] args) {
205
Scanner scanner = new Scanner(System.in);
216

22-
//Ask user for the number of natural numbers to print
23-
System.out.print("Enter the number of natural numbers to print in reverse order: ");
7+
//Ask the user for the number of natural numbers to print
8+
System.out.print("Enter the number, to print natural numbers in reverse order: ");
249
int n = scanner.nextInt();
2510

2611
System.out.println("The first " + n + " natural numbers in reverse order are:");
27-
for (int i = n; i >= 1; i--)
28-
{
12+
for (int i = n; i >= 1; i--) {
2913
System.out.print(i + " ");
3014
}
3115
scanner.close();
3216
}
3317
}
3418

19+
/*
20+
Do-While:
3521
36-
//Do-While
37-
38-
// Initialize the loop variable
39-
40-
// int i = n;
22+
Initialize the loop variable
4123
42-
// Use a do-while loop to print numbers in reverse order
24+
int i = n;
4325
44-
// do {
45-
// System.out.print(i + " ");
46-
// i--; // Decrement the counter
47-
// } while (i >= 1);
48-
// scanner.close();
26+
Use a do-while loop to print numbers in reverse order.
4927
28+
do {
29+
System.out.print(i + " ");
30+
i--; // Decrement the counter
31+
} while (i >= 1);
32+
scanner.close();
5033
51-
//While
34+
While:
5235
53-
// Initialize the loop variable
36+
Initialize the loop variable
5437
55-
// int i = n;
38+
int i = n;
5639
57-
// Use a while loop to print numbers in reverse order
40+
Use a while loop to print numbers in reverse order
5841
59-
// while (i >= 1) {
60-
// System.out.print(i + " ");
61-
// i--; // Decrement the counter
62-
// }
63-
// scanner.close();
42+
while (i >= 1) {
43+
System.out.print(i + " ");
44+
i--; // Decrement the counter
45+
}
46+
scanner.close();
47+
*/

0 commit comments

Comments
 (0)