Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use a for loop to determine the sum and number of all odd integers from 1 to 100

ID: 3830981 • Letter: U

Question

Use a for loop to determine the sum and number of all odd integers from 1 to 100 which is divisible by 3. Use fprintf to display the answers as below. Ans: sum of all odd integers from 1 to 100 which is divisible by 3: 867 number of odd integers from 1 to 100 which is divisible by 3: 17 Use a while loop to create a vector containing the first 10 elements in the harmonic series, i.e., 1/1 1/2 1/3 1/4 1/5... 1/10 Ans: first 10 elements in the harmonic series Columns 1 through 8. 11/2 1/3 1/4 1/5 1/6 Columns 9 through 10 1/9 1/10 Use a for loop lo create a vector containing the first 10 elements in the harmonic series, i.e., 1/1 1/2 1/3 1/4 1/5... 1/10

Explanation / Answer

Answer:

7. Below is the maltab code:

my_sum = 0;
my_count = 0;
for ii = 1:100
if (mod(ii,2) != 0) && (mod(ii,3) == 0)
my_sum = my_sum + ii;
my_count = my_count + 1;
end
end
fprintf('sum of all odd integers from 1 to 100 which is divisible by 3: %d ', my_sum);
fprintf('number of odd integers from 1 to 100 which is divisible by 3: %d ', my_count);

OUTPUT:

sum of all odd integers from 1 to 100 which is divisible by 3: 867 number of odd integers from 1 to 100 which is divisible by 3: 17

8. Below is the matlab code:

format rat;
c_i = 1;
harmonic_vector = [];
while c_i <= 10
if (c_i == 1)
harmonic_vector = [harmonic_vector; 1];
else
harmonic_vector = [harmonic_vector; (1/c_i)];
end
c_i = c_i + 1;
end
g = sprintf('%d ', harmonic_vector);
fprintf('Answer: %s ', g);

9. Below is the matlab code:

format rat;
harmonic_vector = [];
for c_i = 1:10;
if (c_i == 1)
harmonic_vector = [harmonic_vector; 1];
else
harmonic_vector = [harmonic_vector; (1/c_i)];
end
end