Skip to content

Instantly share code, notes, and snippets.

@lonelycompiler
Created September 29, 2023 15:35
Show Gist options
  • Select an option

  • Save lonelycompiler/abe27b0698eba80cfcbcb5516feba533 to your computer and use it in GitHub Desktop.

Select an option

Save lonelycompiler/abe27b0698eba80cfcbcb5516feba533 to your computer and use it in GitHub Desktop.
Write a program that calculates and prints the sum of all the natural numbers divisible by 3 or 5, up to a given limit entered by the user.
#include <cstdio>
int sumOfNaturalDivisibleByThreeAndFive (int limit)
{
int sum = 0;
for (int i = 1; i <= limit; ++i)
{
if (i % 5 == 0 && i % 3 == 0) sum += i;
}
return sum;
}
int main ()
{
int limit = 100;
printf("sum of natural numbers divisible by three and five from 1 to %d is %d\n", limit, sumOfNaturalDivisibleByThreeAndFive(limit));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment