The Wayback Machine - https://web.archive.org/web/20230419162301/https://www.geeksforgeeks.org/program-to-convert-pixels-to-rem-and-em/
Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Program to convert Pixels to rem and em

Improve Article
Save Article
Like Article
author
CoderSaty
proficient
56 published articles
Improve Article
Save Article
Like Article

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++




// C++ program to convert
// pixels to rem and em
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert
// pixels to rem and em
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;
}
 
// Driver Code
int main()
{
    // Input
    double PX = 45;
    Conversion(PX);
    return 0;
}

Java




// Java program to convert pixel to rem and em
 
import java.io.*;
 
class GFG {
 
    // Function to convert pixel to rem and em
    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;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        double pixels = 45;
        Conversion(pixels);
    }
}

Python




# Python program to convert pixel to rem and em
 
# Function to convert pixel to rem and em
 
 
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)
 
 
# Driver Code
pixel = 45
Conversion(pixel)

C#




// C# program to convert pixel to rem and em
using System;
 
class GFG {
 
    // Function to convert pixel to rem and em
    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;
    }
 
    // Driver Code
    public static void Main()
    {
        double pixel = 45;
        Conversion(pixel);
    }
}

PHP




<?php
 
// PHP program to convert pixel to rem and em
// Function to convert pixel to rem and em
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");
     
}
 
// Driver Code
$pixel = 45;
Conversion($pixel);
?>

Javascript




<script>
 
// Javascript program of the above approach
 
// Function to convert pixel to rem and em
    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;
    }
 
    // Driver Code
     
    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)


My Personal Notes arrow_drop_up
Last Updated : 04 May, 2021
Like Article
Save Article
Similar Reads
Related Tutorials