i want a simple java program which will show the sum of first n numbers....

View Answers

December 26, 2011 at 3:24 PM

import java.util.*; public class SumOfNumbers { public static void main(String[]args){ Scanner input=new Scanner(System.in); System.out.print("Enter value of n: "); int n=input.nextInt(); long sum=0; for(int i=1;i<=n;i++){ sum+=i; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum); } }

Related Tutorials/Questions & Answers:

Sum of first n numbers i want a simple java program which will show the sum of first n numbers.... import java.util.*; public class...; } System.out.println("Sum of Numbers from 1 to "+n+" : "+sum

("Sum of positive numbers: "+sum2); System.out.println("Sum of negative...Sum of positive and negative numbers Hi if someone could help... the sum of all the positive integers and the sum of all the negative integers

Advertisements

C calculate sum of first and last element of array In this section, you will learn how to calculate sum of first and last element from the array of five...; sum = first + last;

Printing numbers up to N into a file I'd like to print the first N integers, that is, "1, 2, 3, 4, ..., N-1, N", say N equals 1000, or 10000 or whatever. I'd also like to have the result stored as a file instead of having

Calculate sum of even and odd numbers In this section, you will learn how to read the file that contains even and odd numbers and calculate their sum separately. To do this, first of all, we have found all the even and odd numbers from 1

Calculate the Sum of Three Numbers ... . In this section you will learn how to calculate the sum of three numbers by using three... how to calculate three integer number . First of all define class name "

ModuleNotFoundError: No module named 'average-n-numbers-calvine' Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks

ModuleNotFoundError: No module named 'average-n-numbers-calvine' Hi...: No module named 'average-n-numbers-calvine' How to remove the ModuleNotFoundError: No module named 'average-n-numbers-calvine' error? Thanks

Find consecutive numbers whose sum is equal to input number. In this section, you will learn how to display the consecutive natural numbers whose sum is equal... numbers which sum up to given number. Here is the code: import java.util.

To find first two maximum numbers in an array Java program to find first two maximum numbers in an array,using single loop without sorting array

Sum of two numbers using command line arguments in Java Hi, How...? The calculate the sum of two numbers using command line arguments in Java... = window.adsbygoogle || []).push({}); int sum = a+b; Check complete example code

ask user how many numbers to be inputted and determine the sum and highest number using an array in java ask user how many numbers to be inputted and determine the sum and highest number using an array in java

; sum=sum+s; if(i==n){ System.out.print(s...sum s=1+12+123+1234+............+123....n terms import...){ Scanner input=new Scanner(System.in); System.out.print("Enter value of n

; } } System.out.println("Sum of even numbers: "+sumevenNo); System.out.println("Sum of odd numbers: "+sumoddNo...Sum of integers A Java program that reads an integer value from

Find sum of all the elements of matrix using Java A matrix is a rectangular array of numbers. The numbers in the matrix are called its entries or its elements. A matrix with m rows and n columns is called m-by-n matrix or m × n matrix

To find first two maximum numbers in an array,using single loop without sorting array. Java program to find first two maximum numbers in an array,using single loop without sorting array

To find first two maximum numbers in an array,using single loop without sorting array. Java program to find first two maximum numbers in an array,using single loop without sorting array

Find sum of series This section illustrates you how to find the sum of n terms... function that takes the parameter 'n' up to which the sum of the series... { public static double sum(int n) { if (n == 1) return 1.0; return sum(n - 1

Sum of seris Program to find the sum of the following series using a function declaration. sum=x-(xxx)/3!+(xxxxx)/5!-(xxxxxxx)/7!+.....(xx....x)/n!. Where n and x are entered from the keyboard

:- In this example we are calculating sum and average of n numbers. The given code accepts..., we have calculated the sum of all numbers. And after dividing the sum...Calculate sum and Average in java How to calculate sum and average

number"); Scanner sin; int m,n,sum=0; sin=new Scanner(System.in); n=sin.nextInt(); for(int i=1;i<n;i++) { if(i%2!=0) sum=sum+i...A program to find the sum of odd number In this section you will learn how

calculate sum of individual digit How calculate product of first and last digit of indiviual numbers Like a=43278 4*8=32

to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents... a positive value for n, and should loop until they do so

. Following code calculates the sum of digits: while (n > 0) { int p = n % 10; sum = sum + p; n = n / 10; } Here is the code... = input.nextInt(); int t = n; while (n > 0) { int p = n % 10; sum

the sum of first 50 natural numbers. To draw a flowchart, we have used... Following code draws the Y and N on the decision box by the following...;ProcessBox(); process1.setName("SUM=0,NUM=0");

sum of fibonacci series Write a Java program to print Fibonacci series upto n and find their sum also. 0+1+1+2+3+5+8+13+21Ã¢â?¬Â¦Ã¢â?¬Â¦Ã¢â?¬Â¦Ã¢â?¬Â¦=sum Hi, Please see the thread Fibonacci program

Label("Sum of Two Numbers: "); label3.setBounds(20,80,130,20... numbers? import java.awt.Graphics; import javax.swing.... extends JApplet { int sum; int x; (adsbygoogle = window.adsbygoogle

prime numbers Write a java program to find the prime numbers between n and m

calculate sum of individual digit Hi How to calculate product of first snd last digit of indiviual digit Like Num=23456 S=2*6

it is equal to the sum of all its exact divisors (other than itself). 6 = 1... numbers. Use methods Hi Friend, Try the following code: public class PerfectNumber{ public static void main(String[]args){ int sum=0, x=0

of the square of numbers: " + sum); } } Output:ADS_TO_REPLACE_1 Enter 5 numbers: 1 2 3 4 5 Sum of the square...Find sum of the squares of Array elements In this section, you will learn how

numbers divisible by 5 and 6 Find the first ten numbers which are greater than Long.MAX_VALUE divisible by 5 and 6

ModuleNotFoundError: No module named 'sum' Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sum' How to remove the ModuleNotFoundError: No module named 'sum' error

ModuleNotFoundError: No module named 'sum' Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sum' How to remove the ModuleNotFoundError: No module named 'sum' error

automorphic numbers how to find automorphic number in java Hi Friend, Pleas visit the following link:ADS_TO_REPLACE_1 Automorphic numbers Thanks

sorting numbers How to sort the numbers in ascending order import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers

sum and average of grades how to print a program which is sum and average of Korean (90), English(85), Mathematics(78), Biology(65), Chemistry(83

numbers in Java it will return you int value as sum of two numbers. In real world... numbers. The value will be returned as sum of the number. So, we have to define... is: 30 So, in this program you learned how to sum(add) two numbers in Java

Ads