Java Program to print N Whole numbers

Description

Get input n and print  whole numbers till n.
Input:
7
Output:
The first Whole numbers are:
0 1 2 3 4 5 6 7

Solution

import java.util.*;
class Whole
{
   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 First Whole Numbers are:");
	for(i=0;i<=lim;i++)
	 {
		System.out.print(" "+i);
      }
    }
}

Explanation

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

Here i is initialized to 0 and incremented by 1 for each iteration, instructions inside the for block are executed unless i becomes greater than lim.  so value of i will be printed like 0 1 2 3 .... n 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