Accenture Questions
Accenture Questions
Accenture Questions
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:
Example
Input
n:4
m:20
Output
90
Explanation
Sample Input
n:3
m:10
Sample 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;
}
coding Question 2
You are required to implement the following Function def LargeSmallSum(arr).
The function accepts an integers arr of size ’length’ as its arguments you are required to return the sum of second largest largest element from the even positions and second
smallest from the odd position of given ‘arr’.
Assumption:
All array elements are unique
Treat the 0th position a seven
NOTE
Example:-
Input
arr:3 2 1 7 5 4
Output
Explanation
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
8
#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
Coding Question 3
Implement the following Function
The function def ProductSmallestPair(sum, arr) accepts an integers sum and an integer array arr of size n.
Implement the function to find the pair, (arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k] are the least
two elements of array (arr[j] + arr[k] <= sum) and return the product of element of this pair
NOTE
Return -1 if array is empty or if n<2
Return 0, if no such pairs found
All computed values lie within integer range
Example
Input
sum:9
Arr:5 2 4 3 9 7 1
Output
2
Explanation
Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2
Sample Input
sum:4
Arr:9 8 3 -7 3 9
Sample Output
-21
#include<stdio.h>;
int main()
{
int n, sum, result, i;
scanf("%d",&sum);
scanf("%d",&n);
int array[n];
for(i=0; i<n; i++)
{
scanf("%d",&array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d",result);
return 0;
}
N-base notation is a system for writing numbers which uses only n different symbols, This symbols
are the first n symbols from the given notation list(Including the symbol for o) Decimal to n base
notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:A,11:B and so on upto 35:Z)
Steps:
Divide the decimal number by n,Treat the division as the integer division
Write the the remainder (in n-base notation)
Divide the quotient again by n, Treat the division as integer division
Repeat step 2 and 3 until the quotient is 0
The n-base value is the sequence of the remainders from last to first
Assumption:
1 < n < = 36
Example
Input
n: 12
num: 718
Output
4BA
Explanation
718 12 59 10(A)
59 12 4 11(B)
4 12 0 4(4)
Sample Input
n: 21
num: 5678
Sample Output
CI8
Coding Question 5
Implement the following functions. achar*MoveHyphen(char str[],int n);
The function accepts a string “str” of length ‘n’, that contains alphabets and hyphens (-).
Implement the function to move all hyphens(.) in the string to the front of the given string.
NOTE:-
Return null if str is null.
Example :-
Input:
str.Move-Hyphens-to-Front
Output:
-MoveHyphenstoFront
Explanation:-
The string “Move-Hyphens -to-front” has 3 hyphens (.), which are moved to the front of the
string, this output is “— MoveHyphen”
Sample Input
Str: String-Compare
Sample Output-
-StringCompare
Coding Question 6
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two numbers from right-to-left one
digit at a time
You are required to implement the following function, Int NumberOfCarries(int num1 , int num2);
The functions accepts two numbers ‘num1’ and ‘num2’ as its arguments. You are required to calculate and return
the total number of carries generated while adding digits of two numbers ‘num1’ and ‘ num2’.
Example:
Input
Num 1: 451
Num 2: 349
Output
2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10, again 1 is
carried. Hence 2 is returned.
Sample Input
Num 1: 23
Num 2: 563
Sample Output
0
#include<stdio.h>
int numberOfCarries(int num1 , int num2)
{ int carry = 0, sum, p, q, count = 0;
while((num1!=0)&&(num2!=0))
{ p = num1 % 10;
q = num2 % 10;
sum = carry + p + q;
if(sum>9)
{ carry = 1;
count++;
}
else
{
carry = 0;
}
num1 = num1/10;
num2 = num2/10;
}
return count;
}
int main()
{
int x, y, a;
scanf("%d",&x);
scanf("%d",&y);
a = numberOfCarries(x, y);
printf("%d",a);
return 0;
}
Coding Question 7
You are given a function, Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);
The function accepts a string ‘ str’ of length n and two characters ‘ch1’ and ‘ch2’ as its
arguments . Implement the function to modify and return the string ‘ str’ in such a way that all
occurrences of ‘ch1’ in original string are replaced by ‘ch2’ and all occurrences of ‘ch2’ in
original string are replaced by ‘ch1’.
Assumption: String Contains only lower-case alphabetical letters.
Note:
Return null if string is null.
If both characters are not present in string or both of them are same , then return the string
unchanged.
Example:
Input:
Str: apples
ch1:a
ch2:p
Output:
Paales
Explanation:
‘A’ in original string is replaced with ‘p’ and ‘p’ in original string is replaced with ‘a’, thus output
is paales.
#include <stdio.h>
#include <string.h>
void *ReplaceCharacter(char str[], int n, char ch1, char ch2)
{ int i;
for(i=0; i<n ; i++)
{ if(str[i]==ch1)
{ str[i]=ch2;
}
else if(str[i]==ch2)
{ str[i]=ch1;
}
}
printf("%s",str);
}
int main()
{
char a[100];
char b, c;
int len;
scanf("%s",a);
scanf("%s",&b);
scanf("%s",&c);
len = strlen(a);
ReplaceCharacter(a, len, b, c);
return 0;
}
Output:
apples
a
p
paales
Coding Question 8
You are required to implement the following function, Int OperationChoices(int c, int n, int a , int b )
The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its arguments. Implement the function to
return.
( a+ b ) , if c=1
( a - b ) , if c=2
( a * b ) , if c=3
(a / b) , if c =4
Assumption :
All operations will result in integer output.
Example:
Input
c :1
a:12
b:16
Output:
Since ‘c’=1 , (12+16) is performed which is equal to 28 , hence 28 is returned.
Sample Input
c : 2
a : 16
b : 20
Sample Output
-4
#include<stdio.h>
int operationChoices(int c, int a , int b)
{
if(c==1)
{
return a + b;
}
else if(c==2)
{
return a - b;
}
else if(c==3)
{
return a * b;
}
else if(c==4)
{
return a / b;
}
}
int main()
{
int x, y, z;
int result;
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
result = operationChoices(x, y, z);
printf("%d",result);
}
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
/* Programming Question */
#include <stdio.h>
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;
}