Question

write a java program to find the sum of natural numbers from 1 to 1000 inclusive...

write a java program to find the sum of natural numbers from 1 to 1000 inclusive (using for loop)

sample output:

sum=500500

Homework Answers

Answer #1

The program find the sum of natural number from 1 to 1000:

Code:

public class SumNatural {

public static void main(String[] args) {

int num = 1000, sum = 0;

for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}

System.out.println("Sum = " + sum);
}
}

-----------------------------------------------------------------

The code finds out the sum of the natural numbers from 1 to 1000

Output is : 500500

---------------------------------------------------------Please Upvote--------------------------------------------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a python program to find the sum of the first n natural numbers, where the...
Write a python program to find the sum of the first n natural numbers, where the value of n is provided by the user. What is the sum of the first 200 numbers? Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float. What is the average of 10.2,...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the user. Step 2: Modify the previous program so that it asks the user how many numbers they have, inputs that many numbers, and finally outputs the average of their numbers. The messages to the user should be clear throughout.
Please code C# 7. Write a program that can calculate the sum of all numbers between...
Please code C# 7. Write a program that can calculate the sum of all numbers between 1 and 1000 inclusive (including 1 and 1000)
Write a Java program that gets a 4x4 array of numbers and that calculates the sum...
Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner. Examples of resulting programs are: Enter a 4x4 matrix row by row: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 The sum of numbers in the diagonal is: 16 The average of numbers in...
Using for loops: - write a Java program that adds numbers between 1 to 100 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and prints the result. - write a Java program that adds odd numbers between 1 to 100 and prints the result - write a Java program that averages even numbers between 5 to 30 and prints the result
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that...
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that are divisible by 5.
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a complete Java program to recursively find the SUM from 0 to N for a...
Write a complete Java program to recursively find the SUM from 0 to N for a non-negative integer N. Also, design an exception class to handle the negative number entered by the user.
Python code Write a complete program with for loop which calculates the sum of the numbers...
Python code Write a complete program with for loop which calculates the sum of the numbers from 1 to 100
Write a program in C that does the following: Reads 10 real numbers from the keyboard...
Write a program in C that does the following: Reads 10 real numbers from the keyboard using a loop and displays the sum of the numbers and the sum of the square of the numbers.