

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find N % (Remainder with 4) for a large value of N in C++
In this problem, we are given a string num representing a large integer. Our task is to Find N % (Remainder with 4) for a large value of N.
Problem Description − we will be finding the remainder of the number with 4.
Let’s take an example to understand the problem,
Input
num = 453425245
Output
1
Solution Approach
A simple solution to the problem is by using the fact that the remainder of the number with 4 can be found using the last two digits of the number. So, for any large number, we can find the remainder by dividing the number’s last two digits by 4.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; int calc4Mod(string num, int len) { int rem; if (len == 1) rem = num[0] - '0'; else rem = (num[len - 2] - '0') * 10 + num[len - 1] - '0'; return (rem % 4); } int main() { string num = "84525765476513"; int len = num.length(); cout<<"The remainder of the number with 4 is "<<calc4Mod(num, len); return 0; }
Output
The remainder of the number with 4 is 1
- Related Questions & Answers
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n! in C++
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Program to find greater value between a^n and b^n in C++
- Program to find remainder after dividing n number of 1s by m in Python
- Construct Pushdown automata for L = {a(2*m)c(4*n)dnbm | m,n = 0} in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Find sum of Series with n-th term as n^2 - (n-1)^2 in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Program to find N-th term of series 2, 4, 3, 4, 15… in C++
- Construct a Turing Machine for L = {a^n b^n | n>=1}
Advertisements