Skip to content
Related Articles

Related Articles

Program to convert Pixels to rem and em
  • Last Updated : 21 Jan, 2021

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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); 
?> 

chevron_right


Output:

The value in em is 2.812500
The value in rem is 2.812500

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :