Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Accenture Coding Question 1

Coding Question 1

The function def differenceofSum(n. m) accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n.

Assumption:

  • n>0 and m>0
  • Sum lies between integral range

Example

Input
n:4

m:20
Output
90

Explanation

  • Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60
  • Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 + 18 + 19 = 150
  • Difference 150 – 60 = 90

Sample Input
n:3

m:10
Sample Output
19

n = int(input())
m = int(input())
sum1 = 0
sum2 = 0
for i in range(1,m+1):
    if i % n == 0:
        sum1+=i
    else:
        sum2+=i
print(abs(sum2-sum1))
Input:
3
10
Output:
19
#includ<stdio.h>;
int differenceofSum(int n, int m)
{
    int i, sum1 = 0, sum2 = 0;
    for(i=1; i<=m; i++)
    {
        if(i%n==0)
        {
            sum1 = sum1 + i;
        }
        else
        {
            sum2 = sum2 + i;
        }   
    }
    return sum2 - sum1;
}

int main()
{
    int n, m;
    int result;
    scanf("%d",&n);
    scanf("%d",&m);
    result = differenceofSum(n, m);
    printf("%d",result);
    return 0;
}
Input:
3
10
Output:
19
#includ<bits/stdc++.h>
using namespace std;

int differenceofSum(int n, int m)
{
int i, sum1 = 0, sum2 = 0;
for(i=1; i<=m; i++)
{
if(i%n==0)
{
sum1 = sum1 + i;
}
else
{
sum2 = sum2 + i;
}
}
return sum2 - sum1;
}

int main()
{
int n, m;
int result;
cin>>n>>m;
result = differenceofSum(n, m);
cout<<result;
return 0;
}
import java.util.*;
class Solution
{
public static int differenceOfSum (int m, int n)
{
int sum1 = 0, sum2 = 0;
for (int i = 1; i <= m; i++)
{
if (i % n == 0)
sum1 = sum1 + i;
else
sum2 = sum2 + i;
}
return Math.abs (sum1 - sum2);
}

public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int m = sc.nextInt ();
System.out.println (differenceOfSum (m, n));
}
}