Using a For loop you must find the average of all the even numbers and all the odd integers between 0 and 100?
-
Using a For loop you must find the average of all the even numbers and all the odd integers between 0 and 100 (including 100). You will need variables to keep track of the current sum of the odd numbers, current sum of the even numbers, number of odd numbers and number of even numbers so that you can calculate and report to the user the average of the odd numbers and average of the even numbers. You must use Modulus to decide whether a number is even or odd.
-
Answer:
Using a for loop.. repeatedly ask other people to do your homework. Using an if statement.. determine if having somebody else do your homework for you helps you learn. Write the code, and if you have specific problems ask about them. Don't be so lazy.
Tony at Yahoo! Answers Visit the source
Other answers
something like this (this is a java example but you'll get the jist of it). A for lus that goes from 0 to 100, and every count it'll check if the teller is even or not (% is modulus sign, to get the rest value of a devide (2 / 2 = 0 for example). It'll increase the tellers by one and increase the sum with the value of i, then devide the sum by count and you have your avarage. double oddSum = 0, evenSum = 0, oddCount = 0, evenCount = 0; for(int i; i < 101; i++){ if(i % 2 == 0){ evenSum += i; evenCount++; } else{ oddSum += i; oddCount ++; } double avgEven = evenSum / evenCount; double avgOdd = oddSum / oddCount; }
// number of odds int nbOdd = 0; // number of evens int nbEven = 0; // running total of odds int totalOdd = 0; // running total of evens int totalEven = 0; // start loop for(i=1;i<101;i++) { // test using modulus if (i % 2 == 0) { // even nbEven++; totalEven = totalEven + i; }else { // odd nbOdd++; totalOdd = totalOdd + i; } } // end loop // compute averages double avgOdd = totalOdd / nbOdd; double avgEven = totalEven / nbEven;
k=1; l=1; a=0; b=0; for(i=0;i<=100;i++) { if(i%2==0) { a=a+i; even_av=a/k k++; } else { b=b+i; odd_av=b/l l++; } }
Related Q & A:
- How To Connect Two Different Network Segments Using A Switch And A Router?Best solution by Super User
- how to process a simple loop in WWW::Mechanize to be more efficient?Best solution by stackoverflow.com
- How to find the derivative without using a symbolic function in Matlab?Best solution by Stack Overflow
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- What is the difference between a for loop, a do while loop and a do loop?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.