Given two floating-point numbers, find the remainder.
Examples:
Input: a = 36.5, b = 5.0
Output: 1.5
Input: a = 9.7, b = 2.3
Output: 0.5
A simple solution is to do repeated subtraction.
C++
#include <bits/stdc++.h>
using namespace std;
double findMod( double a, double b)
{
double mod;
if (a < 0)
mod = -a;
else
mod = a;
if (b < 0)
b = -b;
while (mod >= b)
mod = mod - b;
if (a < 0)
return -mod;
return mod;
}
int main()
{
double a = 9.7, b = 2.3;
cout << findMod(a, b);
return 0;
}
|
Java
class GFG
{
static double findMod( double a, double b)
{
if (a < 0 )
a = -a;
if (b < 0 )
b = -b;
double mod = a;
while (mod >= b)
mod = mod - b;
if (a < 0 )
return -mod;
return mod;
}
public static void main (String[] args)
{
double a = 9.7 , b = 2.3 ;
System.out.print(findMod(a, b));
}
}
|
Python3
def findMod(a, b):
if (a < 0 ):
a = - a
if (b < 0 ):
b = - b
mod = a
while (mod > = b):
mod = mod - b
if (a < 0 ):
return - mod
return mod
a = 9.7 ; b = 2.3
print (findMod(a, b))
|
C#
using System;
class GFG {
static double findMod( double a, double b)
{
if (a < 0)
a = -a;
if (b < 0)
b = -b;
double mod = a;
while (mod >= b)
mod = mod - b;
if (a < 0)
return -mod;
return mod;
}
public static void Main ()
{
double a = 9.7, b = 2.3;
Console.WriteLine(findMod(a, b));
}
}
|
Javascript
<script>
function findMod(a, b)
{
let mod;
if (a < 0)
mod = -a;
else
mod = a;
if (b < 0)
b = -b;
while (mod >= b)
mod = mod - b;
if (a < 0)
return -mod;
return mod;
}
let a = 9.7, b = 2.3;
document.write(findMod(a, b));
</script>
|
PHP
<?php
function findMod( $a , $b )
{
if ( $a < 0)
$a = - $a ;
if ( $b < 0)
$b = - $b ;
$mod = $a ;
while ( $mod >= $b )
$mod = $mod - $b ;
if ( $a < 0)
return - $mod ;
return $mod ;
}
$a = 9.7; $b = 2.3;
echo findMod( $a , $b );
?>
|
We can use the inbuilt fmod function to find the modulus of two floating-point numbers.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
double a = 9.7, b = 2.3;
cout << fmod (a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
double a = 9.7 , b = 2.3 ;
System.out.print((a % b));
}
}
|
Python3
from math import fmod
if __name__ = = '__main__' :
a = 9.7
b = 2.3
print (fmod(a, b))
|
C#
using System;
class GFG{
static void Main()
{
double a = 9.7;
double b = 2.3;
Console.WriteLine(a % b);
}
}
|
Javascript
<script>
let a = 9.7;
let b = 2.3;
document.write(a%b);
</script>
|
PHP
<?php
$a = 9.7; $b = 2.3;
echo fmod ( $a , $b );
?>
|
Using the abs() function :
Approach:
The abs() function returns the absolute value of a number, which in this case, will be the modulus of the two given floating-point numbers.
- Import the time module to measure the time taken by the program.
- Assign the floating-point numbers to the variables a and b.
- Use the time.time() function to record the start time of the program.
- Calculate the modulus of a and b using the % operator and the abs() function.
- Use the time.time() function again to record the end time of the program.
- Print the modulus of a and b, along with the message “Modulus of a and b is:”.
- Print the time taken by the program, along with the message “Time taken:”.
C++
#include <iostream>
#include <cmath>
#include <chrono>
int main() {
double a = 9.7;
double b = 2.3;
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
double modulus = std:: abs (std:: fmod (a, b));
std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();
std::cout << "Modulus of " << a << " and " << b << " is: " << modulus << std::endl;
std::cout << "Time taken: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() / 1000.0 << " seconds" << std::endl;
return 0;
}
|
Java
import java.time.Duration;
import java.time.Instant;
public class Main {
public static void main(String[] args) {
double a = 9.7 ;
double b = 2.3 ;
Instant startTime = Instant.now();
double modulus = Math.abs(a % b);
Instant endTime = Instant.now();
System.out.println( "Modulus of " + a + " and " + b + " is: " + modulus);
double durationInSeconds = Duration.between(startTime, endTime).toMillis() / 1000.0 ;
System.out.println( "Time taken: " + durationInSeconds + " seconds" );
}
}
|
Python3
import time
a = 9.7
b = 2.3
start_time = time.time()
modulus = abs (a % b)
end_time = time.time()
print ( "Modulus of" , a, "and" , b, "is:" , modulus)
print ( "Time taken:" , end_time - start_time, "seconds" )
|
Output
Modulus of 9.7 and 2.3 is: 0.5
Time taken: 1.1682510375976562e-05 seconds
Time Complexity: O(1)
Space Complexity: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Sep, 2023
Like Article
Save Article