Find the concentration of a solution using given Mass and Volume

17 Mar, 2021
Given two values M and V representing the mass of solute and volume of a solution, the task is to calculate the concentration of the solution.Examples:  
Input: M = 100.00, V = 500.00 Output: 200 Explaination: C = 1000 * (100 / 500) The concentration of solution is 200Input: M = 1.00 V = 1000.00 Output: 1  
 
Approach: The concentration of a solution is defined as the mass of solute in (gram) per liter of solution. Mathematically:  
C = 1000 * (M / V) Where M = Mass of solute and V = Volume of solution.
 Hence, in order to solve the problem, follow the steps below:  
Calculate the concentration of solution using the formula C = 1000* (M / V).Print the result.
Calculate the concentration of solution using the formula C = 1000* (M / V).
Print the result.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to find// concentration of a solution// using given Mass and Volume #include <bits/stdc++.h>using namespace std; // Function to calculate// concentration from the// given mass of solute and// volume of a solutiondouble get_concentration(double mass,                         double volume){    if (volume == 0)        return -1;    else        return (mass / volume)               * 1000;} // Driver Programint main(){    double mass, volume;    mass = 100.00;    volume = 500.00;    cout << get_concentration(mass,                              volume);    return 0;}
// Java program to find concentration// of a solution using given Mass// and Volumeclass GFG{ // Function to calculate// concentration from the// given mass of solute and// volume of a solutionstatic double get_concentration(double mass,                                double volume){    if (volume == 0)        return -1;    else        return (mass / volume) * 1000;} // Driver codepublic static void main(String[] args){    double mass, volume;    mass = 100.00;    volume = 500.00;         System.out.println(get_concentration(mass,                                         volume));}} // This code is contributed by Ritik Bansal
# Python3 program to find# concentration of a solution# using given Mass and Volume # Function to calculate# concentration from the# given mass of solute and# volume of a solutiondef get_concentration(mass, volume):         if (volume == 0):        return -1;    else:        return (mass / volume) * 1000; # Driver codemass = 100.00;volume = 500.00; print(get_concentration(mass, volume))     # This code is contributed by Pratima Pandey
// C# program to find concentration// of a solution using given Mass// and Volumeusing System;class GFG{ // Function to calculate// concentration from the// given mass of solute and// volume of a solutionstatic double get_concentration(double mass,                                double volume){    if (volume == 0)        return -1;    else        return (mass / volume) * 1000;} // Driver codepublic static void Main(){    double mass, volume;    mass = 100.00;    volume = 500.00;         Console.Write(get_concentration(mass,                                    volume));}} // This code is contributed by rock_cool
<script>// javascript program to find// concentration of a solution// using given Mass and Volume  // Function to calculate// concentration from the// given mass of solute and// volume of a solutionfunction get_concentration( mass,                          volume){    if (volume == 0)        return -1;    else        return (mass / volume)               * 1000;} // Driver Program    let mass, volume;    mass = 100.00;    volume = 500.00;     document.write(get_concentration(mass, volume));     // This code is contributed by todaysgaurav </script>
200
 
Time Complexity: O(1) Auxiliary Space: O(1) 
bansal_rtk_
rock_cool
dewantipandeydp
todaysgaurav
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.