🧪
Lab 2 — Active Formula & Operator Logic
Lab 2: Intro 🏠 Home
PSUC Lab 2 — Formula & Arithmetic Expressions

Active Logic & Mathematical C Programming

In Lab 2, we bridge real-world math formulas into C code expressions. Master integer division truncation, modulus remainder operator, floating point division, and <math.h> functions!

Essential C Formula Rules

Operator / FunctionC SyntaxKey Rule & Trap
Integer Divisiona / bTruncates decimal! 7 / 2 = 3 (Not 3.5)
Modulus (Remainder)a % bOnly works on integers! 7 % 2 = 1
Float Division5.0 / 9.0Writing 5 / 9 in C yields 0! Use floats.
Square Rootsqrt(x)Header: #include <math.h>
Power Functionpow(base, exp)Header: #include <math.h>

🎯 Learning Philosophy: Active Logic Building

Instead of copy-pasting code, each experiment in Lab 2 features 3 Interactive Learning Modes:

  1. 🧠 Tab 1 (Logic & Hints): Understand the math formula & step-by-step pseudo-code.
  2. đŸ§© Tab 2 (Logic Puzzle): Fill in missing operators/variables & run live simulations.
  3. đŸ’» Tab 3 (C Solution): Verify your answer against verified C source code.
Use the mode tabs on each slide to test your logic before looking at full code!
Program 1 of 6

To Convert Seconds into Hours, Minutes & Seconds

Given total seconds S, extract whole hours using S / 3600, remaining minutes using (S % 3600) / 60, and leftover seconds using S % 60.

Mathematical Formula Breakdown

  • 1 Hour = 3600 seconds
  • 1 Minute = 60 seconds
  • Hours: H = total_sec / 3600 (Integer division truncates decimal)
  • Remaining Sec after Hours: rem = total_sec % 3600
  • Minutes: M = rem / 60
  • Leftover Seconds: S = rem % 60

Interactive Logic Hints

💡 Hint 1: Why do we use integer division / for Hours? â–Œ
In C, 3665 / 3600 gives 1 (whole hour). It automatically drops the .018 decimal part!
💡 Hint 2: How does % (modulus) help pass remaining time? â–Œ
3665 % 3600 gives 65 (the seconds left over after removing 1 full hour). Then 65 / 60 = 1 min and 65 % 60 = 5 sec.

đŸ§© Complete the Missing Formula Logic

Fill in the missing C operators/constants below and test your logic:

h = total_sec 3600;
rem = total_sec 3600;
m = rem / ;
s = rem 60;
Select operators above and click Check!

⏱ Live Time Converter Simulator

Result will appear here...
seconds_converter.cReference C Code
#include <stdio.h> int main() { int total_sec, hours, minutes, seconds, rem; printf("Enter total seconds: "); scanf("%d", &total_sec); hours = total_sec / 3600; // 1 hour = 3600 seconds rem = total_sec % 3600; // Remaining seconds minutes = rem / 60; // 1 minute = 60 seconds seconds = rem % 60; // Leftover seconds printf("%d sec = %d Hours, %d Mins, %d Secs\n", total_sec, hours, minutes, seconds); return 0; }
Program 2 of 6

To Find the Sum of Digits of a Number

Extract digits from right to left: digit = n % 10 isolates the last digit, and n = n / 10 drops it.

Digit Peel Algorithm (Base 10)

  • Extract Last Digit: 1234 % 10 = 4
  • Accumulate: sum = sum + digit (0 + 4 = 4)
  • Remove Last Digit: 1234 / 10 = 123 (Integer truncation drops .4)
  • Repeat loop until n == 0!

Interactive Logic Hints

💡 Hint 1: Why does % 10 always give the last digit? â–Œ
In base 10 decimal system, remainder after dividing by 10 is always the ones-place digit (e.g. 589 % 10 = 9).
💡 Hint 2: What happens when n becomes single digit (e.g. 5)? â–Œ
5 % 10 = 5 (extracts 5), then 5 / 10 = 0. Loop condition n > 0 becomes false and loop stops cleanly!

đŸ§© Complete the Digit Extraction Code

while (temp > 0) {
  digit = temp ;
  sum = sum digit;
  temp = temp 10;
}
Select operators above and click Check!

🔱 Visual Step-by-Step Digit Trace

Trace will show here...
digit_sum.cReference C Code
#include <stdio.h> int main() { int num, temp, sum = 0, digit; printf("Enter a number: "); scanf("%d", &num); temp = num; while (temp > 0) { digit = temp % 10; // Extract last digit sum += digit; // Add to running sum temp = temp / 10; // Drop last digit } printf("Sum of digits of %d = %d\n", num, sum); return 0; }
Program 3 of 6

Temperature Converter (°F ↔ °C)

Beware of integer division trap! Writing 5 / 9 in C evaluates to 0. You must use floating-point literal 5.0 / 9.0.

