Computer Science, asked by ritesh7542, 6 months ago

write a program to calculate sum of N natural's number​

Answers

Answered by rmdolic11
0

Explanation:

Program to find the sum of first n natural numbers. We will see two C programs to calculate the sum of natural numbers. In the first C program we are using for loop for find the sum and in the second program we are doing the same using while loop.

To understand these programs, you should be familiar with the following C Programming Concepts:

C Programming for loop

C Programming while loop

Example 1: Program to find sum of natural numbers using for loop

The user enters the value of n and the program calculate the sum of first n natural numbers using for loop.

#include <stdio.h>

int main()

{

int n, count, sum = 0;

printf("Enter the value of n(positive integer): ");

scanf("%d",&n);

for(count=1; count <= n; count++)

{

sum = sum + count;

}

printf("Sum of first %d natural numbers is: %d",n, sum);

return 0;

}

Output:

Enter the value of n(positive integer): 6

Sum of first 6 natural numbers is: 21

Answered by bhoomidarak
3

Answer:

import java.util.Scanner;

class calc

{

public static void main()

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value for n");

int n =sc.nextInt();//accepts value from the user

int sum=0;//initializing sum with zero

for (int x=1; x<=n; x++)//starting from 1 because natural no.'s start from 1

{

sum+=x;

}//end of for loop

System.out.println("The sum of " + n + "natural numbers is " + sum);

}//end of main

}//end of class

Similar questions