![]() |
Given a number N, the task is to find the sum of the first N natural numbers in PHP. Examples: Input: N = 5 There are different methods to find the sum of first N natural numbers, these are: Table of Content Using for LoopWe will iterate a loop from 1 to N and add all elements to get the sum of first N natural numbers. Example: PHP
Output
Sum of first 5 Natural Numbers : 15 Using Mathematical FormulaeThe mathematical formula to find the sum of first N natural number is N * (N + 1) / 2. Example: PHP
Output
Sum of first 5 Natural Numbers : 15 |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |