Learn how to find the percent of two numbers via coding in Python, JavaScript, Java, and C#. This guide makes things easy to understand by using simple examples. It’s designed to help you confidently handle percentage calculations in different situations. Whether you’re new to coding or have some experience, this tutorial is crafted to give you practical knowledge in a straightforward way. Join us on a journey to master the skill of percentage calculation in a fun and easy-to-grasp manner!
Find the Percent of Two Numbers Via Coding
Here are the coding examples to find the percent of two numbers via coding in different programming languages.
Method#1 Basic Percentage Formula
Explore the basic percentage formula, finding the ratio and converting it into a percentage.
Python:
def perc(part, whole):
return (part / whole) * 100
# Example:
g = 15
t = 30
gp = perc(g, t)
print(f"The girls make up {gp}% of the class.")
JavaScript:
function perc(part, whole) {
return (part / whole) * 100;
}
// Example:
let g = 15;
let t = 30;
let gp = perc(g, t);
console.log('The girls make up ${gp}% of the class.');
Java:
public class PercCalc {
public static double calcPerc(double part, double whole) {
return (part / whole) * 100;
}
// Example:
public static void main(String[] args) {
double g = 15;
double t = 30;
double gp = calcPerc(g, t);
System.out.println("The girls make up " + gp + "% of the class.");
}
}
C#:
using System;
class Program {
static double CalcPerc(double part, double whole) {
return (part / whole) * 100;
}
// Example:
static void Main() {
double g = 15;
double t = 30;
double gp = CalcPerc(g, t);
Console.WriteLine($"The girls make up {gp}% of the class.");
}
Method#2 Percentage Change
Imagine you’re keeping tabs on the stock market, and you want to know how much a particular stock has increased or decreased in value. That’s when percentage change steps in as your trusty guide. It’s like a magnifying glass for fluctuations, helping you make sense of the dynamics. Let’s understand how to find the percent of two numbers via coding in different languages.
Python:
def perc_change(old_val, new_val):
return ((new_val - old_val) / old_val) * 100
# Example:
o_p = 50
n_p = 75
p_change = perc_change(o_p, n_p)
print(f"The price has changed by {p_change}%.")
JavaScript:
function percChange(oldVal, newVal) {
return ((newVal - oldVal) / oldVal) * 100;
}
// Example:
let oP = 50;
let nP = 75;
let pChange = percChange(oP, nP);
console.log(`The price has changed by ${pChange}%.`);
Java:
public class PriceChangeCalc {
public static double calcPercChange(double oldVal, double newVal) {
return ((newVal - oldVal) / oldVal) * 100;
}
// Example:
public static void main(String[] args) {
double oP = 50;
double nP = 75;
double pChange = calcPercChange(oP, nP);
System.out.println("The price has changed by " + pChange + "%.");
}
}
C#:
using System;
class Program {
static double CalcPercChange(double oldVal, double newVal) {
return ((newVal - oldVal) / oldVal) * 100;
}
// Example:
static void Main() {
double oP = 50;
double nP = 75;
double pChange = CalcPercChange(oP, nP);
Console.WriteLine($"The price has changed by {pChange}%.");
}
Method#3 Finding the Percent of a Number
Explore finding a percentage of a specific number, useful for calculating discounts.
Python:
def perc_of_num(perc, num):
return (perc * num) / 100
# Example:
d_p = 20
o_p = 150
d_price = perc_of_num(d_p, o_p)
print(f"A {d_p}% discount results in ${d_price}.")
JavaScript:
function percOfNum(perc, num) {
return (perc * num) / 100;
}
// Example:
let dP = 20;
let oP = 150;
let dPrice = percOfNum(dP, oP);
console.log(`A ${dP}% discount results in $${dPrice}.`);
Java:
public class DiscCalc {
public static double calcDiscPrice(double perc, double num) {
return (perc * num) / 100;
}
// Example:
public static void main(String[] args) {
double dP = 20;
double oP = 150;
double dPrice = calcDiscPrice(dP, oP);
System.out.println("A " + dP + "% discount results in $" + dPrice + ".");
}
}
C#:
using System;
class Program {
static double CalcDiscPrice(double perc, double num) {
return (perc * num) / 100;
}
// Example:
static void Main() {
double dP = 20;
double oP = 150;
double dPrice = CalcDiscPrice(dP, oP);
Console.WriteLine($"A {dP}% discount results in ${dPrice}.");
}
Method#4 Proportions and Cross-Multiplication
Use proportions and cross-multiplication to find an unknown number in the context of percentages.
Python:
def unknown_num(known_part, known_whole, unknown_part):
return (unknown_part * known_whole) / known_part
# Example:
p_known = 20
w_known = 25
p_unknown = 25
u_whole = unknown_num(p_known, w_known, p_unknown)
print(f"The unknown whole is {u_whole}.")
JavaScript:
function unknownNum(knownPart, knownWhole, unknownPart) {
return (unknownPart * knownWhole) / knownPart;
}
// Example:
let pKnown = 20;
let wKnown = 25;
let pUnknown = 25;
let uWhole = unknownNum(pKnown, wKnown, pUnknown);
console.log(`The unknown whole is ${uWhole}.`);
Java:
public class PropCalc {
public static double findUnknownNum(double knownPart, double knownWhole, double unknownPart) {
return (unknownPart * knownWhole) / knownPart;
}
// Example:
public static void main(String[] args) {
double pKnown = 20;
double wKnown = 25;
double pUnknown = 25;
double uWhole = findUnknownNum(pKnown, wKnown, pUnknown);
System.out.println("The unknown whole is " + uWhole + ".");
}
}
C#:
using System;
class Program {
static double UnknownNum(double knownPart, double knownWhole, double unknownPart) {
return (unknownPart * known
Whole) / knownPart;
}
// Example:
static void Main() {
double pKnown = 20;
double wKnown = 25;
double pUnknown = 25;
double uWhole = UnknownNum(pKnown, wKnown, pUnknown);
Console.WriteLine($"The unknown whole is {uWhole}.");
}
Method#5 Multiplication and Division in Action
Use multiplication and division for a practical scenario: calculating a discounted price.
Python:
def disc_price(o_price, disc_perc):
return o_price - (disc_perc / 100 * o_price)
# Example:
o_price = 120
d_perc = 15
f_price = disc_price(o_price, d_perc)
print(f"The final discounted price is ${f_price}.")
JavaScript:
function finalDiscPrice(originalPrice, discountPerc) {
return originalPrice - (discountPerc / 100 * originalPrice);
}
// Example:
let originalPrice = 120;
let discountPerc = 15;
let finalPrice = finalDiscPrice(originalPrice, discountPerc);
console.log(`The final discounted price is $${finalPrice}.`);
Java:
public class FinalPriceCalc {
public static double calcFinalPrice(double oPrice, double discPerc) {
return oPrice - (discPerc / 100 * oPrice);
}
// Example:
public static void main(String[] args) {
double oPrice = 120;
double discPerc = 15;
double fPrice = calcFinalPrice(oPrice, discPerc);
System.out.println("The final discounted price is $" + fPrice + ".");
}
}
C#:
using System;
class Program {
static double FinalDiscPrice(double originalPrice, double discountPerc) {
return originalPrice - (discountPerc / 100 * originalPrice);
}
// Example:
static void Main() {
double originalPrice = 120;
double discountPerc = 15;
double finalPrice = FinalDiscPrice(originalPrice, discountPerc);
Console.WriteLine($"The final discounted price is ${finalPrice}.");
}
Conclusion – Percent of Two Numbers Via Coding
Becoming proficient in calculating percentages is vital for both everyday situations and coding tasks. Whether you’re working on discounts, price adjustments, or handling unknown values, these straightforward examples in Python, JavaScript, Java, and C# are designed to boost your confidence in dealing with percentage calculations. This guide aims to empower you with the skills needed to navigate the world of percentages in both real-life scenarios and coding projects. So, let’s dive in and build your confidence in mastering percentage calculations!
Happy coding!