
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
- Find the value of n:$4^{2n} times 4^n = 2^{12}$
- Consider the numbers ( 4^{n} ), where ( n ) is a natural number. Check whether there is any value of ( n ) for which ( 4^{n} ) ends with the digit zero.
- If $frac{n}{4}-5=frac{n}{6}+frac{1}{2}$, find the value of $n$.
- Find the value of n from the following:$11^n times 11^2=11^4$
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Find value of a and b."\n
- Find the sum:( 4-frac{1}{n}+4-frac{2}{n}+4-frac{3}{n}+ldots ) upto ( n ) terms
- MySQL query to get the highest value from a single row with multiple columns\n\n\n\n\n\n\n\n\n\n
- Find the value of x."\n
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- Find value of n if $7^{2n} = 49 times 7^n$.
- Program to find remainder after dividing n number of 1s by m in Python
- Program to find greater value between a^n and b^n in C++
- Find the value of $4m^2 n^2 times 7m^3$ when $m= frac{-1}{2}$ and $n= 4$.

Advertisements