Jump to content
xisto Community
Sign in to follow this  
oval1405241520

Lesson #3 - Basic Math Operators And Random #s Lesson #3 - Basic Math Operators and Random #s

Recommended Posts

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.

 

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.

Share this post


Link to post
Share on other sites
What is the value of ?[/+-*]Lesson #3 - Basic Math Operators And Random #s

Your program will generate two random numbers of btw 1 and 12. It will then either divide, add, subtract or multiply these numbers together, again following a random pattern. The program user may see something along the of: 7 ? 3 = 21

What is the Value of ? [/+-*]

The user will type in the operate of their choice and the time required for the correct answer to be input will be displayed to the screen. Error and time taken to answer correctly will be reported to the user

Question 2

Add an array called TopTenScore into your program. It is to have ten(10) storage locations and will be used to store the values below. These values represent milliseconds taken to answer question posed by the program 000 1000 2000 3000 4000 5000 6000 7000 8000 9000 your program will compare a user to the array entries above and will replace the largest stored value that it is greater than I.E if user answer correctly in 5709 millisecond, their score will replace 5000 value in index position 5. The program will the inform the user which position they hold in the TopTenScore. If the score is higher or lower than any in the TopTenScore use an alternative suitable message. Offer the user the choice to try again or quit.

provide a menu for the program that offers either

1- test yourself against the clock

2- print out the top ten score to the screen

3- exit the program

 

-question by James

Share this post


Link to post
Share on other sites

Source Code of C++Lesson #3 - Basic Math Operators And Random #s

  •  Write C++ a number guessing game. Your program should store a random number from 0 to 9 and give the user a maximum of three chances to guess the number. The user will be given 5,3 or 1 points depending on whether he gets the correct answer on the first, second or third trial. The program must ask a total of five different questions and declare the result in the end.

  • Write a program that be used for calculating the GPA of student. The inputs of the program are the credit hours and letter grade for each course.
  • Help your little sister to learn her multiplication table. Your program should generate random multiplication questions and ask for an answer. The user should be given a maximum of three chances to get the correct answer. The correct answer should be given if the answer is not correct after the third trial.

-reply by Jhon

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.