answersLogoWhite

0

Increment your counter two at a time. Example, for n = 100:

n = 100;

sum = 0;

for (int i = 1; i <= n; i+=2) sum += i;

Another option (less efficient) is to loop through ALL integers, and check whether the number is odd:

n = 100;

sum = 0;

for (int i = 1; i <= n; i++) {

if (i % 2 == 1)

sum += i;

}

Or:

if (min%2==0) min= min+1;

if (max%2==0) max= max-1;

sum = ((max-min)/2 + 1) * (min+max) / 2;

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


How write new line program in java?

\n


How do you write a java program for finding multiligual languages?

You can use Java's built-in functions to write a code that will find multilingual languages.


How java is 100 percent Internet based?

It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java


Write an expression that computes the sum of two variables in java?

int sum = a + b; PS: a and b are int variables that must have been already declared and initialized.


How do you write an array that computes the product of the two numbers in java?

You don't need an array for that. Just do the multiplication, for example: result = factor1 * factor2; Or: result = 5 * 8;


How do you write a java program to find the ad joint of a matrix?

bgfygfrhjyuyhh


How do you write algorithms of java programs?

Write a program that graphically demonstrates the shortest path algorithm


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.