Find the whole and fractional part of double number

Here given code implementation process.

// C program 
// Find the whole and fractional part of double number
#include <stdio.h>
#include <math.h>

// Separating the Fractional and whole part of given number
void separate(double number)
{

    printf("\n Given number : %lf ",number);

    // Separate decimal 
    double fractional = fmod(number , 1);

    // Separate Whole 
    double whole  = number - fractional;
  
    // Display calculated result
    printf("\n Whole : %lf",whole);
    
    printf("\n Fractional : %lf\n",fractional);
}


int main()
{
    // Test Case
    separate(-35.33);
    separate(78.343);
    separate(10.50);
    separate(0.50);
    separate(40.004);
    return 0;
}
// Run code like this
// (compile)
// gcc test.c -o a.out -lm 
// here test.c is source file
// (run)
// ./a.out

Output

 Given number : -35.330000
 Whole : -35.000000
 Fractional : -0.330000

 Given number : 78.343000
 Whole : 78.000000
 Fractional : 0.343000

 Given number : 10.500000
 Whole : 10.000000
 Fractional : 0.500000

 Given number : 0.500000
 Whole : 0.000000
 Fractional : 0.500000

 Given number : 40.004000
 Whole : 40.000000
 Fractional : 0.004000
/*
  Java Program
  Separate Whole and decimal in double number
*/
public class Separation
{
    
