Program to convert Centimeters to Pixels
Last Updated :
29 Apr, 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++
#include <bits/stdc++.h>
using namespace std;
void Conversion( double centi)
{
double pixels = (96 * centi) / 2.54;
cout << fixed << setprecision(2) << pixels;
}
int main()
{
double centi = 15;
Conversion(centi);
return 0;
}
|
Java
import java.io.*;
class GFG {
static double Conversion( double centi)
{
double pixels = ( 96 * centi) / 2.54 ;
System.out.println(pixels);
return 0 ;
}
public static void main(String args[])
{
int centi = 15 ;
Conversion(centi);
}
}
|
Python3
def Conversion(centi):
pixels = ( 96 * centi) / 2.54
print ( round (pixels, 2 ))
centi = 15
Conversion(centi)
|
C#
using System;
class GFG {
static double Conversion( double centi)
{
double pixels = (96 * centi) / 2.54;
Console.WriteLine(pixels);
return 0;
}
public static void Main()
{
double centi = 15;
Conversion(centi);
}
}
|
PHP
<?php
function Conversion( $centi )
{
$pixels = (96 * $centi )/2.54;
echo ( $pixels . "\n" );
}
$centi = 15;
Conversion( $centi );
?>
|
Javascript
<script>
function Conversion(centi)
{
let pixels = (96 * centi) / 2.54;
document.write(pixels);
return 0;
}
let centi = 15;
Conversion(centi)
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...