Accenture Menu9>
PREPINSTA PRIME
Accenture Coding Questions and Solution 2025
Accenture Coding Questions With Answers 2025
Accenture Coding Test Questions with Solution 2025 are discussed below. A lot of Accenture Coding Question will be of same pattern as mentioned on our Dashboard so it is suggested that you prepare from PrepInsta.
PrepInsta offer resources specifically tailored to Accenture’s coding test patterns. By utilizing these resources, you can gain valuable insights into the types of questions that may be asked during the test.
Accenture Coding Questions
In Accenture there will be 2 coding questions that you have to solve in 45 minutes. In the Accenture Coding Round ,you can write coding using in these preferred language:-
- C
- C++
- Java
- Python
- Dot Net
The difficulty level of the questions are high. You have to practice a lot to get good score in the accenture coding Questions.
Accenture Coding Questions marking Scheme
There will be total of 2 Questions asked in the Accenture Coding Round. For successfully clearing the Coding Round, Students need to have 1 Complete Output and 1 Partial Output.
Accenture Coding Round | No of Questions | Min. Selection Criteria |
---|---|---|
Coding Questions | 2 | One Complete Output One Partial Output |
Rules for Accenture Coding Round Questions Section:
- There are two question for 45 minutes.
- We must start our code from the scratch.
- The coding platform is divided into two, one for writing the code and other for output. We should write the whole program.
- The errors are clearly mentioned.
- One Partial and One Complete Output is required for clearing the round.
Accenture Coding Question
Total number of Questions | 2 Question |
Total Time Duration | 45 minutes |
Type of Test | Non- Adaptive |
Negative Marking | No |
Accenture Coding Test Questions and Answers
Question 1: Rat Count House
(Asked in Accenture OnCampus 10 Aug 2022, Slot 1)
Problem Description :
The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i
Note:
- Return -1 if the array is null
- Return 0 if the total amount of food from all houses is not sufficient for all the rats.
- Computed values lie within the integer range.
Example:
Input:
- r: 7
- unit: 2
- n: 8
- arr: 2 8 3 5 7 4 1 2
Output:
4
Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4 houses is sufficient for all the rats. Thus, output is 4.
#include<bits/stdc++.h> using namespace std; int calculate (int r, int unit, int arr[], int n) { if (n == 0) return -1; int totalFoodRequired = r * unit; int foodTillNow = 0; int house = 0; for (house = 0; house < n; ++house) { foodTillNow += arr[house]; if (foodTillNow >= totalFoodRequired) { break; } } if (totalFoodRequired > foodTillNow) return 0; return house + 1; } int main () { int r; cin >> r; int unit; cin >> unit; int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } cout << calculate (r, unit, arr, n); return 0; }
#include <stdio.h> int calculate (int r, int unit, int arr[], int n) { if (n == 0) return -1; int totalFoodRequired = r * unit; int foodTillNow = 0; int house = 0; for (house = 0; house < n; ++house) { foodTillNow += arr[house]; if (foodTillNow >= totalFoodRequired) { break; } } if (totalFoodRequired > foodTillNow) return 0; return house + 1; } int main () { int r; scanf ("%d", &r); int unit; scanf ("%d", &unit); int n; scanf ("%d", &n); int arr[n]; for (int i = 0; i < n; ++i) { scanf ("%d", &arr[i]); } printf ("%d", calculate (r, unit, arr, n)); return 0; }
def calculate (r, unit, arr, n): if n == 0: return -1 totalFoodRequired = r * unit foodTillNow = 0 house = 0 for house in range (n): foodTillNow += arr[house] if foodTillNow>=totalFoodRequired: break if totalFoodRequired > foodTillNow: return 0 return house + 1 r = int (input ()) unit = int (input ()) n = int (input ()) arr = list (map (int, input ().split ())) print (calculate (r, unit, arr, n))
import java.util.*; class Main { public static int solve (int r, int unit, int arr[], int n) { if (arr == null) return -1; int res = r * unit; int sum = 0; int count = 0; for (int i = 0; i < n; i++) { sum = sum + arr[i]; count++; if (sum >= res) break; } if (sum < res) return 0; return count; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int r = sc.nextInt (); int unit = sc.nextInt (); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); System.out.println (solve (r, unit, arr, n)); } }
Question 2:
(Asked in Accenture OnCampus 10 Aug 2022, Slot 2)
Problem Description :
The Binary number system only uses two digits, 0 and 1 and number system can be called binary string. You are required to implement the following function:
int OperationsBinaryString(char* str);
The function accepts a string str as its argument. The string str consists of binary digits eparated with an alphabet as follows:
- – A denotes AND operation
- – B denotes OR operation
- – C denotes XOR Operation
You are required to calculate the result of the string str, scanning the string to right taking one opearation at a time, and return the same.
Note:
- No order of priorities of operations is required
- Length of str is odd
- If str is NULL or None (in case of Python), return -1
Input:
str: 1C0C1C1A0B1
Output:
1
Explanation:
The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1 AND 0 OR 1”, result of the expression becomes 1, hence 1 is returned.
Sample Input:
0C1A1B1C1C1B0A0
Output:
0
#include<bits/stdc++.h> using namespace std; int OperationsBinaryString (char *str) { if (str == NULL) return -1; int i = 1; int a = *str - '0'; str++; while (*str != '\0') { char p = *str; str++; if (p == 'A') a &= (*str - '0'); else if (p == 'B') a |= (*str - '0'); else a ^= (*str - '0'); str++; } return a; } int main () { string s; getline (cin, s); int len = s.size (); char *str = &s[0]; cout << OperationsBinaryString (str); }
#include <stdio.h> #include <string.h> int OperationsBinaryString (char *str) { if (str == NULL) return -1; int i = 1; int a = *str - '0'; str++; while (*str != '\0') { char p = *str; str++; if (p == 'A') a &= (*str - '0'); else if (p == 'B') a |= (*str - '0'); else a ^= (*str - '0'); str++; } return a; } int main () { char str[100]; fgets (str, sizeof (str), stdin); int len = strlen (str); if (str[len - 1] == '\n') { str[len - 1] = '\0'; // Remove the newline character len--; // Decrement the length } int result = OperationsBinaryString (str); printf ("%d\n", result); return 0; }
def OperationsBinaryString(str): a=int(str[0]) i=1 while i< len(str): if str[i]=='A': a&=int(str[i+1]) elif str[i]=='B': a|=int(str[i+1]) else: a^=int(str[i+1]) i+=2 return a str=input() print(OperationsBinaryString(str))
import java.util.*; class Main { public static int operationsBinaryString (String str) { if (str == null) return -1; int res = str.charAt (0) - '0'; for (int i = 1; i < str.length ();) { char prev = str.charAt (i); i++; if (prev == 'A') res = res & (str.charAt (i) - '0'); else if (prev == 'B') res = res | (str.charAt (i) - '0'); else res = res ^ (str.charAt (i) - '0'); i++; } return res; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); String str = sc.next (); System.out.println (operationsBinaryString (str)); } }
Question 3: Password Checker
(Asked in Accenture OnCampus 10 Aug 2022, Slot 3)
You are given a function.
int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument. Implement the function which returns 1 if given string str is valid password else 0.
str is a valid password if it satisfies the below conditions.
- – At least 4 characters
- – At least one numeric digit
- – At Least one Capital Letter
- – Must not have space or slash (/)
- – Starting character must not be a number
Assumption:
Input string will not be empty.
Example:
Input 1:
aA1_67
Input 2:
a987 abC012
Output 1:
1
Output 2:
0
#include<bits/stdc++.h> using namespace std; int CheckPassword (char str[], int n) { //At least 4 characters if (n < 4) return 0; //Starting character must not be a number if (str[0] - '0' >= 0 && str[0] - '0' <= 9) return 0; int a = 0, cap = 0, nu = 0; while (a < n) { //Must not have space or slash (/) if (str[a] == ' ' || str[a] == '/') return 0; //counting capital letters if (str[a] >= 65 && str[a] <= 90) { cap++; } //counting numeric digit else if (str[a] - '0' >= 0 && str[a] - '0' <= 9) nu++; //incrementing for while loop a++; } // returns 1 if there are > 0 numeric digits and capital letters return cap > 0 && nu > 0; } int main () { string s; getline (cin, s); int len = s.size (); char *c = &s[0]; cout << CheckPassword (c, len); }
#include <stdio.h> #include <string.h> int CheckPassword (char str[], int n) { // At least 4 characters if (n < 4) return 0; // Starting character must not be a number if (str[0] - '0' >= 0 && str[0] - '0' <= 9) return 0; int a = 0, cap = 0, nu = 0; while (a < n) { // Must not have space or slash (/) if (str[a] == ' ' || str[a] == '/') return 0; // Counting capital letters if (str[a] >= 'A' && str[a] <= 'Z') { cap++; } // Counting numeric digits else if (str[a] - '0' >= 0 && str[a] - '0' <= 9) { nu++; } // Incrementing for the while loop a++; } // Returns 1 if there are > 0 numeric digits and capital letters return cap > 0 && nu > 0; } int main () { char str[100]; fgets (str, sizeof (str), stdin); int len = strlen (str); if (str[len - 1] == '\n') { str[len - 1] = '\0'; // Remove the newline character len--; // Decrement the length } int result = CheckPassword (str, len); printf ("%d\n", result); return 0; }
def CheckPassword(s,n): if n<4: return 0 if s[0].isdigit(): return 0 cap=0 nu=0 for i in range(n): if s[i]==' ' or s[i]=='/': return 0 if s[i]>='A' and s[i]<='Z': cap+=1 elif s[i].isdigit(): nu+=1 if cap>0 and nu>0: return 1 else: return 0 s=input() a=len(s) print(CheckPassword(s,a))
import java.util.*; class Solution { public static int checkPassword (String str, int n) { if (n < 4) return 0; if (str.charAt (0) >= '0' && str.charAt (0) <= '9') return 0; int num = 0, cap = 0; for (int i = 0; i < n; i++) { if (str.charAt (i) == ' ' || str.charAt (i) == '/') return 0; if (str.charAt (i) >= 'A' && str.charAt (i) <= 'Z') cap++; if (str.charAt (i) >= '0' && str.charAt (i) <= '9') num++; } if (cap > 0 && num > 0) return 1; else return 0; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); String str = sc.next (); System.out.println (checkPassword (str, str.length ())); } }
Question 4:
(Asked in Accenture OnCampus 11 Aug 2022, Slot 1)
You are given a function,
int findCount(int arr[], int length, int num, int diff);
The function accepts an integer array ‘arr’, its length and two integer variables ‘num’ and ‘diff’. Implement this function to find and return the number of elements of ‘arr’ having an absolute difference of less than or equal to ‘diff’ with ‘num’.
Note: In case there is no element in ‘arr’ whose absolute difference with ‘num’ is less than or equal to ‘diff’, return -1.
Example:
Input:
- arr: 12 3 14 56 77 13
- num: 13
- diff: 2
Output:
3
Explanation:
Elements of ‘arr’ having absolute difference of less than or equal to ‘diff’ i.e. 2 with ‘num’ i.e. 13 are 12, 13 and 14.
#include<bits/stdc++.h> using namespace std; int findCount (int n, int arr[], int num, int diff) { int count = 0; for (int i = 0; i < n; ++i) { if (abs (arr[i] - num) <= diff) { count++; } } return count > 0 ? count : -1; } int main () { int n; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int num; cin >> num; int diff; cin >> diff; cout << findCount (n, arr, num, diff); }
#include <stdio.h> #include <stdlib.h> int findCount (int n, int arr[], int num, int diff) { int count = 0; for (int i = 0; i < n; ++i) { if (abs (arr[i] - num) <= diff) { count++; } } return count > 0 ? count : -1; } int main () { int n; scanf ("%d", &n); int arr[n]; for (int i = 0; i < n; ++i) { scanf ("%d", &arr[i]); } int num; scanf ("%d", &num); int diff; scanf ("%d", &diff); printf ("%d\n", findCount (n, arr, num, diff)); return 0; }
def findCount(n, arr, num, diff): count=0 for i in range(n): if(abs(arr[i]-num)<=diff): count+=1 if count: return count return 0 n=int(input()) arr=list(map(int,input().split())) num=int(input()) diff=int(input()) print(findCount(n, arr, num, diff))
import java.util.*; class Main { public static int findCount (int arr[], int length, int num, int diff) { int count = 0; for (int i = 0; i < length; i++) { if (Math.abs (num - arr[i]) <= diff) count++; } return count > 0 ? count : -1; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); int num = sc.nextInt (); int diff = sc.nextInt (); System.out.println (findCount (arr, n, num, diff)); } }
Question 5 :
(Asked in Accenture OnCampus 11 Aug 2022, Slot 2)
Implement the following Function
def differenceofSum(n. m)
The function 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
#include <iostream> 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; } } if (sum2 > sum1) return sum2 - sum1; else return sum1 - sum2; } int main () { int n, m; int result; cin >> n; cin >> m; result = differenceofSum (n, m); cout << result; return 0; }
#include<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; } } if (sum2 > sum1) return sum2 - sum1; else return sum1 - sum2; } int main () { int n, m; int result; scanf ("%d", &n); scanf ("%d", &m); result = differenceofSum (n, m); printf ("%d", result); return 0; }
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))
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)); } }
Question:6
(Asked in Accenture OnCampus 11 Aug 2022, Slot 3)
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 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 as even
NOTE
- Return 0 if array is empty
- Return 0, if array length is 3 or less than 3
Example
Input
arr:3 2 1 7 5 4
Output
7
Explanation
- Second largest among even position elements(1 3 5) is 3
- Second smallest among odd position element is 4
- Thus output is 3+4 = 7
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
8
#include <iostream> #include <algorithm> #include <vector> using namespace std; int LargeSmallSum (vector < int >&arr) { int length = arr.size (); if (length <= 3) { return 0; } vector < int >evenPos; vector < int >oddPos; for (int i = 0; i < length; i++) { if (i % 2 == 0) { evenPos.push_back (arr[i]); } else { oddPos.push_back (arr[i]); } } // Sort the even position array in descending order sort (evenPos.rbegin (), evenPos.rend ()); // Sort the odd position array in ascending order sort (oddPos.begin (), oddPos.end ()); return evenPos[1] + oddPos[1]; } int main () { vector < int >arr = { 3, 2, 1, 7, 5, 4 }; int result = LargeSmallSum (arr); cout << result << endl; return 0; }
#include <stdio.h> int LargeSmallSum (int arr[], int length) { if (length <= 3) { return 0; } int evenPos[100], oddPos[100]; int evenCount = 0, oddCount = 0; for (int i = 0; i < length; i++) { if (i % 2 == 0) { evenPos[evenCount++] = arr[i]; } else { oddPos[oddCount++] = arr[i]; } } // Sort the even position array in descending order for (int i = 0; i < evenCount - 1; i++) { for (int j = 0; j < evenCount - i - 1; j++) { if (evenPos[j] < evenPos[j + 1]) { int temp = evenPos[j]; evenPos[j] = evenPos[j + 1]; evenPos[j + 1] = temp; } } } // Sort the odd position array in ascending order for (int i = 0; i < oddCount - 1; i++) { for (int j = 0; j < oddCount - i - 1; j++) { if (oddPos[j] > oddPos[j + 1]) { int temp = oddPos[j]; oddPos[j] = oddPos[j + 1]; oddPos[j + 1] = temp; } } } return evenPos[1] + oddPos[1]; } int main () { int arr[] = { 3, 2, 1, 7, 5, 4 }; int length = sizeof (arr) / sizeof (arr[0]); int result = LargeSmallSum (arr, length); printf ("%d\n", result); return 0; }
import java.util.*; class Main { public static int largeSmallSum (int[]arr, int n) { if (n <= 3) return 0; ArrayList < Integer > even = new ArrayList < Integer > (); ArrayList < Integer > odd = new ArrayList < Integer > (); even.add (arr[0]); for (int i = 1; i < arr.length; i++) { if (i % 2 == 0) even.add (arr[i]); else odd.add (arr[i]); } Collections.sort (even); Collections.sort (odd); return even.get (even.size () - 2) + odd.get (odd.size () - 2); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); System.out.println (largeSmallSum (arr, n)); } }
length = int(input()) arr = list(map(int, input().split())) even_arr = [] odd_arr = [] for i in range(length): if i % 2 == 0: even_arr.append(arr[i]) else: odd_arr.append(arr[i]) even_arr = sorted(even_arr) odd_arr = sorted(odd_arr) print(even_arr[len(even_arr)-2] + odd_arr[len(odd_arr)-2])
1 8 0 2 3 5 6 Output: 8
Question:7
(Asked in Accenture OnCampus 12 Aug 2022, Slot 1)
Implement the following Function
def ProductSmallestPair(sum, arr)
The function 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
size of Arr = 7
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
size of Arr = 6
Arr:9 8 3 -7 3 9
Sample Output
-21
#include <iostream> #include <algorithm> int productSmallestPair (int *array, int n, int sum) { int answer, temp, i, j, check; if (n < 2) { answer = -1; } else { for (i = 0; i < n; i++) { // sorting of array for (j = i + 1; j < n; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } check = array[0] + array[1]; if (check <= sum) { answer = array[0] * array[1]; } else { answer = 0; } } return answer; } int main () { int n, sum, result, i; std::cin >> sum; std::cin >> n; int *array = new int[n]; for (i = 0; i < n; i++) { std::cin >> array[i]; } result = productSmallestPair (array, n, sum); std::cout << result; delete[]array; return 0; }
#include<stdio.h> int productSmallestPair (int *array, int n, int sum) { int answer, temp, i, j, check; if (n < 2) { answer = -1; } else { for (i = 0; i < n; i++) //sorting of array { for (j = i + 1; j < n; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } check = array[0] + array[1]; if (check <= sum) { answer = array[0] * array[1]; } else { answer = 0; } } return answer; } 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; }
import java.util.*; class Main { public static int productSmallestPair (int arr[], int n, int sum) { if (n <2) return -1; int ans, temp, check; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } check = arr[0] + arr[1]; if (check <= sum) return arr[0] * arr[1]; else return 0; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int sum = sc.nextInt (); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); System.out.println (productSmallestPair (arr, n, sum)); } }
n = int(input()) sum1 = int(input()) arr = list(map(int, input().split())) if n < 2: print('-1') arr = sorted(arr) for i in range(n-1): if arr[i] + arr[i+1] < sum1: print(arr[i] * arr[i+1]) break else: print('0')
Input: 6 4 9 8 3 -7 3 9 Output: -21
Question:8
(Asked in Accenture OnCampus 12 Aug 2022, Slot 2)
N-base notation is a system for writing numbers that 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)
Implement the following function
Char* DectoNBase(int n, int num):
The function accept positive integer n and num Implement the function to calculate the n-base equivalent of num and return the same as a string
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
num Divisor quotient remainder
718 12 59 10(A)
59 12 4 11(B)
4 12 0 4(4)
Sample Input
n: 21
num: 5678
Sample Output
CI8
#include<bits/stdc++.h> using namespace std; string decitoNBase (int n, int num) { string res = ""; int quotient = num / n; vector < int >rem; rem.push_back (num % n); while (quotient != 0) { rem.push_back (quotient % n); quotient = quotient / n; } for (int i = 0; i < rem.size (); i++) { if (rem[i] > 9) { res = (char) (rem[i] - 9 + 64) + res; } else res = to_string (rem[i]) + res; } return res; } int main () { int n, num; cin >> n >> num; cout << decitoNBase (n, num); return 0; }
#include <stdio.h> #includechar *decitoNBase (int n, int num) { char *res = NULL; int quotient = num / n; int *rem = malloc (sizeof (int)); int remSize = 1; rem[0] = num % n; while (quotient != 0) { remSize++; int *temp = realloc (rem, remSize * sizeof (int)); if (temp == NULL) { free (rem); return NULL; } rem = temp; rem[remSize - 1] = quotient % n; quotient = quotient / n; } int resSize = remSize + 1; res = malloc (resSize * sizeof (char)); if (res == NULL) { free (rem); return NULL; } int i, j = 0; for (i = remSize - 1; i >= 0; i--) { if (rem[i] > 9) { res[j] = (char) (rem[i] - 9 + 64); } else { res[j] = (char) (rem[i] + '0'); } j++; } res[j] = '\0'; free (rem); return res; } int main () { int n, num; scanf ("%d %d", &n, &num); char *result = decitoNBase (n, num); printf ("%s\n", result); free (result); return 0; }
import java.util.*; class Main { public static String dectoNBase (int n, int num) { String res = ""; int quotient = num / n; ArrayList < Integer > rem = new ArrayList < Integer > (); rem.add (num % n); while (quotient != 0) { rem.add (quotient % n); quotient = quotient / n; } for (int i = 0; i < rem.size (); i++) { if (rem.get (i) > 9) { res = (char) (rem.get (i) - 9 + 64) + res; } else res = rem.get (i) + res; } return res; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int num = sc.nextInt (); System.out.println (dectoNBase (n, num)); } }
n = int(input()) num = int(input()) reminder = [] quotient = num // n reminder.append(num%n) while quotient != 0: reminder.append(quotient%n) quotient = quotient // n reminder = reminder[::-1] equivalent = '' for i in reminder: if i > 9: a = i - 9 a = 64 + a equivalent+=chr(a) else: equivalent+=str(i) print(equivalent)
Input: 21 5678 Output: CI8
Question:9
(Asked in Accenture Offcampus 1 Aug 2021, Slot 1)
Implement the following functions.a
char*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
#include<bits/stdc++.h> using namespace std; string MoveHyphen (string s, int n) { int count = 0; for (int i = 0; i < n;) { if (s[i] == '-') { count++; s.erase (i, 1); } else i++; } while (count--) { s = '-' + s; } return s; } int main () { string s; cin >> s; int n = s.size (); cout << MoveHyphen (s, n); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> char * MoveHyphen (char *s, int n) { int count = 0; int i = 0; while (i < n) { if (s[i] == '-') { count++; memmove (&s[i], &s[i + 1], n - i); n--; } else { i++; } } char *res = malloc ((n + count + 1) * sizeof (char)); if (res == NULL) { return NULL; } for (i = 0; i < count; i++) { res[i] = '-'; } for (i = count; i < count + n; i++) { res[i] = s[i - count]; } res[count + n] = '\0'; return res; } int main () { char s[100]; scanf ("%s", s); int n = strlen (s); char *result = MoveHyphen (s, n); printf ("%s\n", result); free (result); return 0; }
import java.util.*; class Solution { public static String moveHyphen (String str, int n) { String res = ""; for (int i = 0; i < n; i++) { if (str.charAt (i) == '-') res = str.charAt (i) + res; else res = res + str.charAt (i); } return res; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); String str = sc.next (); System.out.println (moveHyphen (str, str.length ())); } }
inp = input()
count = 0
final = ""
for i in inp:
if i == '-':
count+=1
else:
final+=i
print("-"*count,final)
Output: move-hyphens-to-front --- movehyphenstofront
Question:10
(Asked in Accenture Offcampus 1 Aug 2021, Slot 2)
Problem Statement
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’.
Assumption: num1, num2>=0
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 <iostream> using namespace std; 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; } while (num1 != 0) { p = num1 % 10; sum = carry + p; if (sum > 9) { carry = 1; count++; } else { carry = 0; } num1 = num1 / 10; } while (num2 != 0) { q = num2 % 10; sum = carry + q; if (sum > 9) { carry = 1; count++; } else { carry = 0; } num2 = num2 / 10; } return count; } int main () { int x, y, a; cin >> x >> y; a = numberOfCarries (x, y); cout << a; return 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; } while (num1 != 0) { p = num1 % 10; sum = carry + p; if (sum > 9) { carry = 1; count++; } else carry = 0; num1 = num1 / 10; } while (num2 != 0) { q = num2 % 10; sum = carry + q; if (sum > 9) { carry = 1; count++; } else carry = 0; 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; }
import java.util.*; class Main { public static int numberOfCarries (int num1, int num2) { int count = 0; int temp1 = num1, temp2 = num2; int rem = 0; while (temp1 != 0 && temp2 != 0) { int d1 = temp1 % 10, d2 = temp2 % 10; if ((d1 + d2 + rem) > 9) { count++; int sum = d1 + d2 + rem; sum = sum / 10; rem = sum; } temp1 = temp1 / 10; temp2 = temp2 / 10; } while(temp1!=0) { int d1=temp1%10; if((d1+rem)>9) { count++; int sum=d1+rem; sum=sum/10; rem=sum; } temp1=temp1/10; } while(temp2!=0) { int d2=temp2%10; if((d2+rem)>9) { count++; int sum=d2+rem; sum=sum/10; rem=sum; } temp2=temp2/10; } return count; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int num1 = sc.nextInt (); int num2 = sc.nextInt (); System.out.println (numberOfCarries (num1, num2)); } }
def NumberOfCarries(n1,n2): count=0 carry = 0 if len(n1) <= len(n2): l= len(n1)-1 else: l = len(n2)-1 for i in range(l+1): temp = int(n1[l-i])+int(n2[l-i])+carry if len(str(temp))>1: count+=1 carry = 1 else: carry = 0 return count+carry n1=input() n2=input() print(NumberOfCarries(n1,n2))
Output: 23 563 0
Question:11
(Asked in Accenture Offcampus 1 Aug 2021, Slot 3)
Problem Statement
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 <iostream> #include <cstring> using namespace std; void ReplaceCharacter (char str[], int n, char ch1, char ch2) { for (int i = 0; i < n; i++) { if (str[i] == ch1) { str[i] = ch2; } else if (str[i] == ch2) { str[i] = ch1; } } cout << str; } int main () { char a[100]; char b, c; int len; cin >> a; cin >> b; cin >> c; len = std::strlen (a); ReplaceCharacter (a, len, b, c); return 0; }
#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; }
import java.util.*; class Solution { public static void replaceChar (String str, char ch1, char ch2) { String res = ""; for (int i = 0; i < str.length (); i++) { if (str.charAt (i) == ch1) res = res + ch2; else if (str.charAt (i) == ch2) res = res + ch1; else res = res + str.charAt (i); } System.out.println (res); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); String str = sc.next (); char ch1 = sc.next ().charAt (0); char ch2 = sc.next ().charAt (0); replaceChar (str, ch1, ch2); } }
def swap (user_input, str1, str2): result = '' if user_input != None: result = user_input.replace(str1, '*').replace(str2, str1).replace('*', str2) return result return 'Null' user_input = input() str1, str2 = map(str,input().split()) print(swap(user_input, str1,str2))
Output: apples a p paales
Question:12
(Asked in Accenture Offcampus 2 Aug 2021, Slot 1)
Problem Statement
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 <iostream> using namespace std; 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; } return 0; } int main() { int x, y, z; int result; cin >> x; cin >> y; cin >> z; result = operationChoices(x, y, z); cout << result; return 0; }
#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); return 0; }
import java.util.*; class Solution { public static int operationChoices (int c, int a, int b) { int res = 0; switch (c) { case 1: res = a + b; break; case 2:res = a - b; break; case 3:res = a * b; break; case 4:res = a / b; break; } return res; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int c = sc.nextInt (); int a = sc.nextInt (); int b = sc.nextInt (); System.out.println (operationChoices (c, a, b)); } }
def operationChoices(c,a,b): if c == 1 : return(a+b) elif c == 2: return(a-b) elif c == 3: return(a*b) else: return(a//b) c,a,b = map(int,input().split()) print(operationChoices(c, a, b))
Output: 2 16 12 20 -4
Question:13
(Asked in Accenture Offcampus 2 Aug 2021, Slot 2)
Problem Statement
You are given a function,
Int MaxExponents (int a , int b);
You have to find and return the number between ‘a’ and ‘b’ ( range inclusive on both ends) which has the maximum exponent of 2.
The algorithm to find the number with maximum exponent of 2 between the given range is
- Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
- Find the exponent (power) of 2 for each ‘i’ and store the number with maximum exponent of 2 so faqrd in a variable , let say ‘max’. Set ‘max’ to ‘i’ only if ‘i’ has more exponent of 2 than ‘max’.
- Return ‘max’.
Assumption: a <b
Note: If two or more numbers in the range have the same exponents of 2 , return the small number.
Example
- Input:
- 7
- 12
- Output:
- 8
Explanation:
Exponents of 2 in:
7-0
8-3
9-0
10-1
11-0
12-2
Hence maximum exponent if two is of 8.
#include<bits/stdc++.h> using namespace std; int count (int n) { int c = 0; while (n % 2 == 0 && n != 0) { c++; n = n / 2; } return c; } int maxExponents (int a, int b) { int max = 0, num = 0, ans; for (int i = a; i <= b; i++) { int temp = count (i); if (temp > max) { max = temp; num = i; } } return num; } int main () { int a, b; cin >> a >> b; cout << maxExponents (a, b); return 0; }
#include <stdio.h> int count (int n) { int c = 0; while (n % 2 == 0 && n != 0) { c++; n = n / 2; } return c; } int maxExponents (int a, int b) { int max = 0, num = 0, ans; for (int i = a; i <= b; i++) { int temp = count (i); if (temp > max) { max = temp; num = i; } } return num; } int main () { int a, b; scanf ("%d %d", &a, &b); printf ("%d", maxExponents (a, b)); return 0; }
import java.util.*; class Main { public static int count (int i) { int count = 0; while (i % 2 == 0 && i != 0) { count += 1; i = i / 2; } return count; } public static int maxExponents (int a, int b) { int max = 0, num = 0, ans; for (int i = a; i <= b; i++) { int temp = count (i); if (temp > max) { max = temp; num = i; } } return num; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int a = sc.nextInt (); int b = sc.nextInt (); System.out.println (maxExponents(a, b)); } }
def countExponents(i): count = 0 while i%2 == 0 and i != 0: count+=1 i = i//2 return count def maxExponents(a, b): maximum, number = 0, a for i in range(a,b): temp = countExponents(i) if temp>maximum: maximum, number = temp, i return number a, b = map(int,input().split()) print(maxExponents(a, b))
Question : 14
(Asked in Accenture Offcampus 2 Aug 2021, Slot 3)
Problem Statement
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
510
#include<stdio.h> int Calculate (int, int); int main () { int m, n, result; printf ("Enter the value of m : "); scanf ("%d", &m); printf ("Enter the value of n : "); scanf ("%d", &n); result = Calculate (n, m); printf ("%d", result); return 0; } int Calculate (int n, int m) { int i, sum = 0; for (i = m; i <= n; i++) { if ((i % 3 == 0) && (i % 5 == 0)) { sum = sum + i; } } return sum; }
#include<vits/stdc++.h> using namespace std; int Calculate (int, int); int main () { int m, n, result; cout << "Enter the value of m :"; cin >> m; cout << "Enter the value of n :"; cin >> n; result = Calculate (n, m); cout << result; return 0; } int Calculate (int n, int m) { int i, sum = 0; for (i = m; i <= n; i++) { if ((i % 3 == 0) && (i % 5 == 0)) { sum = sum + i; } } return sum; }
import java.util.Scanner; public class Main { int Calculate (int m, int n) { int sum = 0; 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); } }
def Calculate(m, n): sum = 0 for i in range(m, n+1): if (i % 3 == 0) and (i % 5 == 0): sum = sum + i return sum m = int(input("Enter the value of m: ")) n = int(input("Enter the value of n: ")) result = Calculate(m, n) print(result)
Question 15
Problem Statement
You are required to input the size of the matrix then the elements of matrix, then you have to divide the main matrix in two sub matrices (even and odd) in such a way that element at 0 index will be considered as even and element at 1st index will be considered as odd and so on. then you have sort the even and odd matrices in ascending order then print the sum of second largest number from both the matrices
Example
- enter the size of array : 5
- enter element at 0 index : 3
- enter element at 1 index : 4
- enter element at 2 index : 1
- enter element at 3 index : 7
- enter element at 4 index : 9
Sorted even array : 1 3 9
Sorted odd array : 4 7
7
#include <stdio.h> int main() { int arr[100]; int length, i, j, oddlen, evenlen, temp; int odd[50], even[50]; printf("Enter the length of the array: "); scanf("%d", &length); for (i = 0; i < length; i++) { printf("Enter element at index %d: ", i); scanf("%d", &arr[i]); } if (length % 2 == 0) { oddlen = length / 2; evenlen = length / 2; } else { oddlen = length / 2; evenlen = (length / 2) + 1; } for (i = 0; i < length; i++) { if (i % 2 == 0) { even[i / 2] = arr[i]; } else { odd[i / 2] = arr[i]; } } for (i = 0; i < evenlen - 1; i++) { for (j = i + 1; j < evenlen; j++) { if (even[i] > even[j]) { temp = even[i]; even[i] = even[j]; even[j] = temp; } } } for (i = 0; i < oddlen - 1; i++) { for (j = i + 1; j < oddlen; j++) { if (odd[i] > odd[j]) { temp = odd[i]; odd[i] = odd[j]; odd[j] = temp; } } } printf("\nSorted even array: "); for (i = 0; i < evenlen; i++) { printf("%d ", even[i]); } printf("\n"); printf("Sorted odd array: "); for (i = 0; i < oddlen; i++) { printf("%d ", odd[i]); } printf("\n"); printf("%d", even[evenlen - 2] + odd[oddlen - 2]); return 0; }
#include<iostream> using namespace std; int main () { int arr[100]; int length, i, j, oddlen, evenlen, temp, c, d; int odd[50], even[50]; cout << "enter the length of array : "; cin >> length; for (i = 0; i < length; i++) { cout << "Enter element at " << i << " index : "; cin >> arr[i]; } if (length % 2 == 0) { oddlen = length / 2; evenlen = length / 2; } else { oddlen = length / 2; evenlen = (length / 2) + 1; } for (i = 0; i < length; i++) // seperation of even and odd array { if (i % 2 == 0) { even[i / 2] = arr[i]; } else { odd[i / 2] = arr[i]; } } for (i = 0; i < evenlen - 1; i++) // sorting of even array { for (j = i + 1; j < evenlen; j++) { temp = 0; if (even[i] > even[j]) { temp = even[i]; even[i] = even[j]; even[j] = temp; } } } for (i = 0; i < oddlen - 1; i++) // sorting of odd array { for (j = i + 1; j < oddlen; j++) { temp = 0; if (odd[i] > odd[j]) { temp = odd[i]; odd[i] = odd[j]; odd[j] = temp; } } } cout << "\nSorted even array : "; // printing even array for (i = 0; i < evenlen; i++) { cout << even[i] << " "; } cout << "\n"; cout << "Sorted odd array : "; // printing odd array for (i = 0; i < oddlen; i++) { cout << odd[i] << " "; } cout << endl; cout << even[evenlen - 2] + odd[oddlen - 2]; // printing final result }
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); System.out.print ("Enter size of array : "); int arrsize = sc.nextInt (); int[] main = new int[arrsize]; ArrayList < Integer > even = new < Integer > ArrayList (); ArrayList < Integer > odd = new < Integer > ArrayList (); System.out.println ("Enter " + arrsize + " Elements"); for (int i = 0; i < arrsize; i++) main[i] = sc.nextInt (); for (int i = 0; i < arrsize; i++) { if (i % 2 == 0) even.add (main[i]); else odd.add (main[i]); } Collections.sort (even); Collections.sort (odd); System.out.println ("Sorted even array "); for (int e:even) System.out.print (e + " "); System.out.println (); System.out.println ("sorted odd array "); for (int e:odd) System.out.print (e + " "); System.out.println (); int evensec = even.get (even.size () - 2); int oddsec = odd.get (odd.size () - 2); System.out.println ("Second Largest Element in Even List is:" + evensec); System.out.println ("Second Largest Element in Odd List is:" + oddsec); System. out.println ("Sum Of Second Largest Element Of Odd and Even List:" + (evensec + oddsec)); } }
array = [] evenArr = [] oddArr = [] n = int(input("Enter the size of the array:")) for i in range(0,n): number = int(input("Enter Element at {} index:".format(i))) array.append(number) if i % 2 == 0: evenArr.append(array[i]) else: oddArr.append(array[i]) evenArr = sorted(evenArr) print("Sorted Even Array:", evenArr[0:len(evenArr)]) oddArr = sorted(oddArr) print("Sorted Odd Array:", oddArr[0:len(oddArr)]) print(evenArr[1] + oddArr[1]) evenArr = sorted(evenArr) print("Sorted Even Array:", evenArr[0:len(evenArr)]) oddArr = sorted(oddArr) print("Sorted Odd Array:", oddArr[0:len(oddArr)]) print(evenArr[1] + oddArr[1])
Question : 16
Instructions: You are required to write the code. You can click on compile and run anytime to check compilation/execution status. The code should be logically/syntactically correct.
Problem: Write a program in C to display the table of a number and print the sum of all the multiples in it.
Test Cases:
Test Case 1:
Input:
5
Expected Result Value:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50
275
Test Case 2:
Input:
12
Expected Result Value:
12, 24, 36, 48, 60, 72, 84, 96, 108, 120
660
#include<stdio.h> int main () { int n, i, value = 0, sum = 0; printf ("Enter the number for which you want to know the table : "); scanf ("%d", &n); for (i = 1; i <= 10; ++i) { value = n * i; printf ("%d ", value); sum = sum + value; } printf ("\nsum is %d", sum); return 0; }
#include<iostream> using namespace std; int main () { int n, i, value = 0, sum = 0; cout << "Enter the number for which you want to know the table: "; cin >> n; for (i = 1; i <= 10; ++i) { value = n * i; cout << value << " "; sum = sum + value; } cout << "\nsum is " << sum; return 0; }
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter Any No:"); int no = sc.nextInt(); int sum=0,value=1; for(int i=1 ; i<=10 ; i++) { value = no*i; System.out.print(value+" "); sum=sum+value; } System.out.println("\nSum is : "+sum); } }
table_number = int(input()) sum = 0 for i in range(1, 11): value = table_number * i print(value, end=" ") sum = sum + value print() print(sum)
Question : 17
Instructions: You are required to write the code. You can click on compile and run anytime to check compilation/execution status. The code should be logically/syntactically correct.
Question: Write a program in C such that it takes a lower limit and upper limit as inputs and print all the intermediate palindrome numbers.
Test Cases:
TestCase 1:
Input :
10 , 80
Expected Result:
11 , 22 , 33 , 44 , 55 , 66 , 77.
Test Case 2:
Input:
100,200
Expected Result:
101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191.
#include<stdio.h> int main () { int i, n, reverse, d, f, l; printf ("enter the starting \n"); scanf ("%d", &f); printf ("enter the ending\n"); scanf ("%d", &l); for (i = f; i <= l; i++) { reverse = 0; n = i; while (n != 0) { d = n % 10; reverse = reverse * 10 + d; n = n / 10; } if (i == reverse) printf ("%d ", i); } return 0; }
#include<iostream> using namespace std; int reverse (int); int main () { int i, f, l; cout << "enter the starting \n", f; cin >> f; cout << "enter the ending\n", l; cin >> l; for (i = f; i <= l; i++) { if (i == reverse (i)) cout << i << " "; } return 0; } int reverse (int a) { int n = 0, d = 0, rev = 0; n = a; while (n != 0) { d = n % 10; rev = rev * 10 + d; n = n / 10; } return rev; }
import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; public class Main { static int palindrome (int no1) { int rem = 0; int div = no1; while (div != 0) { int r = div % 10; rem = (rem * 10) + r; div = div / 10; } return rem; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); System.out.println ("Enter Upper and Lower Limit"); int ul = sc.nextInt (); int ll = sc.nextInt (); for (int i = ul; i <= ll; i++) { if (i == palindrome (i)) System.out.print(i+" "); } } }
# Palindrome Number Checking first_number = int(input()) second_number = int(input()) for i in range(first_number, second_number+1): reverse = 0 temp = i while temp != 0: remainder = temp % 10 reverse = (reverse * 10)+remainder temp = temp // 10 if i == reverse: print(reverse, end=" ")
Question : 18
Instructions: You are required to write the code. You can click on compile & run anytime to check the compilation/ execution status of the program. The submitted code should be logically/syntactically correct and pass all the test cases.
Ques: The program is supposed to calculate the sum of distance between three points from each other.
For
x1 = 1 y1 = 1
x2 = 2 y2 = 4
x3 = 3 y3 = 6
Distance is calculated as : sqrt(x2-x1)2 + (y2-y1)2
#include <iostream> #includeusing namespace std; int main() { float x1, y1, x2, y2, x3, y3; cout << "Enter x1,y1: "; cin >> x1 >> y1; cout << "Enter x2,y2: "; cin >> x2 >> y2; cout << "Enter x3,y3: "; cin >> x3 >> y3; float firstDiff = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); float secondDiff = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2)); float thirdDiff = sqrt(pow(x3 - x1, 2) + pow(y3 - y1, 2)); cout << firstDiff + secondDiff + thirdDiff; return 0; }
#include<stdio.h> #includeint main() { float x1,y1,x2,y2,x3,y3; printf("Enter x1,y1 : "); scanf("%f %f",&x1,&y1); printf("Enter x2,y2 : "); scanf("%f %f",&x2,&y2); printf("Enter x3,y3 : "); scanf("%f %f",&x3,&y3); float firstDiff =(float) sqrt (pow (x2 - x1, 2) + pow (y2 - y1, 2)); float secondDiff =(float) sqrt (pow (x3 - x2, 2) + pow (y3 - y2, 2)); float thirdDiff =(float) sqrt (pow (x3 - x1, 2) + pow (y3 - y1, 2)); printf("%f",(firstDiff + secondDiff + thirdDiff)); return 0; }
import java.util.*; class Main { public static void main (String[]args) { Scanner sc = new Scanner (System.in); float x1 = sc.nextFloat(); float y1 = sc.nextFloat(); float x2 = sc.nextFloat(); float y2 = sc.nextFloat(); float x3 = sc.nextFloat(); float y3 = sc.nextFloat(); float firstDiff =(float) Math.sqrt (Math.pow (x2 - x1, 2) + Math.pow (y2 - y1, 2)); float secondDiff =(float) Math.sqrt (Math.pow (x3 - x2, 2) + Math.pow (y3 - y2, 2)); float thirdDiff =(float) Math.sqrt (Math.pow (x3 - x1, 2) + Math.pow (y3 - y1, 2)); System.out.println (firstDiff + secondDiff + thirdDiff); } }
import math x1, y1 = 1, 1 x2, y2 = 2, 4 x3, y3 = 3, 6 first_diff = math.sqrt(math.pow(x2-x1, 2) + math.pow(y2-y1, 2)) second_diff = math.sqrt(math.pow(x3-x2, 2) + math.pow(y3-y2, 2)) third_diff = math.sqrt(math.pow(x3-x1, 2) + math.pow(y3-y1, 2)) print(round(first_diff,2), round(second_diff,2), round(third_diff,2))
Question : 19
Find the maximum value and its index in the array
Problem Statement :
You are given a function, void MaxInArray(int arr[], int length); The function accepts an integer array ‘arr’ of size ‘length’ as its argument. Implement the function to find the maximum element of the array and print the maximum element and its index to the standard output
(STDOUT). The maximum element and its index should be printed in separate lines.
Note:
- Array index starts with 0
- Maximum element and its index should be separated by a line in the output
- Assume there is only 1 maximum element in the array
- Print exactly what is asked, do not print any additional greeting messages
Example:
Input:
23 45 82 27 66 12 78 13 71 86
Output:
86
9
Explanation:
86 is the maximum element of the array at index 9.
#include<bits/stdc++.h> using namespace std; void MaxInArray (int arr[], int length) { int max = INT_MIN, index = -1; for (int i = 0; i < length; i++) { if (arr[i] > max) { max = arr[i]; index = i; } } cout << max << endl << index; } int main () { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; MaxInArray (arr, n); return 0; }
#include <stdio.h> #include <limits.h> void MaxInArray (int arr[], int length) { int max = INT_MIN, index = -1; for (int i = 0; i < length; i++) { if (arr[i] > max) { max = arr[i]; index = i; } } printf ("%d\n%d\n", max, index); } int main () { int n; scanf ("%d", &n); int arr[n]; for (int i = 0; i < n; i++) scanf ("%d", &arr[i]); MaxInArray (arr, n); return 0; }
import java.util.*; class Main { public static void maxInArray (int arr[], int length) { int max = arr[0], index = 0; for (int i = 1; i < length; i++) if (arr[i] > max) { max = arr[i]; index = i; } System.out.println (max); System.out.println (index); } public static void main (String[]args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt (); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt (); maxInArray (arr, arr.length); } }
Question : 20
Autobiographical Number
Problem Statement :
An Autobiographical Number is a number N such that the first digit of N represents the count of how many zeroes are there in N, the second digit represents the count of how many ones are there in N and so on.
You are given a function, def FindAutoCount(n):
The function accepts string “n” which is a number and checks whether the number is an autobiographical number or not. If it is, an integer is returned, i.e. the count of distinct numbers in ‘n’. If not, it returns 0.
Assumption:
- The input string will not be longer than 10 characters.
- Input string will consist of numeric characters.
Note:
If string is None return 0.
Example:
Input:
n: “1210”
Output:
3
Explanation:
0th position in the input contains the number of 0 present in input, i.e. 1, in 1st position the count of number of 1s in input i.e. 2, in 2nd position the count of 2s in input i.e. 1, and in 3rd position the count of 3s i.e. 0, so the number is an autobiographical number.
Now unique numbers in the input are 0, 1, 2, so the count of unique numbers is 3. So 3 is returned.
#include<bits/stdc++.h> using namespace std; int FinndAutoCount (string n) { int sum = 0; set < char >st; for (int i = 0; i < n.size (); i++) { sum += (n[i] - '0'); st.insert (n[i]); } if (sum != n.size ()) return 0; return st.size (); } int main () { string n; cin >> n; cout << FinndAutoCount (n); return 0; }
#include <stdio.h> #include <string.h> int FindAutoCount (char n[]) { int sum = 0; int length = strlen (n); int uniqueDigits[10] = { 0 }; for (int i = 0; i < length; i++) { sum += (n[i] - '0'); uniqueDigits[n[i] - '0'] = 1; } if (sum != length) return 0; int count = 0; for (int i = 0; i < 10; i++) { if (uniqueDigits[i] == 1) count++; } return count; } int main () { char n[100]; scanf ("%s", n); printf ("%d", FindAutoCount (n)); return 0; }
import java.util.*; class Solution { public static int findAutoCount (String n) { int sum = 0; for (int i = 0; i < n.length (); i++) sum = sum + Integer.parseInt (n.charAt (i) + ""); if (sum == n.length ()) { int count = 0; int arr[] = new int[10]; for (int i = 0; i < n.length (); i++) arr[Integer.parseInt (n.charAt (i) + "")]++; for (int i = 0; i < arr.length; i++) if (arr[i] != 0) count++; return count; } else return 0; } public static void main (String[]args) { Scanner sc = new Scanner (System.in); String str = sc.next (); System.out.println (findAutoCount (str)); } }
FAQs on Accenture Coding Questions and Solution 2025
Question 1: In which all coding languages we can solve the Coding Question asked in Accenture Coding Round?
Students can use any of the following languages to solve the Coding Questions
- C
- C++
- Python
- Java
- Dot Net
Question 2: In which all coding languages we can solve the Coding Question asked in Accenture Coding Round?
For the complete Online Assessment of the Exam, Accenture uses CoCubes as a platform to conduct the exam.
Question 3: What is the difficulty of the Coding Questions asked in Accenture Coding Test 2025?
The Coding Questions asked in Accenture are of two difficulty type
- 1 Question with Medium to High difficulty
- 1 Question High difficulty
Question 3: How to Clear Accenture Coding Round?
Prepare for PrepInsta’s best Coding Question material, this will help you understand the difficulty of the questions that can be asked in the exam and also Students in PrepInsta’s Online class for Accenture will get the opportunity to solve all the previous year questions that were asked in Accenture Coding Round.
Login/Signup to comment
65 comments on “Accenture Coding Questions and Solution 2025”
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
def a(n):
l=[]
sum=0
for i in n:
l.append(ord(i)-48)
for i in range(len(l)):
if l[i]>0:
sum+=i
return(sum)
n=input()
print(a(n))
My Q-19 python solution (is it correct? )
arr = list(map(int,input().split()))
arr.sort()
print(arr)
print(arr[-1])
print(len(arr)-1)
no
u just change the previous positions of arr
Thank you so much PrepInsta. I got placed in Accenture because of you guys!
Yes arnab, but we would recommend that you must visit those company specific coding pages, that would be more relevant
Hey Arnab!
Congratulations on getting placed, we wish you all the best for your future.
Also, guys if you are looking for Accenture Preparation, check out this link.
Click here
In question number 19 it is asked to find maximum element of an array and its index.
the code is given here is wrong.
The code should be-
#include
using namespace std;
void MaxInArray (int arr[],int length)
{
int max=arr[0];
int maxIdx=0;
for (int i = 0; i max){
maxIdx = i;
max = arr[i];
}
}
cout << max << endl <> n;
int arr[n];
for (int i = 0; i > arr[i];
MaxInArray (arr, n);
return 0;
}
Thank you for the submission.
Sir could you please let me know from where can I get off campus updates so that I don’t miss out on any single opportunity?
There is no surety of that, but yes there are high chances that you may find a few questions similar to the questions given above
Hey Himanshu, don’t worry we are right here with all the off campus updates.
Click here
There is a mistake in question-3,
You are added array[0], array[1] and returning the answer directly without removing duplicates, Suppose in the given array after sorted will be like{2,2,4,5,8} now we want to product for (2,3) but according to your program it takes (2,2) and the answer will be wrong they clearly mentioned in question (arr[ j]!=arr[k]).
Sir there has been so many questions of the same pattern you taught, PrepInsta has been so helpful in the whole learning process for me.
Hey Jessica, thank you so much for your valuable feedback.
Also, guys if you looking for placement preparation course which covers whole aptitude, Python, Java DSA, Competitive Coding etc. and past year’s Accenture questions along with Interview Preparation material, Click here
Team, I am in a need of support from your mentoring team, how can I connect with you guys for instant response?
Hey Bhavesh, Our mentoring team is available on 8448440710 via call and on 7044817397 via whatsapp.
Do contact us, wed love to help you out 🙂
when can i apply for accenture off-campus drive for 2022 batch?
Hey Minal, stay updated on our Instagram and Telegram groups, you’ll get all the updates from there regarding Accenture hiring and whole recruitment process as well.
Click here
hi prepinsta, while running the program1 in my system i’m getting error ,can u pls help to solve the error
n=int(input( ))
ValueError: invalid literal for int() with base 10: ”
I have purchased Accenture Prime Mock Material but i am unable to view after buying
Hey Raghvendra, if you are facing any issues regarding access, you can call us at 8448440710 or whatsapp message us on 7044817397.
Our team would love to help you out!
I brought Accenture Prime mock material yesterday
how can i access it
where are the mock test i could not find please help
Hey lalitha, we are sorry for the inconveniences, you can access it from here.
Click here