FAQ: Working with Numbers - Review

This community-built FAQ covers the “Review” exercise from the lesson “Working with Numbers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Taking the example of calculating compound interest, how would you write something clean that would compound for X-amount of years? If it were able to calculate based on user input for X-years would be nice, but I can’t wrap my head around it at the moment. The code I have written so far is as follows:

double baseSavings = 2000.67;
Console.WriteLine (baseSavings);
double interestRate = 0.11;
Console.WriteLine (interestRate);
double firstYearInterest = (baseSavings * interestRate);
Console.WriteLine (firstYearInterest);
double firstYearTotal = (baseSavings + firstYearInterest);
Console.WriteLine (firstYearTotal);

1 Like

I’ve written a basic spending tracker program as follows:

// List of monthly outgoings
decimal outBroadband = 25;
decimal outContactLenses = 25;
decimal outPetrol = 50;
decimal outMortgage = 538;
decimal outCouncilTax = 112;
decimal outUtilities = 55;
decimal outPhones = 10 + 10;
decimal outWater = 42;
decimal outInsurance = 54 + 10 + 16;
decimal outFood = 200;

  decimal outTotal = (outBroadband + outContactLenses + outPetrol + outMortgage + outCouncilTax + outUtilities + outPhones + outWater + outInsurance + outFood);
  
  decimal monthlyPay = 1621;
  
  decimal NDI = (monthlyPay - outTotal);
  
  // Calculations
  Console.WriteLine($"Monthly pay = £{monthlyPay}");
  Console.WriteLine($"Total outgoings = £{outTotal}");
        
  Console.WriteLine($"Net monthly disposal income = £{NDI}");

Is there a way to program so it automatically adds all of the ‘decimal outXs’ together without having to list each individual one (i.e. ‘decimal outTotal = (outBroadband + outContactLenses, etc.’).

Thanks!

1 Like

Hi I am sort of stuck on this exercise.
I wrote a program that ask the user for there age and then tells the what age they are in dog years.

        int dogYears = 7;
        Console.Write("Please enter your age : ");
        int userAge = int.Parse(Console.ReadLine());

        Console.WriteLine("Your age in dog is " + userAge * dogYears);

I know my code works as I ran it in visual studio, but when I try runing it in Codecademy I get the fallowing errors.

Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: s
at System.Int32.Parse(String s)
at Review.Program.Main(String args) in /home/ccuser/workspace/csharp-working-with-numbers-review-working-with-numbers-csharp/Program.cs:line 21

can any one help?

Hi guys can some one look below and tell me why the netPay won’t show after (“My Pay Before tax is”

int netPay = 12500;
double vat = 0.20;

		double totalTax = netPay * vat;
  double grossPay = netPay - totalTax;
  Console.WriteLine("My Pay Before tax is", netPay);
  Console.WriteLine(totalTax);
  Console.WriteLine(grossPay);

Hello, @method5217209232. Welcome to the forum.

There are a couple of ways to include the value of your variable, netPay, in your output. I’m not sure which of these you may be more familiar with, but here are a few examples to consider:

string myString = "wild grapes";
//concatenation:
Console.WriteLine("I like bananas, coconuts and " + myString + "!");
//or composite formatting:
Console.WriteLine("I like bananas, coconuts and {0}!", myString);

Output:

I like bananas, coconuts and wild grapes!
I like bananas, coconuts and wild grapes!

The {0} acts as a placeholder for the variable. If you wanted to include values from multiple variables, you just add more placeholders:

string item1 = "bananas";
string item2 = "coconuts";
string item3 = "wild grapes";
Console.WriteLine("I like {0}, {1} and {2}!", item1, item2, item3);

Output:

I like bananas, coconuts and wild grapes!

There is also string interpolation:

string item1 = "bananas";
string item2 = "coconuts";
string item3 = "wild grapes";
Console.WriteLine($"I like {item1}, {item2} and {item3}!");

Output:

I like bananas, coconuts and wild grapes!

Happy coding!

2 Likes

instead of using “int.Parse()” try using “Convert.ToInt32()”

Here is my attempt at a compound interest calculator.

int startingMoney = 100;
double intrestPercentage = 0.05;
double currentMoney = startingMoney;

  for (int years = 1; years <= 10; years++){
    double interestEarned = currentMoney * intrestPercentage;
    currentMoney = currentMoney + interestEarned;
    currentMoney = Math.Round(currentMoney, 2);
    Console.WriteLine("Year: " + years);
    Console.WriteLine("Value: " + currentMoney);
1 Like

Hello everyone. Regarding the exercise about compound interest rate i found this formula at investopedia.
This is the code i came up with and it works on visual studio. I can enter the necessary details step by step as intended but in Codecademy doesn´t work and i can´t find anything wrong with it.

//Compound interest calculation
//Formula: CI = [P(1+i)^n]-P
//Where P is loan amount
//Where i is interest rate
//where n is duration of loan

        //Variables declaration
        
        int P = 0;
        double i = 0;
        int n = 0;
        double result = 0;

        //Formula
        Console.WriteLine("Formula for Compound interest Calculation:");
        Console.WriteLine();
        Console.WriteLine("CI = [P1+i)^n]-P");
        Console.WriteLine("Where P is loan amount");
        Console.WriteLine("Where i is interest rate");
        Console.WriteLine("Where n is duration of loan");


        //Asking user for loan details

        Console.WriteLine("Please insert your loan details below.");
        Console.WriteLine();
        Console.WriteLine("What is your loan amount?");
        P = P + Convert.ToInt32( Console.ReadLine());
        Console.WriteLine("What is your Interest Rate in percentage?");
        i = i + Convert.ToDouble(Console.ReadLine()) / 100;
        Console.WriteLine("What's the duration of your loan?");
        n = n + Convert.ToInt32(Console.ReadLine());

        //Calculate Compound Interest rate
        result = result + (P * (Math.Pow(i + 1, n) - 1));

        Console.WriteLine("Your Compound Interest rate will be " + Convert.ToDecimal(result));

Any ideas?

1 Like

Hi, I’m trying to to do the dog years thing, but I can’t get it to display the console to run the program. I went back the a previous lesson where you entered your favorite number and copied that code exactly to test it. However, it doesn’t display the $ symbol where I can type “dotnet run” to execute the program or enter input.

here is my code:

Console.Write ("Enter your age: ");
int myAge = Convert.ToInt32(Console.ReadLine());
int dogYears = (myAge / 7);
Console.WriteLine($"You are {dogYears} in dog years");

It runs, but it just spits out “Enter your age: You are 0 in dog years”. I never get the option to enter my age.

What gives?

1 Like

Note: A dog’s age goes at a different rate than a human’s age. A one year old human is equivalent to a fifteen year old dog. Then a two year old human is equivalent to a twenty-four year old dog. A three year old human is the same as a 29 year old dog. It then increases at a 1 : 5 ratio, meaning that when you are 4 a dog is 34, when you are 5 a dog is 39, so on and so fourth for the rest of its lifetime.

1 Like

Here, check out this code academy link to help you:https://www.codecademy.com/articles/command-line-interface

What is the error message saying?

Is the data type for i supposed to be a double? Or an int?

Also, you have an extra space in this line: P = P + Convert.ToInt32(Console.ReadLine());

That may not be the problem though. Since you already declared that P, I and n equal zero, you can’t just change the value of it.

I may be completely wrong though, so… Hope this kinda helped! :smile:

My code works but it’s not interactive like previous lessons. Does anyone know how to make the console interactive?

using System;

namespace Review
{
  class Program
  {
    static void Main(string[] args)
    {
      double actBal;
      double interest= 1.02;
      double years;
      double newActBal;

      Console.WriteLine("I'll calculate compound interest. Please enter a random number.");
      actBal=Convert.ToDouble(Console.ReadLine());
      Console.WriteLine("Your compound interest will be caclulated at a 2% growth per year. Please enter the amount of years you want to calculate.");
      years=Convert.ToDouble(Console.ReadLine());
      newActBal= actBal*Math.Pow(interest, years);
      Console.WriteLine("Your balance after "+years+" years is "+newActBal+".");


    }
  }
}

Hi

Completed project using Visual Studio

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Calculating compound interest  using the formula

                A = P(1 + r/n)**nt
                A = Final amount we will end with
                P = Principal Amount (Invested or Borrowed)
                r = Annual Interest Rate (expressed as decimal in my example)
                n = Number of times the rate is compounded each year
                t = Time, in years
            */
            Console.WriteLine("Enter your Initial Investment: ");
            double P = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter Interest Rate: ");
            double r = Convert.ToDouble(Console.ReadLine());
            double rp = r / 100;
            int n = 1;
            int t = 50;
            double nt = n * t;
            double ciPow = Math.Pow(1 + (rp / n), nt);
            double A = P * ciPow;

            Console.WriteLine("Compound Interest calculated with inital principal "+ P +" and " + r + "% will be: "+ A);
            Console.ReadLine();
        }
    }
}

