Operators In C#

Whenever we are going to perform any kind of operations with the variables or particular values then we require operators to perform different kinds of operations. Today, we are going to make a function that uses the different operators and returns the value after the particular operations.

Here, we have a function that has the three-argument base price of the product, tip price which is a tip given in the form of a percentage of the base price, and the tax amount which is again in the percentage of the base price of the product.

So, firstly we are going to define a function which is something like this,

public static void solve(double basePrice, int tipPer, int taxPer)
{
    //Function Body...
}

As you see on the above snippet, we have the function with the base price which is in double, tip percentage in integer, and tax percentage again in the integer. So, now firstly we are going to calculate the percentage of the Tip.

double tip = basePrice * tipPer / 100;

For the Tax Percentage again we have to count the Percentage of the Tax based on base Price same as above.

double tax = basePrice * taxPer/ 100;

Here, now we have the final tax and the tip price from which we have to calculate the product's final price. 

double finalPrice = basePrice + tip + tax;
//return Math.Round(finalPrice);
Console.WriteLine(Math.Round(finalPrice));

Here, as in the above snippet, you can check we have calculated the final price and if the return type of the function is not void then we have to return the value, but in our case, we are going to print the value by rounding up it to the nearest integer value.

The final code of the function is given as follows,

public static void solve(double basePrice, int tipPer, int taxPer)
{
     double tip = basePrice * tipPer / 100;
     double tax = basePrice * taxPer / 100;

     double finalPrice = basePrice + tip + tax;

     //return Math.Round(finalPrice);

     Console.WriteLine(Math.Round(finalPrice));
}

The complete solution for the HackerRank Day 2 Challenge of the 30daysofcode is given as follows,

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{
    /*
     * Complete the 'solve' function below.
     *
     * The function accepts following parameters:
     *  1. DOUBLE basePrice
     *  2. INTEGER tipPer
     *  3. INTEGER taxPer
     */
    public static void solve(double basePrice, int tipPer, int taxPer)
    {
        double tip = basePrice * tipPer / 100;
        double tax = basePrice * taxPer / 100;
        
        double finalPrice = basePrice + tip + tax;
        
        //return Math.Round(finalPrice);
        
        Console.WriteLine(Math.Round(finalPrice));
    }
}
class Solution
{
    public static void Main(string[] args)
    {
        double meal_cost = Convert.ToDouble(Console.ReadLine().Trim());
        int tip_percent = Convert.ToInt32(Console.ReadLine().Trim());
        int tax_percent = Convert.ToInt32(Console.ReadLine().Trim());
        Result.solve(meal_cost, tip_percent, tax_percent);
    }
}


Similar Articles