473,595 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple math program.

Would someone be kind and explain why and how this works. It comes up with the correct answer, but I dont understand how it does. Wouldn't Console.WriteLi ne("5+4={0}",5+ 4); wouldn't it come up as 5+4=1 instead of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for the help in advance!
using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;
//addition with integers works as expected
Console.WriteLi ne("5 + 4 = {0}", 5 + 4);
Console.Write(" Please press \"enter\" to continue");

Console.ReadLin e();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson
Dec 5 '05 #1
3 1421
Eric Anderson wrote:
Would someone be kind and explain why and how this works. It comes up with the
correct answer, but I dont understand how it does. Wouldn't
Console.WriteLi ne("5+4={0}",5+ 4); wouldn't it come up as 5+4=1 instead of 5+4=9?
Sorry guys im new to all of this but im learning. Thanks for the help in advance!


Why would it come up as 5+4=1?
If you wrote: Console.WriteLi ne ("5+4={0}", a); that would come up as
5+4=1, but you're passing it the right answer, so I don't see why you'd
expect it to print the wrong one...

Jon

Dec 5 '05 #2
The {0} is substituted for the appropriate argument (in this case 5+4).
The argument evaluates to 9 and is inserted into the string as position
{0}. Why did you expect it to be 1? Not having a go, just interested.

Simon

As a further example

Console.WriteLi ne("5 + 4 = {0} and 5 - 4 = {1}", 5+4, 5-4);

would result in

5 + 4 = 9 and 5 - 4 = 1

Eric Anderson wrote:
Would someone be kind and explain why and how this works. It comes up
with the correct answer, but I dont understand how it does. Wouldn't
Console.WriteLi ne("5+4={0}",5+ 4); wouldn't it come up as 5+4=1 instead
of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for
the help in advance!

using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;

//addition with integers works as expected

Console.WriteLi ne("5 + 4 = {0}", 5 + 4);

Console.Write(" Please press \"enter\" to continue");

Console.ReadLin e();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson

Dec 5 '05 #3
Lol I know that 5+4 equals to. The reason why I asked is just to understand what was going on in that line of code, because the book that I am reading didn't clarify what all that meant. And I just want to get a full understanding of everything before I move on to the next part. But thanks guys for clearing that up for me.

--
(\(\
(=':')
(,(")(") Eric Anderson
"Eric Anderson" <ti*********@em ail.uophx.edu> wrote in message news:eF******** ********@tk2msf tngp13.phx.gbl. ..
Would someone be kind and explain why and how this works. It comes up with the correct answer, but I dont understand how it does. Wouldn't Console.WriteLi ne("5+4={0}",5+ 4); wouldn't it come up as 5+4=1 instead of 5+4=9? Sorry guys im new to all of this but im learning. Thanks for the help in advance!
using System;

namespace SimpleMath

{

class DoMath

{

static void Main(string[] args)

{

int a = 1;
//addition with integers works as expected
Console.WriteLi ne("5 + 4 = {0}", 5 + 4);
Console.Write(" Please press \"enter\" to continue");

Console.ReadLin e();

} // end main

} // end class

} // end namespace
--
(\(\
(=':')
(,(")(") Eric Anderson
Dec 5 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3104
by: drs | last post by:
Hi, I am trying to find all lists of length x with elements a, b, and c. To this end, I have created the class below, but it is not quite working and I am having trouble figuring out what to change. Does anyone have any insight? class c: def __init__(self): self.traits = 4 # number of list elements self.types = 3 # elements can be 0, 1, or 2
0
2286
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** Invalid arguments to divll. at test_eng/Testout.pm line 30. ...propagated at t/polyser.t line 9. t/polyser.....dubious
1
2722
by: limelight | last post by:
I have discovered a math error in the .NET framework's Log function. It returns incorrect results for varying powers of 2 that depend on whether the program is run from within the IDE or from the command line. The amount by which the calculation is off is very small; even though the double data type holds the errant value, it seems to round off when printed (via ToString()) and shows the correct one. The problem is that the errant value is...
17
3604
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time you cycle through the nest of loops. Using a HD for storage to extend ram is much too slow for many...
32
5089
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 * 10^12) it's pretty efficient, it took 15 minutes to compute the first 1,000,000 primes.
7
2593
by: Wernfried Schwenkner | last post by:
I've found the discussion about Math.Log and the error with Math.Log(8,2) on Google. Unfortunatly the full thread isn't on my news server, so I can't reply. The problem doesn't only depend on representation of floating numbers. When You execute
10
1977
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox txtradius that I enter a radius into. I then have a lable that I want the surface area to be displayed in called lblsurface
5
13527
by: aguirre.adolfo | last post by:
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve problems using arithmetic, but when I try to upgrade the programs using math libraries nothing seems to work. I downloaded a 2002 tutorial from Zelle "An Introduction to Computer Science" where he uses a "import math" statement to calculate a square...
7
242
by: bvdp | last post by:
I'm finding my quest for a safe eval() quite frustrating :) Any comments on this: Just forget about getting python to do this and, instead, grab my set of values (from a user supplied text file) and call an external program like 'bc' to do the dirty work. I think that this would avoid someone from embedding os.system("rm ...") in what I thought would be a math expression and having it maybe do damage? Perhaps I'm getting too paranoid in...
1
2063
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
0
7955
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8251
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5839
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5418
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.