HackerRank - Project Euler #1: Multiples of 3 and 5

HackerRank - Project Euler #1: Multiples of 3 and 5
Photo by Star of the Sea / Unsplash

Introduction

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.

Input and Output Format

# Input:
2
10
100
# Output:
23
2318

Explanation

For N = 10, if we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Similarly for N = 100, we get 2318.

Process

Using a for loop to loop through all the numbers below N, and check if the number is a multiple of 3 or 5, if it is, add it to the sum, and print out the sum at the end of the loop.

Code

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void sumMultiple(int n);

int main(){
    int t;
    scanf("%d",&t);
    for(int a0 = 0; a0 < t; a0++){
        int n;
        scanf("%d",&n);
        sumMultiple(n);
        printf("\n");
    }
    return 0;
}

void sumMultiple(int n){
    int sum = 0;
    for(int i = 1; i< n; i++){
        if(i%3 == 0 || i% 5 == 0){
            sum += i;
        }

    }
    printf("%d", sum);
}

Afterthoughts

This solution was a bit of a brute force solution, but it worked. It passed 4/6 test cases and needed to be optimized. I'm sure there's a more efficient way to do this, but I'll take a second look at this later.

References