Here, we have a basic program example to print the sum of first 10 natural numbers using different languages. This program is created in c language, c++, Java, and Python.
Code to print the sum of natural numbers in C language
#include <stdio.h>
int main()
{
int j, sum = 0;
printf("The first 10 natural number is :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
Code to print the sum of natural numbers in C++ language
#include<iostream>
using namespace std;
int main()
{
int i, sum=0;
cout<<"The first 10 natural numbers are:\n";
for (i=1;i<=10;i++)
{
sum = sum + i;
cout<<i<<endl;
}
cout<<"The sum is: "<<sum;
}
Code to print the sum of natural numbers in Python language
print("The first 10 natural numbers are: \n")
sum = 0
for i in range(1, 11):
sum = sum + i
print(i)
print("The sum is : ", sum)
Code to print the sum of natural numbers in Java language
import java.util.*;
public class Sum {
public static void main(String[] args) {
int i, sum=0;
System.out.println("The first 10 natural numbers are:");
for (i=1;i<=10;i++)
{
sum = sum + i;
System.out.println(i);
}
System.out.println("The sum is: " + sum);
}
}