Program to convert Pixels to rem and em
Given an integer pixel representing the width of a box in pixels, the task is to convert pixel into their equivalent value measured in rem and em units.
Examples:
Input: pixel = 45
Output: em = 2.812500, rem = 2.812500
Input: pixel = 160
Output: em = 10, rem = 10
Approach: The problem can be solved based on the following mathematical relations:
For modern browsers,
- 1 rem = 16 px
Therefore, 1px = 0.0625 rem
- 1 em = 16 px
Therefore, 1 px = 0.0625 em
Follow the steps below to solve the problem:
- Calculate the equivalent value in rem and em of pixels by multiplying pixels by 0.0625.
- Print the equivalent value of pixels in rem and em unit.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Conversion( double pixels)
{
double rem = 0.0625 * pixels;
double em = 0.0625 * pixels;
cout << fixed << setprecision(6)
<< "The value in em is " << em << endl;
cout << fixed << setprecision(6)
<< "The value in rem is " << rem << endl;
}
int main()
{
double PX = 45;
Conversion(PX);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double Conversion( double pixel)
{
double rem = 0.0625 * pixel;
double em = 0.0625 * pixel;
System.out.println( "The value in rem is " + rem);
System.out.println( "The value in em is " + em);
return 0 ;
}
public static void main(String args[])
{
double pixels = 45 ;
Conversion(pixels);
}
}
|
Python
def Conversion(pixel):
rem = 0.0625 * pixel
em = 0.0625 * pixel
print "The value in em is" , round (em, 2 )
print "The value in rem is" , round (rem, 2 )
pixel = 45
Conversion(pixel)
|
C#
using System;
class GFG {
static double Conversion( double pixel)
{
double rem = 0.0625 * pixel;
double em = 0.0625 * pixel;
Console.WriteLine( "The value in em is " + em);
Console.WriteLine( "The value in rem is " + rem);
return 0;
}
public static void Main()
{
double pixel = 45;
Conversion(pixel);
}
}
|
PHP
<?php
function Conversion( $pixel )
{
$rem = 0.0625 * $pixel ;
$em = 0.0625 * $pixel ;
echo ( "The value in em is " . $em . "\n" );
echo ( "The value in rem is " . $rem . "\n" );
}
$pixel = 45;
Conversion( $pixel );
?>
|
Javascript
<script>
function Conversion(pixel)
{
let rem = 0.0625 * pixel;
let em = 0.0625 * pixel;
document.write(
"The value in rem is " + rem + "<br/>"
);
document.write(
"The value in em is " + em
);
return 0;
}
let pixels = 45;
Conversion(pixels);
</script>
|
Output:
The value in em is 2.812500
The value in rem is 2.812500
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...