Program to convert Pixels to rem and em
CoderSaty
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.812500Input: 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 emvoid 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 Codeint main(){ // Input double PX = 45; Conversion(PX); return 0;} |
Java
// Java program to convert pixel to rem and emimport 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 emdef 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 Codepixel = 45Conversion(pixel) |
C#
// C# program to convert pixel to rem and emusing 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 emfunction 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)



Please Login to comment...