FAQ: Working with Numbers - Arithmetic Operators

This community-built FAQ covers the “Arithmetic Operators” 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 Arithmetic Operators

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!

I just wanted to say that I made my own version of the lesson on codeacademy, thought I may as well show people what I did…

This is the entire code:

using System;

namespace PlanetCalculations
{
class Program
{
static void Main(string args)
{
Console.WriteLine(“Type your age into the code.”);
// Your Age
int userAge = 13;

  // Length of years on Jupiter (in Earth years)

double jupiterYears = 11.86;

  // Age on Jupiter

double jupiterAge = userAge/jupiterYears;

  // Time to Jupiter

double journeyToJupiter = 6.142466;

  // New Age on Earth

double newEarthAge = userAge + journeyToJupiter;

  // New Age on Jupiter

double newJupiterAge = newEarthAge/jupiterYears;

// Log calculations to console
Console.WriteLine("If you are {userAge},"); Console.WriteLine(“Then you are {jupiterAge} year/years old on Jupiter.”);
Console.WriteLine(“Since it takes 6.142466 years to travel to Jupiter,”);
Console.WriteLine("Then you would be {newEarthAge} by the time you reach Jupiter in Earth years."); Console.WriteLine(“In Jupiter years, you would be {newJupiterAge} years old by the time you reached Jupiter.”);
Console.WriteLine(“Cool Right?!”);
}
}
}

2 Likes

Note: You cannot copy this code. If you do, a series of errors occur. If you want to try my code, then it needs to be typed. Sorry!

Hey @fluffymuffinsareawes, the reason that the errors occur is… You need to use string interpolation, I don’t know much about this yet, but I know that string interpolation can be acheived by using the $ mark before a string.
An example:

Console.WriteLine($"You are {age} on Jupiter.")
2 Likes

Hi There

can someone please explain me this expression

If we do 1 %20 2 * 3,

should the answer be 9 or 7?
C# follows a set of rules to determine which operations to perform first
(the answer is 7).

Thanks

1 Like

What am I doing wrong??? I’m new to this FYI
// Log calculations to console
Console.Writeline(userAge);
Console.Writeline(newEarthAge);
Console.Writeline(userAge+journeyToJupiter);
Console.Writeline(double newJupiterAge=newEarthAge/jupiterYears);

I was wondering the same thing. It made sense when I changed ‘%20’ to ‘+’. Do Americans write ‘percent twenty’ when they want to say ‘plus’? I don’t think so.

You already made the calculation (variable) for each age.
in //Log calculation to console, you were asked to print the result to the console.
Not to calculate again. So the code will turn like this.

// Log calculations to console

  Console.WriteLine(userAge);

  Console.WriteLine(jupiterAge);

  Console.WriteLine(newEarthAge);

  Console.WriteLine(newJupiterAge);

image

The example code show Console.Write(answer)

Is that correct? Isn’t it suppose to be Console.WriteLine(answer) ?

1 Like

Also, you cannot define a variable inside the Console.WriteLine()
It will give you error.

Console.WriteLine(double newJupiterAge = newEarthAge/jupiterYears); :heavy_multiplication_x:

double newJupiterAge = newEarthAge/jupiterYears; 〇
Console.WriteLine(newJupiterAge);

1 Like

Hi guys,

I cannot get past this point. It prints my variables, but I get a red x and not a green tick. Can anybody help me with some fresh eyes?
Thanks :slight_smile:

(code is down below)

// Your Age

int userAge = 73;

  // Length of years on Jupiter (in Earth years)

double jupiterYears = 11.86;

  // Age on Jupiter

double jupiterAge = userAge/jupiterYears;

  // Time to Jupiter

double journeyToJupiter = 6.142466;

  // New Age on Earth

double newEarthAge = userAge+journeyToJupiter;

  // New Age on Jupiter

double newJupiterAge = newEarthAge/jupiterYears;

  // Log calculations to console

Console.WriteLine($“My Age on Earth: {userAge} !”);

Console.WriteLine($“My Age on Jupiter: {jupiterAge}”);

Console.WriteLine($“My Earth age after journey: {newEarthAge}”);

Console.WriteLine($“My age on Jupiter on Jupiter: {newJupiterAge}”);

does anybody know why we are now adding here I get that integer and integer is integer and integer and double is double but why are we adding
Screenshot 2021-07-25 at 16.26.47

I think its because its adding ur age u are as ur leaving earth to the age u are when ur on Jupiter if any of you are wondering it was very simple actually.

“Console.WriteLine” puts the answer on a new line by itself. This is good to use if you have many different integers that you want on separate lines rather than running them all together on one line.

“Console.Write” prints the output on a single line. If you have four different integers and you print them all using Console.Write, they will all run together on a single line.

  1. in the end part (No.6 - logging your answers) seeing as your variable answers are in {} brackets you need to utilise string interpolation. You could just concatinate them like "some text " + yourVariable

  2. The issue with this section is the system seems to only allow the below as as acceptable answer before allowing you to go to Next section (which is silly to me… seems like misinformation in the question). you need to add below code it will work.

// Log calculations to console

  Console.WriteLine(jupiterAge);

  Console.WriteLine(newEarthAge);

  Console.WriteLine(newJupiterAge);

Hi,

In task number five, I tried solving it using the code:

"
// New Age on Jupiter
double newJupiterAge = jupiterAge + (journeyToJupiter / jupiterYears);
"

This gives an error. As the editor wants you to do

"
// New Age on Jupiter
double newJupiterAge = newEarthAge / jupiterYears;
"
even though the values are the same. My logic was, even though the code was longer, it would work regardless of whether or not newEarthAge was known and defined. I chose to base it on the base defined values to save time when having to reformat the code, if you did not want to store newEarthAge anymore, you would have to rewrite newJupiterAge as well. Shouldn’t my solution be valid as well and the other given as an alternative solution to the problem?

Thank you.

When logging calculations to the console, we can also cast to ensure that the ages are an integer.

  // Log calculations to console

  Console.WriteLine((int)jupiterAge);
  Console.WriteLine((int)newEarthAge);
  Console.WriteLine((int)newJupiterAge);

Thought about this problem: Why don’t we cast the newAge types back into integers? This lesson is directly after the casting lesson and I feel these values are appropriately represented as an int.