Sum of first n numbers

Ads
 

Sum of first n numbers

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);
 }
}

Ads









Related Tutorials/Questions & Answers:
Sum of first n numbers
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 and negative numbers
]; } } System.out.println("Sum of positive numbers: "+sum2); System.out.println("Sum of negative numbers: "+sum1...Sum of positive and negative numbers  Hi if someone could help
Advertisements
ModuleNotFoundError: No module named 'average-n-numbers-calvine'
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'
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  
To find first two maximum numbers in an array
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
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... sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks
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  ask user how many numbers to be inputted and determine the sum and highest number using an array in java
sum
; 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
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
Sum of integers
; } } 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
sum
sum  3,8,15,24,............n terms
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
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
sum
sum  s=135.............17 (mul of 1 to 17 odd numbers
sum
sum  s=(1+2)/(1x2)+(1+2+3)/(1x2x3+)+.......+(1+2+3+......n terms)/(1x2x3x..........n terms
sum
sum  s=1/(1+2)+1/(1+2+3)+1/(1+2+3+4)+.....+1/(1+2+3+....n terms
sum
sum  s=1/(1+2)+1/(1+2+3)+1/(1+2+3+4)+.....+1/(1+2+3+....n terms
calculate sum of individual digit
calculate sum of individual digit   How calculate product of first and last digit of indiviual numbers Like a=43278 4*8=32
sum of the series
sum of the series  Program to sum the series 1+1/1!+1/2!+1/3!+....n
Calculate sum and Average in java
. Description:- In this example we are calculating sum and average of n numbers... the for loop, we have calculated the sum of all numbers. And after dividing the sum by the total number of numbers, we have determined the average of numbers
A program to find the sum of odd number
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
Prime Numbers
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
Applet for add two numbers
); add(text2); label3 = new Label("Sum of Two Numbers... numbers?   import java.awt.Graphics; import javax.swing.*; public class AddApplet extends JApplet { int sum; int x; int y; public void init
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
sum of fibonacci series
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. Thanks
calculate sum of individual digit
calculate sum of individual digit  Hi How to calculate product of first snd last digit of indiviual digit Like Num=23456 S=2*6
sum
sum
sum
How to add two numbers in Java?
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... how to sum(add) two numbers in Java. Check more tutorials at: Java
sorting numbers
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

Ads