In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
Complete the following functions using C programming language:
To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in 3 different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value. Return the total sum at the end of each function
#include <stdio.h> int intQ1_for(){ int result = 0, i; for(i = 30;i<=1000;i++){ if(i%4==0){ result += i; } } return result; } int intQ1_while(){ int result = 0, i; i = 30; while(i<=1000){ if(i%4==0){ result += i; } i++; } return result; } int intQ1_do(){ int result = 0, i; i = 30; do{ if(i%4==0){ result += i; } i++; }while(i<=1000); return result; } int main() { printf("Using for loop: %d\n",intQ1_for()); printf("Using while loop: %d\n",intQ1_while()); printf("Using do loop: %d\n",intQ1_do()); return 0; }
Get Answers For Free
Most questions answered within 1 hours.