Multiples Power
PROBLEM :
Calculate the sum of all the multiples of 3 or 7 below the natural number N.
Input:
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output:
For each test case, print an integer that denotes the sum of all the multiples of 3 or 7 below N.
Constraints:
1 = T = 50
1 = N = 100000
Example:
Input:
2
1
20
Output:
25
84
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long calculate_sum(long long ) ;
int main()
{
long long sum,no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
sum=calculate_sum(no) ;
cout<<sum<<endl ;
}
return 0;
}
long long calculate_sum(long long no)
{
long long i,sum ;
sum=0 ;
for(i=1;i<no;i++)
{
if(i%3==0||i%7==0)
sum=sum+i ;
}
return sum ;
}
---------------------------------------------------------------------------------
Calculate the sum of all the multiples of 3 or 7 below the natural number N.
Input:
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output:
For each test case, print an integer that denotes the sum of all the multiples of 3 or 7 below N.
Constraints:
1 = T = 50
1 = N = 100000
Example:
Input:
2
1
20
Output:
25
84
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long calculate_sum(long long ) ;
int main()
{
long long sum,no ;
int t ;
cin>>t ;
while(t--)
{
cin>>no ;
sum=calculate_sum(no) ;
cout<<sum<<endl ;
}
return 0;
}
long long calculate_sum(long long no)
{
long long i,sum ;
sum=0 ;
for(i=1;i<no;i++)
{
if(i%3==0||i%7==0)
sum=sum+i ;
}
return sum ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment