Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol3.c File Reference

Problem 1 solution. More...

#include <stdio.h>
Include dependency graph for sol3.c:

Functions

int main ()
 Main function.
 

Detailed Description

Problem 1 solution.

This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.

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.

Function Documentation

◆ main()

int main ( void  )

Main function.

15{
16 int n = 0;
17 int sum = 0;
18 int num = 0;
19 scanf("%d", &n);
20
21 while (1)
22 {
23 num += 3;
24 if (num >= n)
25 break;
26 sum += num;
27 num += 2;
28 if (num >= n)
29 break;
30 sum += num;
31 num += 1;
32 if (num >= n)
33 break;
34 sum += num;
35 num += 3;
36 if (num >= n)
37 break;
38 sum += num;
39 num += 1;
40 if (num >= n)
41 break;
42 sum += num;
43 num += 2;
44 if (num >= n)
45 break;
46 sum += num;
47 num += 3;
48 if (num >= n)
49 break;
50 sum += num;
51 }
52
53 printf("%d\n", sum);
54 return 0;
55}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy