Program to calculate sum of natural numbers in c,Python,c++,javascript,c# .

C program :

Here's a simple C program to calculate the sum of natural numbers:

#include <stdio.h>

int main() {

    int n, i, sum = 0;

 

    printf("Enter a positive integer: ");

    scanf("%d", &n);

 

    for (i = 1; i <= n; ++i) {

        sum += i;

    }

 

    printf("Sum = %d", sum);

    return 0;

}

picture:

Here's a simple C program to calculate the sum of natural numbers:


 


Python Program:

Here's a simple Python program to calculate the sum of natural numbers:

# Program to calculate the sum of natural numbers

 

# User input

num = int(input("Enter a positive integer: "))

 

# Initialize sum and counter variables

sum = 0

counter = 1

 

# Loop to calculate the sum

while counter <= num:

    sum = sum + counter

    counter = counter + 1

 

# Print the sum

print("The sum of first", num, "natural numbers is:", sum)

Explain This Code:

This program takes an input from the user (a positive integer), and then calculates the sum of the first num natural numbers using a while loop. The sum is then printed as the output.

picture:

Here's a simple Python program to calculate the sum of natural numbers:


Write a c++ Program to calculate sum of natural numbers.

Here's a simple C++ program to calculate the sum of natural numbers:

#include <iostream> using namespace std; int main() { int num, sum = 0; cout << "Enter a positive integer: "; cin >> num; for (int i = 1; i <= num; ++i) { sum += i; } cout << "Sum = " << sum << endl; return 0; }

Explaination this code:

This program takes an input from the user (a positive integer), and then calculates the sum of the first num natural numbers using a for loop. The sum is then printed as the output.

picture:

Here's a simple C++ program to calculate the sum of natural numbers:


Write a Javascript Program to calculate sum of natural numbers:

Here's a simple javascript program to calculate the sum of natural numbers:

// Program to calculate the sum of natural numbers


// User input

let num = parseInt(prompt("Enter a positive integer: "));


// Initialize sum variable

let sum = 0;


// Loop to calculate the sum

for (let i = 1; i <= num; i++) {

  sum += i;

}


// Print the sum

console.log(`The sum of first ${num} natural numbers is: ${sum}`);

Explain this code:

This program takes an input from the user (a positive integer), and then calculates the sum of the first num natural numbers using a for loop. The sum is then printed as the output using console.log().

picture:

Here's a simple javascript program to calculate the sum of natural numbers:



Write a c# Program to calculate sum of natural numbers.

Here's a simple C# program to calculate the sum of natural numbers:

using System; namespace SumOfNaturalNumbers { class Program { static void Main(string[] args) { Console.Write("Enter a positive integer: "); int num = int.Parse(Console.ReadLine()); int sum = 0; for (int i = 1; i <= num; i++) { sum += i; } Console.WriteLine("Sum = " + sum); } } }


Explain this code :

This program takes an input from the user (a positive integer), and then calculates the sum of the first num natural numbers using a for loop. The sum is then printed as the output.

picture:

Here's a simple C# program to calculate the sum of natural numbers:




Check program to Check Prime Number in c,Python,c++,c#,Javascript : Click here

Post a Comment

0 Comments