Programa para hallar la suma de los primeros n números naturales

Dado un número n, encuentre la suma de los primeros números naturales.

C++

// CPP program to find sum of first
// n natural numbers.
#include<iostream>
using namespace std;
 
// Returns sum of first n natural
// numbers
int findSum(int n)
{
   int sum = 0;
   for (int x=1; x<=n; x++)
     sum = sum + x;
   return sum;
}
 
// Driver code
int main()
{
  int n = 5;
  cout << findSum(n);
  return 0;
}

Java

// JAVA program to find sum of first
// n natural numbers.
import java.io.*;
 
class GFG{
 
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        int sum = 0;
        for (int x = 1; x <= n; x++)
            sum = sum + x;
        return sum;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Nikita Tiwari.

Python

# PYTHON program to find sum of first
# n natural numbers.
 
# Returns sum of first n natural
# numbers
def findSum(n) :
    sum = 0
    x = 1
    while x <=n :
        sum = sum + x
        x = x + 1
    return sum
 
 
# Driver code
 
n = 5
print findSum(n)
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find sum of first
// n natural numbers.
using System;
 
class GFG{
 
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        int sum = 0;
        for (int x = 1; x <= n; x++)
            sum = sum + x;
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.Write(findSum(n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find sum of first
// n natural numbers.
 
// Returns sum of first n natural
// numbers
function findSum($n)
{
$sum = 0;
for ($x = 1; $x <= $n; $x++)
    $sum = $sum + $x;
return $sum;
}
 
// Driver code
$n = 5;
echo findSum($n);
 
// This code is contributed by Sam007
?>

Javascript

<script>
// Javascript program to find sum of first
// n natural numbers.
 
// Returns sum of first n natural
// numbers
function findSum(n)
{
   let sum = 0;
   for (let x = 1; x <= n; x++)
     sum = sum + x;
   return sum;
}
 
// Driver code
let n = 5;
document.write(findSum(n));
 
// This code is contributed by rishavmahato348.
</script>

C++

// Efficient CPP program to find sum of first
// n natural numbers.
#include<iostream>
using namespace std;
 
// Returns sum of first n natural
// numbers
int findSum(int n)
{
   return n * (n + 1) / 2;
}
 
// Driver code
int main()
{
  int n = 5;
  cout << findSum(n);
  return 0;
}

Java

// Efficient JAVA program to find sum
// of first n natural numbers.
import java.io.*;
 
class GFG{
     
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        return n * (n + 1) / 2;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(findSum(n));
    }
}
 
// This code is contributed by Nikita Tiwari.

Python

# Efficient CPP program to find sum
# of first n natural numbers.
 
# Returns sum of first n natural
# numbers
def findSum(n) :
    return n * (n + 1) / 2
     
# Driver code
n = 5
print findSum(n)
 
# This code is contributed by Nikita Tiwari.

C#

// Efficient C# program to find sum
// of first n natural numbers.
using System;
 
class GFG{
     
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        return n * (n + 1) / 2;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.Write(findSum(n));
    }
}
 
// This code is contributed by vt_m.

php

<?php
// Efficient PHP program to find sum
// of first n natural numbers.
 
// Returns sum of first n natural
// numbers
function findSum($n)
{
    return ($n * ($n + 1) / 2);
}
 
// Driver code
$n = 5;
echo findSum($n);
 
// This code is contributed by Sam007
?>

Javascript

<script>
// javascript Program to find the average
// of sum of first n natural numbers
 
// Return the average of sum
// of first n even numbers
function findSum(n)
{
    return n * (n + 1) / 2;
}
var n = 5;
document.write(findSum(n));
 
// This code is contributed by sravan kumar
</script>

C++

// Efficient CPP program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
#include<iostream>
using namespace std;
  
// Returns sum of first n natural
// numbers
int findSum(int n)
{
   if (n % 2 == 0)
      return (n/2) * 1LL * (n+1); // Here multiplying by 1LL help to perform calculations in long long, so that answer should not be overflowed
  
   // If n is odd, (n+1) must be even
   else
      return  ((n + 1) / 2) * 1LL * n; // Here multiplying by 1LL help to perform calculations in long long, so that answer should not be overflowed
}
  
// Driver code
int main()
{
  int n = 5;
  cout << findSum(n);
  return 0;
}

Java

// Efficient JAVA program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
import java.io.*;
 
class GFG{
 
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        if (n % 2 == 0)
            return (n / 2) * (n + 1);
 
        // If n is odd, (n+1) must be even
        else
            return ((n + 1) / 2) * n;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(findSum(n));
    }
}
 
//This code is contributed by Nikita Tiwari.

Python

# Efficient Python program to find the sum 
# of first n natural numbers that avoid
# overflow if the result is going to be
# within limits.
 
# Returns sum of first n natural
# numbers
def findSum(n) :
    if (n % 2 == 0) :
        return (n / 2) * (n + 1)
  
   # If n is odd, (n+1) must be even
    else :
       return  ((n + 1) / 2) * n
        
# Driver code
n = 5
print findSum(n)
 
# This code is contributed by Nikita Tiwari.

C#

// Efficient C# program to find the sum of first
// n natural numbers that avoid overflow if
// result is going to be within limits.
using System;
 
class GFG{
 
    // Returns sum of first n natural
    // numbers
    static int findSum(int n)
    {
        if (n % 2 == 0)
            return (n / 2) * (n + 1);
 
        // If n is odd, (n+1) must be even
        else
            return ((n + 1) / 2) * n;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 5;
        Console.Write(findSum(n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// Efficient php program to find sum of first
// n natural numbers that avoids overflow if
// result is going to be within limits.
 
// Returns sum of first n natural
// numbers
function findSum($n)
{
    if ($n % 2 == 0)
        return ($n / 2) *
               ($n + 1);
     
    // If n is odd, (n+1) must be even
    else
        return (($n + 1) / 2) * $n;
}
 
// Driver code
$n = 5;
echo findSum($n);
 
// This code is contributed by Sam007
?>

Javascript

<script>
//efficient approach using  javascript to find the average
// of sum of first n natural numbers
 
// Return the average of sum
// of first n even numbers
function findSum(n)
{
    if (n % 2 == 0)
        return (n / 2) * (n + 1)
 
// If n is odd, (n+1) must be even
    else
    return ((n + 1) / 2) * n
         
}
var n = 5;
document.write(findSum(n));
 
// This code is contributed by sravan kumar
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *