Created
September 29, 2023 15:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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