Write a program to find the modulus of two numbers without using the '%' operator.
Example 1:
Input: num=100, division=8
Output: 4
Approach:
Java
public class NumberModulus {public static void main(String[] args) {int num = 100;int division = 8;int modulus = getModulus(num, division);System.out.println("Modulus is " + modulus);}private static int getModulus(int num, int division) {return num-division*(num/division);}}
C++
#include <bits/stdc++.h>using namespace std;//function to find mod of a//given number with some numberint getModulus(int num,int division){return num-division*(num/division);}int main(){int num=100;int division=8;int modulus= getModulus(num,division);cout<<"Modulus is ";cout<<modulus<<"\n";return 0;}
No comments:
Post a Comment