๐Ÿ  Session 7 Homework

Decision Making Practice - Basic if Statements

๐Ÿ“š What You'll Practice

This homework focuses on mastering basic if statements - the foundation of decision making in C programming. You'll build 6 different programs that make decisions based on user input.

Goal: Get comfortable with if statements before we move to if-else in Session 8!

๐Ÿ“‹ Instructions for Rahul

๐ŸŒก๏ธ Program 1: Temperature Checker

Help people decide what to wear based on temperature!

What it does:

Asks for a temperature and tells the user whether it's hot or cold, suggesting appropriate clothing.

#include <stdio.h> int main() { int temp; printf("Enter the temperature: "); scanf("%d", &temp); if (temp > 80) { printf("It's hot! Wear shorts.\n"); } if (temp < 60) { printf("It's cold! Wear a jacket.\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 85 โ†’ "It's hot! Wear shorts."
  • Try 45 โ†’ "It's cold! Wear a jacket."
  • Try 70 โ†’ No message (between 60-80)

โž•โž– Program 2: Positive or Negative

Determine if a number is positive, negative, or zero!

What it does:

Takes a number from the user and classifies it as positive, negative, or zero using multiple if statements.

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num > 0) { printf("The number is positive!\n"); } if (num < 0) { printf("The number is negative!\n"); } if (num == 0) { printf("The number is zero!\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 5 โ†’ "The number is positive!"
  • Try -3 โ†’ "The number is negative!"
  • Try 0 โ†’ "The number is zero!"

๐Ÿ—ณ๏ธ Program 3: Can You Vote?

Check if someone is old enough to vote!

What it does:

Asks for a person's age and determines if they can vote (18 or older) or are too young.

#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); if (age >= 18) { printf("You can vote!\n"); } if (age < 18) { printf("You are too young to vote.\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 20 โ†’ "You can vote!"
  • Try 16 โ†’ "You are too young to vote."
  • Try 18 โ†’ "You can vote!" (exactly 18)

๐Ÿ”ข Program 4: Even or Odd

Determine if a number is even or odd using the modulo operator!

What it does:

Uses the modulo operator (%) to check if a number is divisible by 2, determining if it's even or odd.

#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) { printf("The number is EVEN!\n"); } if (num % 2 != 0) { printf("The number is ODD!\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 8 โ†’ "The number is EVEN!"
  • Try 7 โ†’ "The number is ODD!"
  • Try 0 โ†’ "The number is EVEN!" (0 is even)

๐Ÿ” Program 5: Simple Password Check

Create a basic security system with password verification!

What it does:

Asks for a password and grants or denies access based on whether the correct password (1234) is entered.

#include <stdio.h> int main() { int password; printf("Enter the password: "); scanf("%d", &password); if (password == 1234) { printf("Access Granted! Welcome!\n"); } if (password != 1234) { printf("Access Denied! Wrong password!\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 1234 โ†’ "Access Granted! Welcome!"
  • Try 5678 โ†’ "Access Denied! Wrong password!"
  • Try 0 โ†’ "Access Denied! Wrong password!"

๐Ÿ“Š Program 6: Passing or Failing Grade

Check if a test score is passing or failing!

What it does:

Takes a test score and determines if the student passed (60 or above) or needs to study more.

#include <stdio.h> int main() { int score; printf("Enter your test score: "); scanf("%d", &score); if (score >= 60) { printf("You PASSED! Great job!\n"); } if (score < 60) { printf("You need to study more.\n"); } return 0; }

๐Ÿงช Test Cases

  • Try 85 โ†’ "You PASSED! Great job!"
  • Try 45 โ†’ "You need to study more."
  • Try 60 โ†’ "You PASSED! Great job!" (exactly 60)

โญ Bonus Challenge (Optional)

Ready to go the extra mile? Try these enhancements: