oval
May 3 2006, 08:34 PM
In this lesson I will cover how to use basic math operations, well addition really but you can also use subtraction[-], division[/] and multiplication[*]. I would also like to note that when doing division, if variabes of int type are used you will not get any remainders (i.e. n.345984). To be able to work with real numbers, numbers with decimals, you can use float. Below is a way to declare a float and assign it a value: float floatNumber; float Number = 28.47f; Aside from generating random numbers and basic math operations, we will also incorporate an if statement and convert the user output to an integer. If you recall from the previous lesson, when a user enters an input, it is in string format. In order to perform math calculations correctly, we need to convert it to a numeric value (such as an integer in this instance). Let's take a look at the code below. Be sure to look at the comments if something is unclear. CODE
using System;
namespace lessonThree { /// <summary> /// In this lesson we will cover some basic math /// operations, look at an if statement and a few other /// useful things that should be understood when /// programming in C# /// </summary> class thirdLesson { /// <summary> /// Ask an age. /// If under 18 -> Exit Program /// Give lucky number for the day /// Predict when person will get rich. /// </summary> [STAThread] static void Main(string[] args) { int age; //Random object createRand Random createRand = new Random(); int luckyNumber;
Console.Write("Enter your age: "); //grab the input from user and convert it to //an integer, hence the 'Convert.ToInt32(); age = Convert.ToInt32(Console.ReadLine());
//skip line Console.WriteLine(); if (age > 17) { //Random generated numbers range from 0 to 1, //therefore we multiply by ten (10) below //The .NextDouble() method helps retrieve the value luckyNumber = (int)(createRand.NextDouble()*10); Console.WriteLine("Your Lucky Number is: {0}", luckyNumber); //Here you use the value of luckyNumber and add it to his age Console.WriteLine("You are {0} years old and will be rich at age: {1}", age, age+luckyNumber); //As you can see the zero in braces {0} holds the variable age //and the one in braces {1} holds the sum of the variables "age and luckyNumber" age+luckyNumber
}//If the age is not 18 or above we conclude by saying Console.WriteLine("Thanks and Have a Great Day"); Console.WriteLine(); Console.WriteLine("Press [ENTER] to Exit"); Console.ReadLine();
}//end Main() }//end class }//end namespace
As always, if you are testing this console application on Visual Studio or developing your own, stepping through the application with F11 is very useful.
Reply
Similar Topics
Keywords : lesson, 3, basic, math, operators, random, s, lesson, 3, basic, math, operators, random, s
- C# Tutorial : Lesson 9 - Constructors & Destructors
(0)
C# Tutorial : Lesson 8 - Functions/methods
(0) The programming model has gone through many refinements, each change improving upon the model. One
of the early changes was the inclusion of Subroutines or Subprograms. As the names suggest, these
are fragments of code within a program. They offer the following advantages:- Reduction of code
duplication by offering re-usability Simplifying program logic by decomposing the program into
smaller blocks Improving readability of the program Why do we need functions?
Consider the following program:- CODE using System; namespace ConsoleApplication1 { ....
C# Tutorial : Lesson 7 - Creating Value Types & Reference Types - Part II
(1) foreach statement This statement is explicitly used to traverse through arrays. The
benefit of using foreach over the normal for statement is that it is not needed to check the
size of the array while using the former. Syntax:- foreach(type identifier in expression) {
statement 1; statement 2; .... } Suppose we have an array StudentNames containing the name of all
the students in a class. We need to display the name of each one of them on screen. First we will
see how it can be done using the for loop. CODE string[] StudentNames = new s....
C# Tutorial : Lesson 6 - Creating Value Types & Reference Types - Part I
(0) Value Types & Reference Types In C#, Variables are either value types or reference types
depending on what they store, values or references. By reference we imply that the variable holds
the memory address of another location where the actual value is stored. All built in data types -
Int, Char, String, Float, Boolean, etc are value types while classes are reference types. The
following is a diagrammatic representation of these two types. In this picture there are two
variables Num1 and Num2. Both of these being integer variables store the values directly.....
C# Tutorial : Lesson 5 - Encapsulation & Abstraction
(0) Encapsulation & Abstraction Two concepts that go together in the object oriented approach
are Encapsulation & Abstraction. Abstraction is the representation of only the essential features of
an object, while Encapsulation is the hiding of the non-essential features. Think of a person
driving a car. He does not need to know the internal working of the engine or the way gear changes
work, to be able to drive the car (Encapsulation). Instead, he needs to know things such as how much
turning the steering wheel needs, etc (Abstraction). Consider the example from ....
C# Tutorial : Lesson 4 - Object Oriented Programming
(2) Object Oriented Programming Object Oriented Programming is a methodology modeled on real
life. It comprises of the basic unit, an object, being used in implementing a program. Think of all
the objects around you - cars, buses, birds, trees, etc. You will find countless ones of them,
everywhere you go. In order to tackle this huge number, we started classifying them, thus came the
term Class. Class A class is a composition of the theoretic characteristics (attributes and
properties) of an object and the things it can do - behaviors, methods and features. Tak....
C# Tutorial : Lesson 3 - Programming Constructs
(1) Conditional Branching By branching we imply, having different paths for execution.
Conditions are used to determine which statements need to be executed. Suppose, you have a program
to store the details of employees. Depending upon the post of the employee, there would be various
fields associated with it. A department head, for example, would have a property denoting the
department he heads, etc. We use conditional branching in such a scenario. C# provides the
following conditional constructs:- if .. else Syntax:- if ( ) { statements } else { statement....
C# Tutorial : Lesson 2 - Variables & Operators
(0) Variables A Variable is a named location that stores a value. Although variables are
physically stored in the RAM, their actual memory address is not known to the programmer. We can use
the variables via the name we give them. These are the rules for naming a variable:- The name must
begin with a letter or an underscore & can be followed by a sequence of letters (A-Z), digits (0-9)
or underscore (_) Special Characters such as ? - + * / \ ! @ # $ % ^ ( ) { } , ; :
. cannot be used in the variable name. A variable name must not be the same a....
C# Tutorial : Lesson 1 - Hello World Program
(0) This is the first installment of my venture to explain the bits and pieces of C# that I have managed
to learn at NIIT. In this lesson we would be creating the Hello World application. STEP 1 : Create
a New Console Application in Visual C# The IDE automatically adds the following code:- CODE
using System; using System.Collections.Generic; using System.Text; namespace HelloWorld {
class Program { static void Main(string[] args) { }
} } Lets take a look at the first 3 lines. All of these lines begin with t....
Lesson #2 - Switch Statement In C#
Using the Switch Statement in C# (0) In the previous lesson we demonstrated did a basic input output application which asked your your
name and replied with a general greeting using the name you entered. In this lesson we are going
to learn about a conditional statement called the switch statement. Usually if and if else
statements are taught before switch statements. Although these are very useful, I find the switch
to be easier and more fun to use. The program written below is very similar to the previous
program where the user is asked to enter their name, however now the program will loo....
Lesson #1 - Introduction To C#
Console Application (0) This tutorial will help get you started with C# Sharp. These tutorials will assume that you are
using the Visual Studio environment. If you are just curious to test out some code, then you can
copy and paste this code into your CS file, however, before doing so, make sure to name your project
the same as the namespace on the code. But since this first program is fairly easy, it will not
hurt to type it out. It is fairly easy and short. Also, this program is highly commented so it
should be easy to follow along. Comments will have 2 forward slashes before them '....
Looking for lesson, 3, basic, math, operators, random, s, lesson, 3, basic, math, operators, random, s
|
|
Searching Video's for lesson, 3, basic, math, operators, random, s, lesson, 3, basic, math, operators, random, s
|
advertisement
|
|