


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note from the cs112 introduction to programming course at yale university. It covers the topics of arithmetic and assignment operations. The note explains the concept of arithmetic expressions, arithmetic operators, and their precedence. It also discusses the assignment operators and increment/decrement operators. The note provides examples and code snippets to help students understand the concepts.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1 // Arithmetic.cs 2 // An arithmetic program. 3 4 using System; 5 6 class Arithmetic 7 { 8 static void Main( string[] args ) 9 { 10 string firstNumber, // first string entered by user 11 secondNumber; // second string entered by user 12 13 int number1, // first number to add 14 number2; // second number to add 15 16 // prompt for and read first number from user as string 17 Console.Write( "Please enter the first integer: " ); 18 firstNumber = Console.ReadLine(); 19 20 // read second number from user as string 21 Console.Write( "\nPlease enter the second integer: " ); 22 secondNumber = Console.ReadLine(); 23 24 // convert numbers from type string to type int 25 number1 = Int32.Parse( firstNumber ); 26 number2 = Int32.Parse( secondNumber ); 27 28 // do operations 29 int sum = number1 + number2; 30 int diff = number1 - number2; 31 int mul = number1 * number2; 32 int div = number1 / number2; 33 int mod = number1 % number2;
32 // display results 33 Console.WriteLine( "\n{0} + {1} = {2}.", number1, number2, sum ); 34 Console.WriteLine( "\n{0} – {1} = {2}.", number1, number2, diff ); 35 Console.WriteLine( "\n{0} * {1} = {2}.", number1, number2, mul ); 36 Console.WriteLine( "\n{0} / {2} = {2}.", number1, number2, div ); 37 Console.WriteLine( "\n{0} % {1} = {2}.", number1, number2, mod ); 34 35 } // end method Main 36 37 } // end class Arithmetic