Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 3: Simple Calculator with Memory and Recall

Lesson 4 of 14 in the free C# Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To implement a four-function calculator with Memory Store (MS) and Memory Recall (MR) operations, and to observe IEEE 754 double arithmetic and value-type copy semantics.

Theory

double is a 64-bit IEEE 754 binary floating-point value type: 1 sign bit, 11 exponent bits and 52 mantissa bits, giving roughly 15–17 significant decimal digits. Unlike integer arithmetic, floating-point division never throws: a / 0.0 yields Infinity and 0.0 / 0.0 yields NaN — a crucial contrast with int division, where dividing by zero raises DivideByZeroException at run time. The "memory" of a physical calculator maps to a variable whose lifetime outlives a single operation: the statement memory = add; COPIES the 64-bit value (value semantics — the two variables are thereafter fully independent; changing add later would not affect memory). This is exactly how the CLR treats all primitive numeric types: assignment copies bits, it never aliases storage. One more professional point: for money, decimal (a 128-bit base-10 type) is the correct choice, because binary floating point cannot represent 0.1 exactly — the classic rounding trap asked in every C# viva.

Requirements

  • .NET SDK 8.0+; commands dotnet new console and dotnet run; any editor or the RUN CODE button.

Procedure

  1. Scaffold with dotnet new console -n MemoryCalc, then cd MemoryCalc.
  2. Paste the program into Program.cs.
  3. Run dotnet run and compare each printed operation with a hand calculation.
  4. Experiment: change b to 0 and observe Infinity for the division; then retype both operands as int and observe the thrown DivideByZeroException instead.

Explanation of the Code

  • double a = 20, b = 5; declares the two operands; double memory = 0; is the calculator's M register, zero-initialised like a cleared memory.
  • double add = a + b; memory = add; — the sum is computed once, then value-copied into memory (Memory Store). add and memory are now two independent 64-bit slots in the stack frame.
  • The four Console.WriteLine($"...") lines print Add/Sub/Mul/Div using inline interpolation expressions such as {a - b} — interpolation holes may contain ANY expression, not just variable names; each is evaluated, converted with ToString, and spliced.
  • Console.WriteLine($"Memory Recall: {memory}") reads the stored value back (Memory Recall) — untouched by the operations printed after the store, which is the experimental proof that assignment copied rather than aliased.

Expected Output

A = 20, B = 5
Add: 25
Sub: 15
Mul: 100
Div: 4
Memory Recall: 25

🎯 Viva Questions

  1. Why does 20.0 / 0 print Infinity instead of crashing? — IEEE 754 defines ±Infinity and NaN for floating point; only integer division throws DivideByZeroException.
  2. What are value semantics? — Assignment copies the data itself; memory = add creates an independent copy, not a reference.
  3. When should decimal replace double? — For money and exact decimal fractions; double cannot represent 0.1 exactly in binary.
  4. How many significant digits does double hold? — About 15–17 decimal digits (52-bit mantissa plus the implicit leading bit).
  5. Can an interpolation hole contain an expression? — Yes, e.g. {a * b}; it is evaluated and formatted in place.
  6. Where do these local double variables live at run time? — In the method's stack frame (or in CPU registers after JIT optimisation), not on the GC heap.