Home Answers Viewqa Java-Beginners Sum of first n numbers

 
 


Smruti Ranjan
Sum of first n numbers
1 Answer(s)      a year and 4 months ago
Posted in : Java Beginners

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 Pages:
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
Calculate sum of even and odd numbers in Java
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
Printing numbers up to N into a file
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 the Sum of three Numbers
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 "
C calculate sum of first and last element of array
five numbers:1 2 3 4 5 Sum of first and last...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 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=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  3,8,15,24,............n terms
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
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
Find consecutive numbers whose sum is equal to input number.
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.
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
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
sum of the series
sum of the series  Program to sum the series 1+1/1!+1/2!+1/3!+....n
Find sum of all the elements of matrix using Java
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
Sum of seris
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
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
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
Draw a Flowchart
the sum of first 50 natural numbers. To draw a flowchart, we have used... and N on the decision box by the following code: g.drawText("...;process1.setName("SUM=0,NUM=0");   process1.setBounds(new 
adding two numbers - Java Beginners
InputStreamReader(System.in)); System.out.println("Enter first no.:"); x[1...]=Integer.parseInt(in.readLine()); int s=x[1]+y[1]; System.out.println("Sum of two...("Enter first no.:"); x[0]=Integer.parseInt(in.readLine()); System.out.println
Find sum of series
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... sum(int n) { if (n == 1) return 1.0; return sum(n - 1) + 1.0 / n
Numbers
Java NotesNumbers Two kinds of numbers. There are basically two kinds of numbers in Java and most other programming languages: binary integers (most commonly using the type int) and binary floating-point numbers (most commonly using
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
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
generating random numbers - Java Beginners
of the known prices (except for the first 5, which don't have enough predecessors). We...= -1.0; double ran[]=new double[5]; double error; double sum=0,sum1=0
finding the eigenvalues and eigenvector of n*n matrix in java
* @return */ public static double sumDiagElSq(double A[][], int n) { double sum = 0; for (int i = 0; i < n; i++) { sum = A[i][i] * A[i][i] + sum...finding the eigenvalues and eigenvector of n*n matrix in java  Here
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
looping programm of sum
looping programm of sum  s=1+2/12+1+2+3/12*3+1+2+3+..... n
RMI n JSP - RMI
RMI n JSP  Hi Friends, First of all let me appreciate ur promptness in delivering answers. Is it possible to make communication between RMI and JSP page ? Thanks
RMI n JSP - RMI
RMI n JSP  Hi Friends, First of all let me appreciate ur promptness in delivering answers. Is it possible to make communication between RMI and JSP page ? Thanks
Java Sum of Digits
. 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
sum
sum
sum
prime numbers - Java Beginners
prime numbers  Write a java program to find the prime numbers between n and m
numbers divisible by 5 and 6
numbers divisible by 5 and 6  Find the first ten numbers which are greater than Long.MAX_VALUE divisible by 5 and 6
JavaScript determine add, product of two numbers
("Sum of two numbers is: "+sum); } function divide...JavaScript determine add, product of two numbers In this section, you... numbers. To find this, we have created two textboxes and four button
Perfect Numbers - Java Beginners
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
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
Find sum of the squares of Array elements
of the square of numbers: " + sum); } } Output: Enter 5 numbers: 1 2 3 4 5 Sum of the square of numbers: 55...Find sum of the squares of Array elements In this section, you will learn how
Find Sum of Diagonals of Matrix
Find Sum of Diagonals of Matrix You all know that a matrix is a rectangular array of numbers and these numbers in the matrix are called its entries or its elements. Here we are going to find the sum of Primary diagonal (From left top
Prime Numbers - IDE Questions
Prime Numbers  Create a program that calculates the prime numbers... numbers in rows of 10. Hint: Use a counter to count how many values are printed... = 0; System.out.println("Prime Numbers are:" ); for (int i = 1; i <
permutstion of numbers in java
permutstion of numbers in java  Is it possible to enter the number...*; public class major1 { int n=0; char c[]; public major1...="); Scanner input=new Scanner(System.in); n=input.nextInt(); for(int j=1
Writing and testing method of addition of two numbers
Writing and testing method of addition of two numbers  ... a method named sum() which takes two int parameters and return addition of these two numbers. So here we have to check whether this method is functioning well in all
Sum of diagonal of Matrix in C
; }         printf("Sum of First... of First diagonal=16 Sum of Second diagonal=19...C Sum of diagonal of Matrix In this section, you will learn how to determine
how to print all possible combination that sum to zero??
how to print all possible combination that sum to zero??  Ã? Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 ââ?¬Â¦ N Ã? Insert either a ââ?¬Ë?+' (for addition) or a ââ?¬Ë?-ââ?¬Ë

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.