Open In App

Program to convert Centimeters to Pixels

Last Updated : 29 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer centimeter, representing a length measured in centimetre, the task is to convert it into its equivalent value measured in pixels.

Examples:

Input: centimeter = 10
Output: pixels = 377.95

Input : centimeter = 5.5
Output : pixels = 207.87

Approach: The problem can be solved based on the followed mathematical relations:
 

1 inch = 96 px 
1 inch = 2.54 cm
Therefore, 2.54 cm = 96 px 
=> 1 cm = ( 96 / 2.54 ) px 
=> N cm = ( ( N * 96) / 2.54 ) px 
 

Below is the implementation of the above approach:
 

C++




// C++ program to convert centimeter to pixels
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert
// centimeters to pixels
void Conversion(double centi)
{
 
    double pixels = (96 * centi) / 2.54;
    cout << fixed << setprecision(2) << pixels;
}
 
// Driver Code
int main()
{
 
    double centi = 15;
    Conversion(centi);
    return 0;
}


Java




// Java program to convert
// centimeter to pixels
 
import java.io.*;
 
class GFG {
 
    // Function to convert
    // centimeters to pixels
    static double Conversion(double centi)
    {
        double pixels = (96 * centi) / 2.54;
        System.out.println(pixels);
        return 0;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int centi = 15;
        Conversion(centi);
    }
}


Python3




# Python program to convert
# centimeters to pixels
 
# Function to convert
# centimeters to pixels
def Conversion(centi):
 
    pixels = ( 96 * centi)/2.54
    print ( round(pixels, 2))
# Driver Code
centi = 15
Conversion(centi)


C#




// C# program to convert
// centimeter to pixels
using System;
 
class GFG {
 
    // Function to convert
    // centimeter to pixels
    static double Conversion(double centi)
    {
        double pixels = (96 * centi) / 2.54;
        Console.WriteLine(pixels);
        return 0;
    }
 
    // Driver Code
    public static void Main()
    {
        double centi = 15;
        Conversion(centi);
    }
}


PHP




<?php
 
// PHP program to convert
// centimeter to Pixels
 
// Function to convert
// centimeter to pixels 
function Conversion($centi)
{
    $pixels = (96 * $centi)/2.54;
    echo($pixels . "\n");
}
 
// Driver Code
$centi = 15;
Conversion($centi);
?>


Javascript




<script>
 
// JavaScript program to convert
// centimeter to pixels
function Conversion(centi)
    {
        let pixels = (96 * centi) / 2.54;
        document.write(pixels);
        return 0;
    }        
 
// Driver Code
        let centi = 15;
        Conversion(centi)
   
  // This code is contributed by souravghosh0416.
</script>


Output: 

566.93

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads