answersLogoWhite

0


Best Answer

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

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

class Add

{

public static void main (String args[])

{

int n = 15;

int sum = 0;

for(int i = n;i>=0;i--)

sum = sum + i;

System.out.println("Sum = "+ sum);

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The below method is written in the assumption that, the n numbers are sent as an ArrayList of Integer values.

public void computeSumAndAvg(ArrayList lst){

Iterator itr = lst.iterator();

int sum = 0;

float average = 0;

int count = 0;

while(itr.hasNext(){

Integer val = (Integer) itr.next();

sum = sum + val.intValue();

count++;

}

average = sum/count;

System.out.println("Sum is: " + sum) ;

System.out.println("Average is: " + average) ;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

It isn't clear what you mean by the "difference of N numbers". "Difference" is normally defined just for two numbers, so you would need to clarify what calculations are to be done in the case of more than two numbers.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

That would depend in a great deal on how those N numbers are presented by the program. However, a general pseudocode might be:

Do whatever housekeeping necessary to connect with the group of numbers (define a hardcoded array, open a file of numbers, format a user interface popup, etc.)

Initialize sum variable to 0

For some set of conditions (limits of the array, set and test an EOF flag, get an 'alldone' indication from the user, etc.) do the following

Get the next number by whatever means relevant

If end condition not reached, add obtained value to sum variable

When loop completed, display final result

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Something like this - assuming you already have an array called "myArray":

int sum = 0;

for(int element : myArray)

sum += element;


Note: The "for" is the "for each" variety; in other words, it is run once for each element of the array.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

public int sum(){

int sum = 0;

for (int i = 0; i <= n; i++)

sum+=i;

return sum;

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Something like this:
int n = 100;

int sum = 0;

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

System.out.println("The sum is: " + sum);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a Java program that computes the summation of 1 to n?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.