- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find remainder when large number is divided by 11 in C++
In this problem, we are given a string num which is a large number. Our task is to create a program to find remainder when large number is divided by 11 in C++.
Problem Description − We need to find the remainder when the number defined by the string is divided by 11.
Let’s take an example to understand the problem
Input
num = “43212981843718452”
Output
7
Solution Approach
To find the remainder, we obviously need to divide the number. But dividing huge number is a complex process so to ease up the process, we will divide digit by digit. And store the remainder that follows. This process is to be continued for the whole string that contains number from MSB to LSB. And In the end the remainder is printed.
Program to illustrate the working of our solution
Example
#include <iostream> #include <string.h> using namespace std; int calcRem(string num){ int currDigit, rem = 0; for (int i = 0; i < num.length(); i++) { currDigit = rem * 10 + (num[i] - '0'); rem = currDigit % 11; } return rem; } int main() { string num = "43212981843718452"; cout<<"The remainder when large number is divided by 11 is"<<calcRem(num); return 0; }
Output
The remainder when large number is divided by 11 is 7
Advertisements