    // Separating the Fractional and whole part of given number
    public void separate(double number)
    {
        System.out.print("\n Given number : " + number );
        // Separate decimal 
        double fractional = number % 1;
        // Separate Whole 
        double whole = number - fractional;
        // Display calculated result
        System.out.print("\n Whole : " + whole );
        System.out.print("\n Fractional : " + fractional + "\n");
    }
    public static void main(String[] args)
    {
        Separation num = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);   
    }
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.3299999999999983

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.3430000000000035

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.003999999999997783
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
/*
  C++ Program
  Separate Whole and decimal in double number
*/
class Separation
{
    public:
        // Separating the Fractional and whole part of given number
        void separate(double number)
        {
            cout << "\n Given number : " << number;
            // Separate decimal
            double fractional = fmod(number , 1);
            // Separate Whole
            double whole = number - fractional;
            // Display calculated result
            cout << "\n Whole : " << whole;
            cout << "\n Fractional : " << fractional << "\n";
        }
};
int main()
{
    Separation num = Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
    return 0;
}

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.33

 Given number : 78.343
 Whole : 78
 Fractional : 0.343

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.004
// Include namespace system
using System;
/*
  C# Program
  Separate Whole and decimal in double number
*/
public class Separation
{
    // Separating the Fractional and whole part of given number
    public void separate(double number)
    {
        Console.Write("\n Given number : " + number);
        // Separate decimal
        double fractional = number % 1;
        // Separate Whole
        double whole = number - fractional;
        // Display calculated result
        Console.Write("\n Whole : " + whole);
        Console.Write("\n Fractional : " + fractional + "\n");
    }
    public static void Main(String[] args)
    {
        Separation num = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);
    }
}

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.329999999999998

 Given number : 78.343
 Whole : 78
 Fractional : 0.343000000000004

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.00399999999999778
<?php
/*
  Php Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    public  function separate($number)
    {
        echo "\n Given number : ". $number;
        // Separate decimal
        $fractional = fmod($number , 1);
        // Separate Whole
        $whole = $number - $fractional;
        // Display calculated result
        echo "\n Whole : ". $whole;
        echo "\n Fractional : ". $fractional ."\n";
    }
}

function main()
{
    $num = new Separation();
    // Test Case
    $num->separate(-35.33);
    $num->separate(78.343);
    $num->separate(10.50);
    $num->separate(0.50);
    $num->separate(40.004);
}
main();

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.33

 Given number : 78.343
 Whole : 78
 Fractional : 0.343

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.0039999999999978
/*
  Node Js Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    separate(number)
    {
        process.stdout.write("\n Given number : " + number);
        // Separate decimal
        var fractional = number % 1;
        // Separate Whole
        var whole = number - fractional;
        // Display calculated result
        process.stdout.write("\n Whole : " + whole);
        process.stdout.write("\n Fractional : " + fractional + "\n");
    }
}

function main()
{
    var num = new Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}
main();

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.3299999999999983

 Given number : 78.343
 Whole : 78
 Fractional : 0.3430000000000035

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.003999999999997783
#   Python 3 Program
#   Separate Whole and decimal in double number

class Separation :
    #  Separating the Fractional and whole part of given number
    def separate(self, number) :
        print("\n Given number : ", number, end = "")
        flag = False
        if(number < 0) :
            number =-number
            flag = True
        
        #  Separate decimal 
        fractional = number % 1
        #  Separate Whole 
        whole = number - fractional
        if(flag == True) :
            fractional = - fractional
            whole = - whole
        #  Display calculated result
        print("\n Whole : ", whole, end = "")
        print("\n Fractional : ", fractional )
    

def main() :
    num = Separation()
    #  Test Case
    num.separate(-35.33)
    num.separate(78.343)
    num.separate(10.50)
    num.separate(0.50)
    num.separate(40.004)

if __name__ == "__main__": main()

Output

 Given number :  -35.33
 Whole :  -35.0
 Fractional :  -0.3299999999999983

 Given number :  78.343
 Whole :  78.0
 Fractional :  0.3430000000000035

 Given number :  10.5
 Whole :  10.0
 Fractional :  0.5

 Given number :  0.5
 Whole :  0.0
 Fractional :  0.5

 Given number :  40.004
 Whole :  40.0
 Fractional :  0.003999999999997783
#   Ruby Program
#   Separate Whole and decimal in double number

class Separation 
    #  Separating the Fractional and whole part of given number
    def separate(number) 
        print("\n Given number : ", number)
        flag = false
        if(number<0)
            flag = true
            number = - number
        end
        #  Separate decimal 
        fractional = number % 1
        #  Separate Whole 
        whole = number - fractional

        if(flag)
            fractional = -fractional
            whole = -whole
        end
        #  Display calculated result
        print("\n Whole : ", whole)
        print("\n Fractional : ", fractional ,"\n")
    end

end

def main() 
    num = Separation.new()
    #  Test Case
    num.separate(-35.33)
    num.separate(78.343)
    num.separate(10.50)
    num.separate(0.50)
    num.separate(40.004)
end

main()

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.3299999999999983

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.3430000000000035

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.003999999999997783
/*
  Scala Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    def separate(number: Double): Unit = {
        print("\n Given number : " + number);
        // Separate decimal
        var fractional: Double = number % 1;
        // Separate Whole
        var whole: Double = number - fractional;
        // Display calculated result
        print("\n Whole : " + whole);
        print("\n Fractional : " + fractional + "\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var num: Separation = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);
    }
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.3299999999999983

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.3430000000000035

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.003999999999997783
/*
  Swift 4 Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    func separate(_ number: Double)
    {
        print("\n Given number : ", number, terminator: "");
        // Separate decimal
        let fractional: Double =  number.truncatingRemainder(dividingBy: 1);
        // Separate Whole
        let whole: Double = number - fractional;
        // Display calculated result
        print("\n Whole : ", whole, terminator: "");
        print("\n Fractional : ", fractional );
    }
}
func main()
{
    let num: Separation = Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}
main();

Output

 Given number :  -35.33
 Whole :  -35.0
 Fractional :  -0.329999999999998

 Given number :  78.343
 Whole :  78.0
 Fractional :  0.343000000000004

 Given number :  10.5
 Whole :  10.0
 Fractional :  0.5

 Given number :  0.5
 Whole :  0.0
 Fractional :  0.5

 Given number :  40.004
 Whole :  40.0
 Fractional :  0.00399999999999778
/*
  Kotlin Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    fun separate(number: Double): Unit
    {
        print("\n Given number : " + number);
        // Separate decimal
        var fractional: Double = number % 1;
        // Separate Whole
        var whole: Double = number - fractional;
        // Display calculated result
        print("\n Whole : " + whole);
        print("\n Fractional : " + fractional + "\n");
    }
}
fun main(args: Array<String>): Unit
{
    var num: Separation = Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.3299999999999983

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.3430000000000035

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.003999999999997783

Note that in a fraction result we are not perform round operation so some of programming language are not produce a valid actual result. In this case we can use below code.

/*
  Java Program
  Separate Whole and decimal in double number
*/
public class Separation
{
    
