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;
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) ;
}
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
write a program draw circle and ellipse by using oval methods in java
Write a program that graphically demonstrates the shortest path algorithm
you can use the condition statement like for(i=0:i<=10;i++)
Yes, that's what JNI is good for.
Since the Java program is basically a text file, you can write it in any text editor (although using a Java IDE, or an IDE with support for Java, does give you certain advantages). Thus, you can use programs such as NotePad or NotePad++ to write the Java program. Using word processors such as MS-Word is problematic, because these insert additional codes. You would have to make sure you save the resulting file in the text format - give the "Save As" command, and choose a text format from the list.
yes ,i can add the website link in java program when we write.
Exactly what do you mean by 'C program in Java'
write a java program to display "Welcome Java" and list its execution steps.
\n
You can use Java's built-in functions to write a code that will find multilingual languages.
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 program draw circle and ellipse by using oval methods in java
int sum = a + b; PS: a and b are int variables that must have been already declared and initialized.
You don't need an array for that. Just do the multiplication, for example: result = factor1 * factor2; Or: result = 5 * 8;
bgfygfrhjyuyhh
Write a program that graphically demonstrates the shortest path algorithm
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.