Conversion Formulas

  • Fahrenheit to Celsius: C = (F - 32) * 5.0 / 9.0
  • Celsius to Fahrenheit: F = (C * 9.0 / 5.0) + 32
  • Format Specifier for 2 decimals: %.2f

Interactive Logic Hints

💡 Hint 1: What is the classic C integer division trap here? â–Œ
If you write (F - 32) * (5 / 9), C evaluates integer division 5 / 9 = 0, making Celsius result ALWAYS 0.00!
💡 Hint 2: How to fix integer division? â–Œ
Write 5.0 / 9.0 (with decimal point). This forces C to perform floating-point division (0.5556).

đŸ§© Complete the Temperature Calculation

float F = 98.6, C;
// Choose correct float constants:
C = (F - 32.0) * ;
Select formula above and click Check!

đŸŒĄïž Dual Temp Simulator

Result live update...
temp_convert.cReference C Code
#include <stdio.h> int main() { float fahrenheit, celsius; printf("Enter temp in Fahrenheit: "); scanf("%f", &fahrenheit); // Note: Use 5.0 / 9.0 for float division celsius = (fahrenheit - 32.0) * 5.0 / 9.0; printf("%.2f°F = %.2f°C\n", fahrenheit, celsius); return 0; }
Program 4 of 6

Distance Between Two Points (x1, y1) & (x2, y2)

Uses Euclidean distance formula d = √((x2 - x1)2 + (y2 - y1)2) with <math.h> functions sqrt() and pow().

Euclidean Distance Formula

  • Header required: #include <math.h>
  • Square Root: sqrt(x)
  • Power: pow(x, 2) computes x2
  • Expression: dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))

Interactive Logic Hints

💡 Hint 1: Can we write (x2 - x1)*(x2 - x1) instead of pow()? â–Œ
Yes! (x2 - x1) * (x2 - x1) is faster than pow() because it avoids library function call overhead.
💡 Hint 2: What data type should distance be? â–Œ
Use double (with %lf in scanf) for highest precision when calculating square roots!

đŸ§© Complete Distance Calculation Code

#include <stdio.h>
#include <>

dist = ((x2 - x1, 2) + pow(y2 - y1, 2));
Fill in functions above and click Check!

📍 Coordinate Distance Calculator

Distance = 5.0000
point_distance.cReference C Code
#include <stdio.h> #include <math.h> // Required for sqrt() & pow() int main() { double x1, y1, x2, y2, distance; printf("Enter x1 y1: "); scanf("%lf %lf", &x1, &y1); printf("Enter x2 y2: "); scanf("%lf %lf", &x2, &y2); distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); printf("Distance = %.4lf\n", distance); return 0; }
Program 5 of 6

Area & Circumference of a Circle

Compute Area = πr2 and Circumference = 2πr using constant macro #define PI 3.14159.

Circle Formulas

  • Macro Constant: #define PI 3.14159265
  • Area: area = PI * r * r
  • Circumference: circumference = 2 * PI * r

Interactive Logic Hints

💡 Hint 1: What does #define macro do? â–Œ
Preprocessor replaces every occurrence of PI in your code with 3.14159265 before compilation!

đŸ§© Complete the Circle Geometry Equations

#define PI 3.14159
float r = 7.0, area, circum;
area = PI * ;
circum = * PI * r;
Select formulas above and click Check!

⭕ Circle Geometry Calculator

Result live update...
circle_calc.cReference C Code
#include <stdio.h> #define PI 3.14159265359 int main() { float radius, area, circumference; printf("Enter radius of circle: "); scanf("%f", &radius); area = PI * radius * radius; circumference = 2 * PI * radius; printf("Area = %.2f sq. units\n", area); printf("Circumference = %.2f units\n", circumference); return 0; }
Program 6 of 6

Swap Two Variables (With & Without 3rd Variable)

Compare standard variable swap using temp vs arithmetic swap without third variable.

Swapping Methods

  • Method 1 (Temp Var): temp = a; a = b; b = temp;
  • Method 2 (Arithmetic): a = a + b; b = a - b; a = a - b;

Interactive Logic Hints

💡 Hint 1: How does arithmetic swapping work? â–Œ
If a=10, b=20: a = 30. Then b = 30 - 20 = 10. Then a = 30 - 10 = 20. Result: swapped!

đŸ§© Complete Arithmetic Swap Equations

int a = 10, b = 20;
a = a b; // a becomes 30
b = a b; // b becomes original a (10)
a = a b; // a becomes original b (20)
Select operators above and click Check!

🔄 Memory Swap Simulator

Trace will show here...
swap_methods.cReference C Code
#include <stdio.h> int main() { int a = 10, b = 20, temp; // Method 1: Using 3rd variable temp = a; a = b; b = temp; printf("Method 1 Swap: a=%d, b=%d\n", a, b); // Method 2: Without 3rd variable (Arithmetic) a = a + b; b = a - b; a = a - b; printf("Method 2 Swap: a=%d, b=%d\n", a, b); return 0; }
Knowledge Check

Test Your Formula & Operator Mastery

Question 1 of 5
Loading question...