Модуль двух чисел с плавающей запятой или двойных чисел

Опубликовано: 4 Декабря, 2021

Учитывая два числа с плавающей запятой, найдите остаток.

Примеры:

Input: a = 36.5, b = 5.0 
Output: 1.5

Input: a = 9.7, b = 2.3 
Output: 0.5 

Рекомендуется: сначала решите эту проблему на ПРАКТИКЕ, прежде чем переходить к решению.

Простое решение - повторить вычитание.

Выход :

 0,5

Мы можем использовать встроенную функцию fmod, чтобы найти модуль двух чисел с плавающей запятой.

C ++

// CPP program to find modulo of floating
// point numbers using library function.
#include <bits/stdc++.h>
using namespace std;
// Driver Function
int main()
{
double a = 9.7, b = 2.3;
cout << fmod (a, b);
return 0;
}

Джава

// JAVA program to find modulo of floating
// point numbers using library function.
import java.util.*;
class GFG{
// Driver Function
public static void main(String[] args)
{
double a = 9.7 , b = 2.3 ;
System.out.print((a % b));
}
}
// This code contributed by umadevi9616

Python3

# Python3 program to find modulo of floating
# point numbers using library function.
from math import fmod
# Driver code
if __name__ = = '__main__' :
a = 9.7
b = 2.3
print (fmod(a, b))
# This code is contributed by mohit kumar 29

C #

// C# program to find modulo of floating
// point numbers using library function.
using System;
class GFG{
static void Main()
{
double a = 9.7;
double b = 2.3;
Console.WriteLine(a % b);
}
}
// This code is contributed by mukesh07

PHP

<?php
// PHP program to find modulo of
// floating point numbers using
// library function.
// Driver Code
$a = 9.7; $b = 2.3;
echo fmod ( $a , $b );
// This code is contributed
// by inder_verma
?>

Javascript

<script>
// Javascript program to find modulo of
// floating point numbers using
// library function.
// Driver Code
let a = 9.7;
let b = 2.3;
document.write(a%b);
// This code is contributed by mohan pavan
</script>

Выход:

 0,5

Хотите узнать о лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для базового уровня плюс STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .