Accenture Coding Question 10
Coding Question 10
You are required to implement the following function, Int Calculate(int m, int n);
The function accepts 2 positive integer ‘m’ and ‘n’ as its arguments.You are required to calculate the sum of numbers divisible both by 3 and 5, between ‘m’ and ‘n’ both inclusive and return the same.
Note:
0 < m <= n
Example
Input:
m : 12
n : 50
Output:
90
Explanation:
The numbers divisible by both 3 and 5, between 12 and 50 both inclusive are {15, 30, 45} and their sum is 90.
Sample Input
m : 100
n : 160
Sample Output
405
C
C++
Java
Python
C
Run
* Programming Question *
#include
int Calculate(int, int);
int main()
{
int m, n, result;
// Getting Input
printf("Enter the value of m : ");
scanf("%d",&m);
printf("Enter the value of n : ");
scanf("%d",&n);
result = Calculate(n,m);
// Getting Output
printf("%d",result);
return 0;
}
/* Write your code below . . . */
int Calculate(int n, int m)
{
// Write your code here
int i, sum = 0;
for(i=m;i<=n;i++)
{
if((i%3==0)&&(i%5==0))
{
sum = sum + i;
}
}
return sum;
}
C++
Run
#include
using namespace std;
int Calculate(int, int);
int main()
{
int m, n, result;
// Getting Input
cout<<"Enter the value of m :";
cin>>m;
cout<<"Enter the value of n :";
cin>>n;
result = Calculate(n,m);
// Getting Output
cout<<result;
return 0;
}
/* Write your code below . . . */
int Calculate(int n, int m)
{
// Write your code here
int i, sum = 0;
for(i=m;i<=n;i++)
{
if((i%3==0)&&(i%5==0))
{
sum = sum + i;
}
}
return sum;
}
Java
Run
import java.util.Scanner;
public class Main {
int sum=0;
int Calculate(int m, int n){
for (int i = m; i < n; i++)
if((i%3==0) && (i%5==0))
sum = sum+i;
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of m and n");
int m = sc.nextInt();
int n = sc.nextInt();
Main q = new Main();
int result = q.Calculate(m,n);
System.out.println(result);
}
}
Python
Run
m = int(input("M:"))
n = int(input("N:"))
def calculate(m, n):
sum = 0
for i in range(m,n+1,1):
if i%3 == 0 and i%5 == 0:
sum = sum + i
print(sum)
calculate(m,n)Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
