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

Problem 1 solution More...

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

Functions

int main ()
 Main function.
 

Detailed Description

Problem 1 solution

An Efficient code to print all the sum of all numbers that are multiples of 3 & 5 below N.

Function Documentation

◆ main()

int main ( void  )

Main function.

13{
14 int t;
15 printf("Enter number of times you want to try");
16 scanf("%d", &t);
17 while (t--) // while t > 0, decrement 't' before every iteration
18 {
19 unsigned long long N, p = 0, sum = 0;
20 printf("Enter the value of N ");
21
22 scanf("%lld", &N); // Take input of N from user
23 p = (N - 1) / 3;
24 sum = ((3 * p * (p + 1)) / 2);
25
26 p = (N - 1) / 5;
27 sum = sum + ((5 * p * (p + 1)) / 2);
28
29 p = (N - 1) / 15;
30 sum = sum - ((15 * p * (p + 1)) / 2);
31 printf("%lld\n", sum); // print the sum of all numbers that are
32 // multiples of 3 & 5 below N
33 }
34 return 0;
35}