Cheers

i have the same issue was this solved?

i have the same issue. was this resolved?

I edited these lines:

i = i + Convert.ToDouble(Console.ReadLine()); // Removed / 100

result = result + (P * (Math.Pow(i/100 + 1, n) - 1)); // Added /100

Now when i change the values it returns the correct answer.

hello Guys! I am new here so please help me out. Hope I’ll be in the position to help anybody in the near future!
My problem is this:
I tried to do the compound Interest exercise and wrote this code in order to have automatic results whenever someone is giving a loan amount and percentage…
There must be a mistake in my code .
Please check this out and you will understand What am I trying to do!
have in mind variables are loanAfterCI for the amount of loan after compound interest, loanBeforecI for loan balance, and cI stands for the percentage of compound interest(0.03 or 0.26…)

int loanBeforecI;
double cI;
double loanAfterCI = loanBeforecI*cI ;
Console.WriteLine(“What is the amount of Loan you want to take?”);
Console.Read(loanBeforecI);
Console.WriteLine(“What is the Compount interest you have?”);
Console.Read(cI);
Console.WriteLine("Your compount interest will be"loanAftercI);
}
}
}

My version of doing the compound interest calculator!

      int P = 10000; //this value can be changed 
      Console.WriteLine("User has a principal balance of: ${0}",P);
      
      double i = 0.05; //this value can be changed
      Console.WriteLine();
      Console.WriteLine("User interest rate is: {0}",i);
      
      int n  = 5; //this value can be changed
      Console.WriteLine();
      Console.WriteLine("Number of compounding periods: {0}",n);
      
      Console.WriteLine();
      
      Console.WriteLine("Calculating compound interest..............");
      
      Console.WriteLine(); 
      double result = P * Math.Pow(1+i,n) - P;
      Console.WriteLine("Your compound interest is: $"+ result);