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);
}
}
Ads
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
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 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 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 in JTable
sum in JTable how to calculate
sum from JTable's one field like total
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
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
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
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
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