Skip to content
Related Articles

Related Articles

Program to convert Centimeters to Pixels
  • Last Updated : 08 Jan, 2021

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

566.93

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 :