    // Separating the Fractional and whole part of given number
    public void separate(double number)
    {
        System.out.print("\n Given number : " + number );
        // Separate decimal 
        double fractional = number % 1;
        // Separate Whole 
        double whole = number - fractional;
        
        // valid for 5 digits precision
        fractional = Math.round(fractional * 100000d) / 100000d;
      
        // Display calculated result
        System.out.print("\n Whole : " + whole );
        System.out.print("\n Fractional : " + fractional + "\n");
    }
    public static void main(String[] args)
    {
        Separation num = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);   
    }
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.33

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.343

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.004
// Include namespace system
using System;
/*
  C# Program
  Separate Whole and decimal in double number
*/
public class Separation
{
    // Separating the Fractional and whole part of given number
    public void separate(double number)
    {
        Console.Write("\n Given number : " + number);
        // Separate decimal
        double fractional = number % 1;
        // Separate Whole
        double whole = number - fractional;
        // valid for 5 digits precision
        fractional = Math.Round(fractional * 100000d) / 100000d;
        // Display calculated result
        Console.Write("\n Whole : " + whole);
        Console.Write("\n Fractional : " + fractional + "\n");
    }
    public static void Main(String[] args)
    {
        Separation num = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);
    }
}

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.33

 Given number : 78.343
 Whole : 78
 Fractional : 0.343

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.004
<?php
/*
  Php Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    public  function separate($number)
    {
        echo "\n Given number : ". $number;
        // Separate decimal
        $fractional = fmod($number , 1);
        // Separate Whole
        $whole = $number - $fractional;
        // 6 digits precision
        $fractional = round($fractional,6);
        // Display calculated result
        echo "\n Whole : ". $whole;
        echo "\n Fractional : ". $fractional ."\n";
    }
}

function main()
{
    $num = new Separation();
    // Test Case
    $num->separate(-35.33);
    $num->separate(78.343);
    $num->separate(10.50);
    $num->separate(0.50);
    $num->separate(40.004);
}
main();

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.33

 Given number : 78.343
 Whole : 78
 Fractional : 0.343

 Given number : 10.5
 Whole : 10
 Fractional : 0.5

 Given number : 0.5
 Whole : 0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40
 Fractional : 0.004
/*
  Node Js Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    separate(number)
    {
        process.stdout.write("\n Given number : " + number);
        // Separate decimal
        var fractional = number % 1;
        // Separate Whole
        var whole = number - fractional;
        //5 digits precision
        fractional = fractional.toFixed(5)
        // Display calculated result
        process.stdout.write("\n Whole : " + whole);
        process.stdout.write("\n Fractional : " + fractional + "\n");
    }
}

function main()
{
    var num = new Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}
main();

Output

 Given number : -35.33
 Whole : -35
 Fractional : -0.33000

 Given number : 78.343
 Whole : 78
 Fractional : 0.34300

 Given number : 10.5
 Whole : 10
 Fractional : 0.50000

 Given number : 0.5
 Whole : 0
 Fractional : 0.50000

 Given number : 40.004
 Whole : 40
 Fractional : 0.00400
#   Python 3 Program
#   Separate Whole and decimal in double number

class Separation :
    #  Separating the Fractional and whole part of given number
    def separate(self, number) :
        print("\n Given number : ", number, end = "")
        flag = False
        if(number < 0) :
            number =-number
            flag = True
        
        #  Separate decimal 
        fractional = number % 1
        #  Separate Whole 
        whole = number - fractional
        fractional = round(fractional,5)
        if(flag == True) :
            fractional = - fractional
            whole = - whole
        #  Display calculated result
        print("\n Whole : ", whole, end = "")
        print("\n Fractional : ", fractional )
    

def main() :
    num = Separation()
    #  Test Case
    num.separate(-35.33)
    num.separate(78.343)
    num.separate(10.50)
    num.separate(0.50)
    num.separate(40.004)

if __name__ == "__main__": main()

Output

 Given number :  -35.33
 Whole :  -35.0
 Fractional :  -0.33

 Given number :  78.343
 Whole :  78.0
 Fractional :  0.343

 Given number :  10.5
 Whole :  10.0
 Fractional :  0.5

 Given number :  0.5
 Whole :  0.0
 Fractional :  0.5

 Given number :  40.004
 Whole :  40.0
 Fractional :  0.004
#   Ruby Program
#   Separate Whole and decimal in double number

class Separation 
    #  Separating the Fractional and whole part of given number
    def separate(number) 
        print("\n Given number : ", number)
        flag = false
        if(number<0)
            flag = true
            number = - number
        end
        #  Separate decimal 
        fractional = number % 1
        #  Separate Whole 
        whole = number - fractional

        if(flag)
            fractional = -fractional
            whole = -whole
        end

        fractional = (fractional*10000).round / 10000.0
        #  Display calculated result
        print("\n Whole : ", whole)
        print("\n Fractional : ", fractional ,"\n")
    end

end

def main() 
    num = Separation.new()
    #  Test Case
    num.separate(-35.33)
    num.separate(78.343)
    num.separate(10.50)
    num.separate(0.50)
    num.separate(40.004)
end

main()

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.33

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.343

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.004
/*
  Scala Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    def separate(number: Double): Unit = {
        print("\n Given number : " + number);
        // Separate decimal
        var fractional: Double = number % 1;
        // Separate Whole
        var whole: Double = number - fractional;
        // valid for 5 digits precision
        fractional = (Math.round(fractional * 100000d) / 100000d);
        // Display calculated result
        print("\n Whole : " + whole);
        print("\n Fractional : " + fractional + "\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var num: Separation = new Separation();
        // Test Case
        num.separate(-35.33);
        num.separate(78.343);
        num.separate(10.50);
        num.separate(0.50);
        num.separate(40.004);
    }
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.33

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.343

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.004
import Foundation
/*
  Swift 4 Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    func separate(_ number: Double)
    {
        print("\n Given number : ", number, terminator: "");
        // Separate decimal
        var fractional: Double = number.truncatingRemainder(dividingBy: 1);
        // Separate Whole
        let whole: Double = number - fractional;
        // valid for 5 digits precision
        fractional = round(fractional * 100000) / 100000.0;
        // Display calculated result
        print("\n Whole : ", whole, terminator: "");
        print("\n Fractional : ", fractional );
    }
}
func main()
{
    let num: Separation = Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}
main();

Output

 Given number :  -35.33
 Whole :  -35.0
 Fractional :  -0.33

 Given number :  78.343
 Whole :  78.0
 Fractional :  0.343

 Given number :  10.5
 Whole :  10.0
 Fractional :  0.5

 Given number :  0.5
 Whole :  0.0
 Fractional :  0.5

 Given number :  40.004
 Whole :  40.0
 Fractional :  0.004
/*
  Kotlin Program
  Separate Whole and decimal in double number
*/
class Separation
{
    // Separating the Fractional and whole part of given number
    fun separate(number: Double): Unit
    {
        print("\n Given number : " + number);
        // Separate decimal
        var fractional: Double = number % 1;
        // Separate Whole
        var whole: Double = number - fractional;
        //  5 digits precision
        fractional = Math.round(fractional * 100000) / 100000.0;
        // Display calculated result
        print("\n Whole : " + whole);
        print("\n Fractional : " + fractional + "\n");
    }
}
fun main(args: Array<String>): Unit
{
    var num: Separation = Separation();
    // Test Case
    num.separate(-35.33);
    num.separate(78.343);
    num.separate(10.50);
    num.separate(0.50);
    num.separate(40.004);
}

Output

 Given number : -35.33
 Whole : -35.0
 Fractional : -0.33

 Given number : 78.343
 Whole : 78.0
 Fractional : 0.343

 Given number : 10.5
 Whole : 10.0
 Fractional : 0.5

 Given number : 0.5
 Whole : 0.0
 Fractional : 0.5

 Given number : 40.004
 Whole : 40.0
 Fractional : 0.004


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved