TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Find the Percent of Two Numbers in Python, Java, C#, Etc.
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
HowTo

How to Find the Percent of Two Numbers in Python, Java, C#, Etc.

Last updated: Feb 06, 2024 3:09 am
By Meenakshi Agarwal
Share
9 Min Read
Find the Percent of Two Numbers Via Coding
SHARE

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!

Contents
Method#1 Basic Percentage FormulaPython:JavaScript:Java:C#:Method#2 Percentage ChangePython:JavaScript:Java:C#:Method#3 Finding the Percent of a NumberPython:JavaScript:Java:C#:Method#4 Proportions and Cross-MultiplicationPython:JavaScript:Java:C#:Method#5 Multiplication and Division in ActionPython:JavaScript:Java:C#:

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!

You Might Also Like

How to Fix Accessibility Issues With Tables in WordPress

How to Use Python To Generate Test Cases for Java Classes

Sorting List of Lists in Python Explained With Examples

How to Fetch the List of Popular GitHub Repos

How Do I Install Pip in Python?

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article How to Find the Percent of Two Numbers How to Find the Percent of Two Numbers: A Short Tutorial
Next Article How to Find the Ratio of Two Numbers How to Find the Ratio of Two Numbers

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x