# Java Program to Display Numbers and Sum of First N Natural Numbers

Print first N natural numbers using an iterative approach i.e. using for loop. For loop has three parameters initialization, testing condition, and increment/decrement.

```Input:     N = 10
Output:    First 10 Numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Sum of first 10 Natural Number = 55

Input:     N = 5
Output:    First 5 Numbers = 1, 2, 3, 4, 5
Sum of first 5 Natural Number = 15```

Approach

1. Start for loop initialization with i = 1.
2. Write testing condition as i <= N.
3. Add increment statement as i++ or i+=1.
4. Initialize a variable sum with 0.
5. Start adding i with the sum at each iteration of for loop and print i.
6. Print sum at the end for loop.

Below is the implementation of above approach

## Java

 `// Java Program to Display Numbers` `// from 1 to N Using For Loop and` `// sum of First N Natural Number` `import` `java.io.*;` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``10``;` `        ``int` `sum = ``0``;` `        ``System.out.print(``"First "` `+ N + ``" Numbers = "``);`   `        ``// we initialize the value of the variable i ` `        ``// with 1 and increment each time with 1` `        ``for` `(``int` `i = ``1``; i <= N; i++) {`   `            ``// print the value of the variable as` `            ``// long as the code executes` `            ``System.out.print(i + ``" "``);` `            ``sum += i;` `        ``}` `        ``System.out.println();` `        ``System.out.println(``"Sum of first "` `+ N` `                           ``+ ``" Natural Number = "` `+ sum);` `    ``}` `}`

Output

```First 10 Numbers = 1 2 3 4 5 6 7 8 9 10
Sum of first 10 Natural Number = 55```

Time Complexity: O(n)
Auxiliary Space: O(1) because constant space for variables is being used

Alternate Approach

1. Start for loop initialization with i = 1.
2. Write testing condition as i <= N.
3. Add increment statement as i++ or i+=1.
4. Start Printing i for each iteration.
5. Print sum using first N natural number formula at the end of for loop.

Below is the implementation of the above approach

## Java

 `// Java Program to Display Numbers` `// from 1 to N Using For Loop and` `// sum of First N Natural Number` `import` `java.io.*;`   `class` `GFG {` `  `  `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `N = ``5``;` `        ``System.out.print(``"First "` `+ N + ``" Numbers = "``);`   `        ``// we initialize the value of the variable i` `        ``// with 1 and increment each time with 1` `        ``for` `(``int` `i = ``1``; i <= N; i++) {` `          `  `            ``// print the value of the variable as` `            ``// long as the code executes` `            ``System.out.print(i + ``" "``);` `        ``}` `        ``System.out.println();` `        ``System.out.println(``"Sum of first "` `+ N` `                           ``+ ``" Natural Number = "` `+ (N*(N+``1``))/``2``);` `    ``}` `}`

Output

```First 5 Numbers = 1 2 3 4 5
Sum of first 5 Natural Number = 15```

Time Complexity: O(n)
Auxiliary Space: O(1) as it is using constant space for variables

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next