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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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:
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.’).
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
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:
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));
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.
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.
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+".");
}
}
}
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();
}
}
}
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);