Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int num01;
int num02;
int num03;

Console.Write("Type your first number: ");
num01 = Convert.ToDouble(Console.ReadLine());
Console.Write("Type a number to be added to: ");
num02 = Convert.ToDouble(Console.ReadLine());
Console.Write("Type a number to be multipied by: ");
num03 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("The result is " + num01 + num02 * num03);
Console.ReadKey();

What I have tried:

int result = (num01 + num02) * num03;
string strResult = "The result is " + result;

Console.WriteLine(strResult);
Console.ReadKey();

put the first formula in brackets.

Also used .ToInt32 instead of .ToDouble


Thanks to Jochen
Posted
Updated 22-Mar-16 2:01am
v3
Comments
Richard MacCutchan 22-Mar-16 6:57am    
What do you mean "the outcome isn't right"? What answer do you get, and what do you expect? Do you understand operator precedence?
Member 12409152 22-Mar-16 7:36am    
the numbers get added onto each other

num01 = 5
num02 = 7
num03 = 1

and the outcome i get is 571

You have to peform the mathematical operation in it's own statement and not inside the WriteLine() call where it is treated differently. So you might split the operations:
C#
// Perform the mathematical operation
int result = num01 + num02 * num03;
// Create the result string
string strResult = "The result is " + result;
// Print the result string
Console.WriteLine(strResult);


Also you are using Convert.ToDouble() to convert the input but assigning to int. So use Convert.ToInt32() or define the variables as double.
 
Share this answer
 
You should try
C#
Console.WriteLine("The result is " + (num01 + num02 * num03));

Quote:
searches on google, youtube but cant find any explantion about multiple operators.

The problem is that + don't do what you expect when there is a string.
You should study documentation of your language. This behavior may vary depending on language.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900