Using R compute the average of all even numbers from 2100 Sh

Using R, compute the average of all even numbers from 2-100. Show commands and work. ( I can figure the answer out but not with correct commands in R)

Solution

sum = 0   # initialize sum to 0
count = 0
for (num in 2:100)   # for loop works from 2 to 100
{
if((num%%2) == 0)    # modulus operator %% is used to check if num is even
{
sum = sum + num    # if num is even add it to sum
count = count + 1
}
}
sum = sum/count # compute average
print(sum)

Output

Using R, compute the average of all even numbers from 2-100. Show commands and work. ( I can figure the answer out but not with correct commands in R)Solutionsu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site