- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find the sum of n natural numbers
Create a new variable sum, initialize it with 0. Add it to the 1st element, repeat this up to n (where n is given) using loops.
Example
import java.util.Scanner; public class SumOfNNumbers { public static void main(String args[]){ int sum = 0; System.out.print("Enter the number value:: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for (int i = 0; i<num; i++){ sum = sum +i; } System.out.println("Sum of numbers : "+sum); } }
Output
Enter the number value:: 50 Sum of numbers : 1225
- Related Articles
- Java Program to cube sum of first n natural numbers
- 8085 program to find the sum of first n natural numbers
- Java Program to Display Numbers and Sum of First N Natural Numbers
- Java Program to Find the Sum of Natural Numbers using Recursion
- Program to find sum of first n natural numbers in C++
- Java Program to calculate Sum of squares of first n natural numbers
- PHP program to find the sum of cubes of the first n natural numbers
- Java Program to Calculate the Sum of Natural Numbers
- PHP program to find the sum of the 5th powers of first n natural numbers
- Find the sum of first $n$ odd natural numbers.
- Sum of first n natural numbers in C Program
- Java Program to Find Sum of N Numbers Using Recursion
- Swift Program to Calculate Cube Sum of First n Natural Numbers
- PHP program to calculate the sum of square of first n natural numbers
- C Program for the cube sum of first n natural numbers?

Advertisements