# Program to find Sum of N natural numbers

10 May 2023

Write a program to find Sum of N natural numbers

## Description about Program to find Sum of N natural numbers

Get the input from the user for the value of n and then find the sum of first n natural numbers.

e.g.  let the n value = 5

then first 5 natural numbers are 1,2,3,4,5 for which we need to find the sum

Therefore sum of first 5 natural numbers is 1+2+3+4+5 = 15

Input

4

Output

10

## C Program to find Sum of N natural numbers

### Method 1

#include <stdio.h>

int main()

{

int num,sum=0;

printf("Enter the number");

scanf("%d",&num);

for(int i=1;i<=num;i++)

sum=sum+i;

printf("%d",sum);

return 0;

}

### Method 2

#include<stdio.h>

int Sum(int n)

{

if (n!=0)

return n + Sum(n-1);

else

return 0;

}

int main()

{

int num,sum=0;

printf("Enter the number: ");

scanf("%d",&num);

sum=Sum(num);

printf("%d",sum);

return 0;

}

## C++ Program to find Sum of N natural numbers

### Method 1

#include <iostream>

using namespace std;

int main()

{

int num,sum=0;

cout<<"Enter the number: ";

cin>>num;

for(int i=1;i<=num;i++)

sum=sum+i;

cout<<sum;

return 0;

}

### Method 2

#include <iostream>

using namespace std;

int Sum(int n)

{

if (n!=0)

return n + Sum(n-1);

else

return 0;

}

int main()

{

int num,sum=0;

cout<<"Enter the number: ";

cin>>num;

cout<<Sum(num);

return 0;

}

## Java Program to find Sum of N natural numbers

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

int sum= 0;

for(int i=0;i<=num;i++)

sum = sum + i;

System.out.print(sum);

}

}

## Python Program to find Sum of N natural numbers

### Method 1

num=int(input("Enter a number: "))

Sum=0

for i in range(1,num+1):

Sum=Sum+i

print(Sum)

### Method 2

def Sum(num):

if num!=0:

return num + Sum(num-1)

else:

return 0

num=int(input("Enter a number: "))

value=Sum(num)

print(value)

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023