Sum of integers

Sum of integers

A Java program that reads an integer value from the user and displays i) the sum of all even integers between 1 and the input value, both inclusive. ii) The sum of all odd integers between 1 and the input value both inclusive

Example: User enters 7. Sum of even integers = 2+4+6 = 12 Sum of odd integers = 1+3+5+7=16

View Answers

February 9, 2012 at 10:42 AM

import java.util.*;

class AddEvenAndOdd {
        public static void main(String[] args) throws Exception {
                int sumevenNo = 0;
                int sumoddNo = 0;
                Scanner input=new Scanner(System.in);
                System.out.print("Enter value of n: ");
                int n=input.nextInt();
                for (int i = 1; i <= n; i++) {
                        if (i % 2 == 0) {
                                sumevenNo += i;
                        } else {
                                sumoddNo += i;
                        }
                }
                System.out.println("Sum of even numbers: "+sumevenNo);
                System.out.println("Sum of odd numbers: "+sumoddNo);

        }
}









Related Tutorials/Questions & Answers:
Sum of integers
the user and displays i) the sum of all even integers between 1 and the input value, both inclusive. ii) The sum of all odd integers between 1 and the input value both inclusive Example: User enters 7. Sum of even integers = 2+4+6 = 12 Sum
how to find the sum of integers in a string of sentence in java
how to find the sum of integers in a string of sentence in java  how to find the sum of integers in a string of sentence in java for e.g:--> abc d 2 3 21 the output should be 2+3+21=26 (adsbygoogle
Advertisements
integers
integers  an application that finds the smallest of several integers. assume the first value read specifies the number of values to input from the user. please help
INTEGERS
application that will input the kms driven and litre of fuel used (both as integers
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
integers
Integers separated by a ", " in a string
Integers separated by a ", " in a string  How do I get integers separated by a ', ' in C++ in a string? Ex. string=1, 2, 3; //something to find the integers and put them in array a cout< output: 123   Hi Friend
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
sum of the series
sum of the series  Program to sum the series 1+1/1!+1/2!+1/3!+....n
Sum of positive and negative numbers
the sum of all the positive integers and the sum of all the negative integers.  ...Sum of positive and negative numbers  Hi if someone could help...]; } } System.out.println("Sum of positive numbers: "+sum2); System.out.println
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
ModuleNotFoundError: No module named 'artisinal_integers'
ModuleNotFoundError: No module named 'artisinal_integers'  Hi, My... named 'artisinal_integers' How to remove the ModuleNotFoundError: No module named 'artisinal_integers' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'artisinal_integers'
ModuleNotFoundError: No module named 'artisinal_integers'  Hi, My... named 'artisinal_integers' How to remove the ModuleNotFoundError: No module named 'artisinal_integers' error? Thanks   Hi, In your
write a java program to find the summation of all the integers entered on command line
write a java program to find the summation of all the integers entered... the integers entered on command line? How to write Java program for summation of all the integers entered on command line? Thanks   Hi, Example program
sum
sum
sum
sum
sum
sum
sum
sum
ModuleNotFoundError: No module named 'sum'
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'
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
Maven Repository/Dependency: com.almworks.integers | integers
Maven Repository/Dependency of Group ID com.almworks.integers and Artifact ID integers. Latest version of com.almworks.integers:integers dependencies. # Version Release Date 1
sum and average of grades
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
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...=input.nextInt(); long sum=0; for(int i=1;i<=n;i++){ sum+=i
java matching array of integers - Java Beginners
token is assigned a vlaue in integers and stored in an array. the consecutive array of integers occurring similarly have to be found out. and depending upon... for finding similar consecutive array of integers
Calculate sum and Average in java
Calculate sum and Average in java  How to calculate sum and average...(); int num = Integer.parseInt(data); int sum = 0; float avg = 0.0f; for (int i = 1; i <= num; i++) { sum = sum + i
how to find [count the number of integers whose value is less than the average value of the integers]
how to find [count the number of integers whose value is less than the average value of the integers]  Construct an algorithm that will prompt... amount to the screen integers from an operator at a terminal, and count the number
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 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
Sum - JSP-Servlet
the total sum of items from database using Java servlet or Jsp and html form. It should display the affected rows and the total sum of all in a seperate column..., Sum Into Database Salary
Using sum() in hibernate criteria.
Using sum() in hibernate criteria.  How to calculate sum() in hibernate criteria by using projection?   You can calculate sum of any...(employee); } } } Output: Hibernate: select sum
Printing the integers before and after - JSP-Servlet
Printing the integers before and after  Hi, I'm trying to print out the integers before and after generated random number, and also stating it as either even or odd, but it does not seem to work. Pls advise on my code below
sum database column
sum database column  please i have two columns-col1 and col2, col1... 3 Soap 4 how will i query the database to sum the Items... for you. select item, sum(quqantity) from `order` group by item
How do you sum a dictionary
How do you sum a dictionary  Hi, I want to sum the values of two dictionary based on keys. How do you sum a dictionary
Hibernate criteria sum.
Hibernate criteria  sum. Here, we will introduce you to about the sum() function . It returns the sum of total value of column. Syntax : Criteria...: select sum(this_.fee) as y0_ from studentinfo this_ Total of fee columm
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
Program to read 2 integers using oops concepts
Program to read 2 integers using oops concepts  Write a program to read two integers with the following significance. ? The first integer value represents a time of day on a 24 hour clock, so that 1245 represents quarter
Sum database colum in java
Sum database colum in java  please i have two columns-col1 and col2... the database to sum the Items and display output so that it will be like... question what you have asked. select item, sum(quqantity) from `order` group
Merge and sum of two dictionaries
Merge and sum of two dictionaries  Hi, I want to merge the two dictionaries and at the same I want to sum the vales of the same key. How I can achieve this? Thanks   Hi, this is very classical example of summing
Sum - JSP-Servlet
Sum  Hi Guys, thank you all for support for us all. Please I want to sum up from a particular row from the database, get all the rows affected... = con.createStatement(); ResultSet res = st.executeQuery("SELECT SUM(amount) FROM
ModuleNotFoundError: No module named 'djl-sum'
ModuleNotFoundError: No module named 'djl-sum'  Hi, My Python...-sum' How to remove the ModuleNotFoundError: No module named 'djl-sum... to install padas library. You can install djl-sum python with following command
ModuleNotFoundError: No module named 'hn_sum'
ModuleNotFoundError: No module named 'hn_sum'  Hi, My Python..._sum' How to remove the ModuleNotFoundError: No module named 'hn_sum'... to install padas library. You can install hn_sum python with following command
ModuleNotFoundError: No module named 'simple-sum'
ModuleNotFoundError: No module named 'simple-sum'  Hi, My Python... 'simple-sum' How to remove the ModuleNotFoundError: No module named 'simple-sum' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'sum-ha'
ModuleNotFoundError: No module named 'sum-ha'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sum-ha' How to remove the ModuleNotFoundError: No module named 'sum-ha'
ModuleNotFoundError: No module named 'sum-ha'
ModuleNotFoundError: No module named 'sum-ha'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'sum-ha' How to remove the ModuleNotFoundError: No module named 'sum-ha'
ModuleNotFoundError: No module named 'sum-walker'
ModuleNotFoundError: No module named 'sum-walker'  Hi, My Python... 'sum-walker' How to remove the ModuleNotFoundError: No module named 'sum... have to install padas library. You can install sum-walker python

Ads