Java Program to print N Natural numbers in reverse

Description

Get input n and print natural numbers from n in reverse.
Input:
7
Output:
The natural numbers in reverse:
7 6 5 4 3 2 1

Solution

import java.util.*;
class NaturalInRev
{
   public static void main(String args[])
   {
	int lim,i;
	System.out.println("Enter The Limit:");
	Scanner sc=new Scanner(System.in);
     lim=sc.nextInt();
	System.out.println(" The Natural Numbers Is In Reverse Order:");
	for(i=lim;i>=1;i--)
	 {
		System.out.print(" "+i);
      }
    }
}

Explanation

Instruction(s) inside the for block{} are executed repeatedly till  the second expression (i>1) is true.

Here i is initialized to 1 and decremented by 1 for each iteration, instructions inside the for block are executed unless i becomes greater than 1. so value of i will be printed like n...3 2 1 using system.out.println.
Ramesh P Natarajan

Author - Ramesh P Natarajan

Learner, Professor, Technology Trainer, Software Developer and Content Marketer, very much passionate in the field of Software Application Development, Machine Learning and Search Engine Optimization.

Write to: admin@decodeschool.com





Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution