java for loop sum and average of range -
i have calculated sum of loop range, not know how count , average solution. must done without ifs. loop:
//defining variables // constants & variables here int i; int numstart; int numend; double sum = 0; double average=0; double loopcount=0; system.out.print("enter start number: "); // keep print line open numstart = console.nextint(); system.out.print("enter end number: "); numend = console.nextint(); //enter thevalue (i = numstart; <= numend; i++ ) { sum = sum + i; loopcount = numend - numstart; average = sum+1 / i; } system.out.println(); system.out.println( "sum is: " + sum); system.out.println(); system.out.println( "average is: " + average); system.out.println();
(i = numstart; <= numend; i++ ) { sum = sum + i; loopcount++; } average = sum / loopcount;
Comments
Post a Comment