Program to Print Integer in Java

class Integers {
public static void main(String[] arguments)
    {
      int c; //declaring a variable
      /* Using for loop to repeat instruction execution */
      for (c = 1; c <= 10; c++) 
      { 
          System.out.println(c);
       }
    }
}

Let’s break this given Java program down step-by-step.

Explanation

 Step 1- class Integers

That defines a class called Integers. Generally speaking, this is the top building block used in Java and a way to represent a program by structure. And, as has already been noticed, this structure has the form of a file main method.

Step2 – public static void main(String[] arguments)

  • This is the main method. This is where every Java program starts.
  • public: This is the keyword for public accessibility. It’s declared as a method that can be called from everywhere so that JVM will be able to call it and start a program.
  • static: The static keyword implies that a method can be invoked without actually instantiating a class, creating an object for the class.
  • void: This means the main method does not return any value.
  • String[] arguments: This is an argument that can accept command-line arguments (though it’s not used in this program). It is a list of strings that would allow the arguments to be accessed if they were passed when the program was launched.

 3. int c;

– This line declares a variable c of type int (integer). This variable will be used as the counter in the for loop.

 4. /* Using for loop to repeat instruction execution */

This is a comment explaining that in the following code, a for loop will be used to repeat an instruction several times. Comments in Java are used to explain the code to anyone reading it, and they do not affect the program’s execution.

 5. for (c = 1; c <= 10; c++) {. }

  • This for loop will repeat a block of code (the System.out.println(c); line) 10 times.
  • c = 1: The loop starts by setting c to 1. This is the initial value of c.
  • c <= 10: This is the condition that the loop checks before each iteration. The loop will continue running if c is less than or equal to 10.
  • c++: This is the increment step. After each iteration, the value of c is increased by 1.

 6. System.out.println(c);

Inside the for loop, this statement prints the value of c to the console. Each time the loop runs, the current value of c will be printed, followed by a new line.

 7. Closing braces 

  • The first closing brace } marks the end of the for loop.
  • The closing brace } at the end of the main method and the Integers class.

Output:

The program will print the value of c (from 1) on a new line every time, from 1 to 10, since the loop will run 10 times and c will increase by 1 during each iteration.

Therefore, the output of the program will be as follows:

1

2

3

4

5

6

7

8

9

10

Every number is printed on a new line due to System.out.println(c) inside the for loop.

Leave a Comment

Your email address will not be published. Required fields are marked *