Cognizant GenC Elevate Sample Coding Question 4
Question 4
In this article, we will discuss about Coding Question with their solution in C++, java and Python. This is the Basic Mathematics based question in which we have to find the remainder when number n is divided by 11.
Question 4
Given a number n, the task is to find the remainder when n is divided by 11. The input number may be very large.
Since the given number can be very large, you can not use n % 11.
Input Specification:
inputs a large number in the form of a string
Output Specification:
Return the remainder modulo 11 of input1
Example1:
Input : str = 13589234356546756
Output : 6
Example2:
Input : str = 3435346456547566345436457867978
Output : 4
Example3:
input: str = 121
Output: 0
C++
Java
Python
C++
#include <bits/stdc++.h>
using namespace std;
int remainder(string str)
{
int len = str.length();
int num, rem = 0;
for (int i = 0; i < len; i++) {
num = rem * 10 + (str[i] - '0');
rem = num % 11;
}
return rem;
}
// Driver code
int main()
{
string str;
cin>>str;
cout << remainder(str);
return 0;
}
Java
import java.util.*;
import java.io.*;
public class Main{
static int remainder(String str)
{
int len = str.length();
int num, rem = 0;
for (int i = 0; i < len; i++) {
num = rem * 10 + (str.charAt(i) - '0');
rem = num % 11;
}
return rem;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(remainder(str));
}
}
Python
s,rem= input(),0
for i in s :
rem = (rem * 10 + int(i))% 11
print(rem)