41.4k views
4 votes
Write a c program that uses iteration to find the sum of all multiples of 3

and all multiples of 4 between 3 and 150 inclusive. Print the sum.

User Staromeste
by
6.8k points

1 Answer

4 votes

#include<stdio.h>

int main()

{

int sum_3=0, sum_4=0; //Variable to store the sum of multiples of 3 and 4

for(int i=3; i<=150; i++) //Iterating from 3 to 150

{

if(i%3==0) //Checking if i is a multiple of 3

sum_3 += i; //Adding i to the sum of multiples of 3

if(i%4==0) //Checking if i is a multiple of 4

sum_4 += i; //Adding i to the sum of multiples of 4

}

printf("Sum of multiples of 3: %d\\", sum_3); //Printing the sum of multiples of 3

printf("Sum of multiples of 4: %d\\", sum_4); //Printing the sum of multiples of 4

return 0;

}

User Rasheeda
by
7.2k points