Concept of Floor operator in MATLAB & JAVA

Floor Operator:

In mathematics and computer science , the floor function is the function that takes as input a real number x and gives as output the greatest integer less than or equal to x.

If you want to know more on Floor operator , check the below link:



Example Program:

Write a program to accept 10 different decimal numbers in a single dimensional array . Store the integer part of the elements in one array & the fractional parts into other array.

Algorithm:

Step 1:

Take the inputs from the user.

Step 2:

Suppose you have 5.23 , then first you store the number in x , so now x=5.23.Now if you apply floor function in the number , you will get 5 & you store that in another variable , say y. So y=5. Now you subtract the 2 variables(x-y) & you will get the fractional part (5.23-5=0.23)& store the fractional part in another variable.

In this way , you can store the integer & fractional part in 2 different variables ..Simple right??😃

MATLAB CODE:

x=input('Enter the array:'); y=floor(x); z=x-y;

OUTPUT:



Explanation of the code:




JAVA CODE:

import java.util.Scanner; import java.lang.Math; class Fl { public static void main(String args[]) { Scanner obj=new Scanner(System.in); System.out.println("Enter the array length:"); int n=obj.nextInt(); double a[]=new double[n]; for(int i=0;i<n;i++) { System.out.println("Enter the number in the array:"); a[i]=obj.nextDouble(); } double b[]=new double[n]; double c[]=new double[n]; for(int i=0;i<n;i++) { b[i]=Math.floor(a[i]); } for(int i=0;i<n;i++) { c[i]=(a[i]-b[i]); } System.out.println("The integer part:"); for(int i=0;i<n;i++) { System.out.print(b[i]+" "); } System.out.println(); System.out.println("The fractional part:"); for(int i=0;i<n;i++) { System.out.print(c[i]+" "); } } }

OUTPUT:



Reference video:

Difference between ceil & round operator:

No comments