Given Marked Price
and Selling price as
of a product. The task is to calculate the Discount Percentage applied to that product.
Examples:
Input: M = 120, S = 100
Output: 16.66%
Input: M = 1000, S = 500
Output: 50%
The mathematical formula to calculate the Discount Percentage for the product is:
Discount = Marked Price - Selling price
Therefore,
Discount Percentage = (Discount / Marked Price) * 100
Below is the program to find the discount percentage for a product:
C++
#include <bits/stdc++.h>
using namespace std;
float discountPercentage( float S, float M)
{
float discount = M - S;
float disPercent = (discount / M) * 100;
return disPercent;
}
int main()
{
int M, S;
M = 120;
S = 100;
cout << std::fixed << std::setprecision(2)
<< discountPercentage(S, M) << "%" << endl;
M = 1000;
S = 500;
cout << std::fixed << std::setprecision(2)
<< discountPercentage(S, M) << "%" << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static float discountPercentage( float S, float M)
{
float discount = M - S;
float disPercent = (discount / M) * 100 ;
return disPercent;
}
public static void main(String args[])
{
int M, S;
M = 120 ;
S = 100 ;
System.out.printf( "%.2f" ,discountPercentage(S,M));
System.out.println( "%" );
M = 1000 ;
S = 500 ;
System.out.printf( "%.2f" ,discountPercentage(S,M));
System.out.println( "%" );
}
}
|
Python3
def discountPercentage(S, M):
discount = M - S
disPercent = (discount / M) * 100
return disPercent
if __name__ = = '__main__' :
M = 120
S = 100
print (discountPercentage(S, M), "%" )
M = 1000
S = 500
print (discountPercentage(S, M), "%" )
|
C#
using System;
class GFG
{
static float discountPercentage( float S,
float M)
{
float discount = M - S;
float disPercent = (discount / M) * 100;
return disPercent;
}
static public void Main ()
{
int M, S;
M = 120;
S = 100;
Console.Write(discountPercentage(S, M));
Console.WriteLine( "%" );
M = 1000;
S = 500;
Console.Write(discountPercentage(S, M));
Console.Write( "%" );
}
}
|
PHP
<?php
function discountPercentage( $S , $M )
{
$discount = $M - $S ;
$disPercent = ( $discount / $M ) * 100;
return $disPercent ;
}
$M ; $S ;
$M = 120;
$S = 100;
echo discountPercentage( $S , $M ), "%" , "\n" ;
$M = 1000;
$S = 500;
echo discountPercentage( $S , $M ), "%" , "\n" ;
?>
|
Javascript
<script>
function discountPercentage( S, M)
{
let discount = M - S;
let disPercent = (discount / M) * 100;
return disPercent;
}
let M, S;
M = 120;
S = 100;
document.write(discountPercentage(S,M).toFixed(2));
document.write( "%" );
document.write( "<br>" );
M = 1000;
S = 500;
document.write(discountPercentage(S,M).toFixed(2));
document.write( "%" );
</script>
|
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Approach#2: Using division formula
Algorithm
1. Divide the selling price (S) by the marked price (M) to get the fraction.
2. Subtract the fraction from 1 to get the discount fraction.
3. Multiply the discount fraction by 100 to get the discount percentage.
4. Return the discount percentage.
C++
#include <bits/stdc++.h>
using namespace std;
string discount_percentage( double M, double S) {
double fraction = S / M;
double discount_fraction = 1 - fraction;
double percentage = discount_fraction * 100;
stringstream stream;
stream << fixed << setprecision(2) << percentage;
return stream.str() + "%" ;
}
int main() {
double M = 120;
double S = 100;
cout << discount_percentage(M, S) << endl;
return 0;
}
|
Java
import java.text.DecimalFormat;
public class Main {
public static String discountPercentage( double M,
double S)
{
double fraction = S / M;
double discountFraction = 1 - fraction;
double percentage = discountFraction * 100 ;
DecimalFormat df = new DecimalFormat( "0.00" );
return df.format(percentage) + "%" ;
}
public static void main(String[] args)
{
double M = 120 ;
double S = 100 ;
System.out.println(discountPercentage(M, S));
}
}
|
Python3
def discount_percentage(M, S):
fraction = S / M
discount_fraction = 1 - fraction
percentage = discount_fraction * 100
return f "{percentage:.2f}%"
M = 120
S = 100
print (discount_percentage(M, S))
|
C#
using System;
class MainClass {
public static string DiscountPercentage( double M, double S) {
double fraction = S / M;
double discountFraction = 1 - fraction;
double percentage = discountFraction * 100;
return $ "{percentage:F2}%" ;
}
public static void Main( string [] args) {
double M = 120;
double S = 100;
Console.WriteLine(DiscountPercentage(M, S));
}
}
|
Javascript
function discount_percentage(M, S) {
let fraction = S / M;
let discount_fraction = 1 - fraction;
let percentage = discount_fraction * 100;
return percentage.toFixed(2) + "%" ;
}
let M = 120;
let S = 100;
console.log(discount_percentage(M, S));
|
Time Complexity: O(1)
Space Complexity: O(1)
Please Login to comment...