Assignment 2-A

Control Flow Programs

Write programs that demonstrate mastery of decision-making and iteration using if-else statements, switch cases, and various loop constructs.

3-4 hours
Beginner
100 Points
Submit Assignment
What You'll Practice
  • if-else decision making
  • switch-case statements
  • for loop iterations
  • while and do-while loops
  • Nested control structures
Contents
01

Assignment Overview

Control flow is the backbone of programming logic. In this assignment, you will implement 6 complete C programs that test your ability to control program execution through conditions, loops, and nested structures. Each program focuses on different aspects of Module 2 concepts.

Standard C Only: You must use ONLY standard C libraries (stdio.h, stdlib.h, math.h). No external libraries or compiler-specific extensions allowed. This tests your understanding of pure C.
Skills Applied: This assignment tests your understanding of Operators (2.1), Conditional Statements (2.2), and Loops (2.3) from Module 2.
Conditionals (2.2)

if, else if, else, switch-case, ternary operator

Loops (2.3)

for, while, do-while, nested loops, counters

Flow Control

break, continue, logical operators, input validation

Ready to submit? Already completed the assignment? Submit your work now!
Submit Now
02

The Scenario

CodeForge Software - Training Week

You've successfully completed your first week at CodeForge Software! Now it's time to demonstrate your ability to write programs that make decisions and repeat actions efficiently.

"Master control flow and you master programming. Every algorithm, every feature, every user interaction depends on your ability to control when and how code executes."

Team Lead

Your Task

Create 6 separate C files, each implementing a specific program that demonstrates different control flow concepts. Your programs must handle various inputs correctly, including edge cases and invalid inputs. Each program should compile without warnings using gcc -Wall -Wextra -std=c99.

03

The Datasets

Use these test inputs to verify your programs work correctly. Your output should match exactly:

Dataset 1: grades.c Test Inputs

// Test scores to verify your grade calculator
// Input → Expected Output

85  → Score: 85 | Grade: B
90  → Score: 90 | Grade: A
75  → Score: 75 | Grade: C
65  → Score: 65 | Grade: D
45  → Score: 45 | Grade: F
100 → Score: 100 | Grade: A
0   → Score: 0 | Grade: F
105 → Error: Score must be between 0 and 100
-5  → Error: Score must be between 0 and 100

Dataset 2: menu.c Test Inputs

// Calculator menu test cases
// Choice, Num1, Num2 → Expected Result

1, 10, 5  → Result: 10 + 5 = 15
2, 20, 8  → Result: 20 - 8 = 12
3, 7, 6   → Result: 7 * 6 = 42
4, 15, 3  → Result: 15 / 3 = 5
4, 10, 0  → Error: Division by zero!
5, 10, 5  → Error: Invalid choice!

Dataset 3: table.c Test Inputs

// Multiplication table test values
// Input number → First and last lines of output

7  → 7 x 1 = 7 ... 7 x 10 = 70
12 → 12 x 1 = 12 ... 12 x 10 = 120
1  → 1 x 1 = 1 ... 1 x 10 = 10
0  → 0 x 1 = 0 ... 0 x 10 = 0

Dataset 4: guess.c Secret Numbers

// Use these secret numbers for testing (hardcode one at a time)
// Recommended test values: 1, 50, 100, 37, 73

// Example gameplay with secret = 37:
// Guess: 50 → Too high!
// Guess: 25 → Too low!
// Guess: 37 → Correct! You got it in 3 attempts!

Dataset 5: pattern.c Test Heights

// Pattern heights to test
// Height → Expected pattern

3 →  *
     **
     ***

5 →  *
     **
     ***
     ****
     *****

1 →  *

Dataset 6: prime.c Test Numbers

// Prime number test cases
// Input → Expected Output

2   → 2 is a prime number
3   → 3 is a prime number
17  → 17 is a prime number
97  → 97 is a prime number
1   → 1 is not a prime number
4   → 4 is not a prime number (divisible by 2)
24  → 24 is not a prime number (divisible by 2)
49  → 49 is not a prime number (divisible by 7)
0   → 0 is not a prime number
-5  → Error: Please enter a positive number
Test Data Guidelines
  • Edge cases - Always test boundary values (0, 1, max values)
  • Invalid inputs - Test negative numbers, out-of-range values
  • Typical cases - Test normal expected inputs
  • Output format - Match spacing, punctuation, and case exactly
04

Requirements

Create 6 separate C files, each implementing a specific program. Each program must compile without warnings and run correctly.

1
Grade Calculator 15 pts

Create a program grades.c that:

  • Takes a numerical score (0-100) as input using scanf()
  • Uses if-else ladder to determine letter grade (A, B, C, D, F)
  • Displays both the score and letter grade
  • Handles invalid input (scores outside 0-100) with error message
// Grade scale:
// A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: below 60

// Example output:
// Enter score: 85
// Score: 85 | Grade: B
2
Simple Calculator Menu 15 pts

Create a program menu.c that:

  • Displays a menu with 4 calculator operations (+, -, *, /)
  • Uses switch-case to handle user choice (1-4)
  • Takes two numbers and performs the selected operation
  • Handles division by zero with appropriate error message
  • Has a default case for invalid menu choices
// Example menu:
// === Calculator Menu ===
// 1. Addition
// 2. Subtraction
// 3. Multiplication
// 4. Division
// Enter choice (1-4): 
3
Multiplication Table 15 pts

Create a program table.c that:

  • Takes a number n as input
  • Uses a for loop to print multiplication table (1 to 10)
  • Format output neatly with proper alignment using printf formatting
  • Validates that n is a positive integer
// Example for n=7:
// Multiplication Table for 7:
// 7 x  1 =   7
// 7 x  2 =  14
// 7 x  3 =  21
// ...
// 7 x 10 =  70
4
Number Guessing Game 20 pts

Create a program guess.c that:

  • Has a secret number (hardcoded between 1-100)
  • Uses while loop to let user keep guessing
  • Provides hints ("Too high!" or "Too low!") after each guess
  • Counts and displays number of attempts when correct
  • Validates input is within valid range (1-100)
// Example gameplay:
// Guess the number (1-100): 50
// Too low! Try again.
// Guess the number (1-100): 75
// Too high! Try again.
// Guess the number (1-100): 63
// Correct! You got it in 3 attempts!
5
Pattern Printing 15 pts

Create a program pattern.c that:

  • Takes height as input (1-20)
  • Uses nested for loops to print a right triangle pattern
  • Uses asterisks (*) for the pattern
  • Validates height is within valid range
// Example for height=5:
// *
// **
// ***
// ****
// *****
6
Prime Number Checker 20 pts

Create a program prime.c that:

  • Takes a positive integer as input
  • Uses a loop with optimized checking (up to sqrt(n))
  • Uses break statement to exit early when factor found
  • Handles edge cases: 0, 1, 2, and negative numbers
  • Displays clear result: "X is a prime number" or "X is not a prime number"
// Include math.h for sqrt()
#include <math.h>

// Example outputs:
// Enter a number: 17
// 17 is a prime number

// Enter a number: 24
// 24 is not a prime number (divisible by 2)
05

Submission

Create a public GitHub repository with the exact name shown below:

Required Repository Name
c-control-flow-assignment
github.com/<your-username>/c-control-flow-assignment
Required Files
c-control-flow-assignment/
├── grades.c          # Program 1: Grade Calculator
├── menu.c            # Program 2: Calculator Menu
├── table.c           # Program 3: Multiplication Table
├── guess.c           # Program 4: Number Guessing Game
├── pattern.c         # Program 5: Pattern Printing
├── prime.c           # Program 6: Prime Number Checker
└── README.md         # REQUIRED - see contents below
README.md Must Include:
  • Your full name and submission date
  • Brief description of each program (1-2 sentences each)
  • Compilation instructions (e.g., gcc -o grades grades.c)
  • Sample test cases you used for each program
  • Any challenges faced and how you solved them
Do Include
  • All 6 .c files that compile without errors
  • Proper comments explaining your logic
  • Input validation in every program
  • Clean, readable code with good formatting
  • README.md with all required sections
  • Meaningful variable names
Do Not Include
  • Compiled executables (.exe, .out files)
  • IDE-specific files (.vscode, .idea folders)
  • Code that does not compile
  • Plagiarized or copied code
  • Programs without input validation
  • Uncommented, unreadable code
Important: Before submitting, compile and test ALL programs to make sure they work correctly with various inputs including edge cases!
Submit Your Assignment

Enter your GitHub username - we'll verify your repository automatically

06

Grading Rubric

Your assignment will be graded on the following criteria:

Criteria Points Description
Program 1: Grade Calculator 15 Correct if-else ladder implementation, proper grade ranges, input validation
Program 2: Calculator Menu 15 Proper switch-case usage, all operations work, division by zero handled
Program 3: Multiplication Table 15 Correct for loop, proper formatting with alignment, input validation
Program 4: Guessing Game 20 While loop, correct hints, attempt counter, input validation
Program 5: Pattern Printing 15 Nested loop implementation, correct pattern output
Program 6: Prime Checker 20 Optimized algorithm, edge cases handled, proper use of break
Total 100

Ready to Submit?

Make sure you have completed all requirements and reviewed the grading rubric above.

Submit Your Assignment
07

What You Will Practice

Decision Making (2.2)

Using if-else ladders for grade ranges, switch-case for menu options, and handling edge cases

Loop Constructs (2.3)

For loops for counting, while loops for unknown iterations, nested loops for patterns

Input Validation

Checking ranges, handling invalid input, providing meaningful error messages

Algorithm Design

Prime number checking with optimization, pattern generation logic, game flow control

08

Pro Tips

C Best Practices
  • Always initialize variables before use
  • Use meaningful variable names (not just i, x)
  • Add comments for complex logic sections
  • Check return value of scanf() for input validation
Compilation Tips
  • Use gcc -Wall to see all warnings
  • For prime.c, link math library: gcc -o prime prime.c -lm
  • Test with edge cases (0, negative, very large numbers)
  • Compile frequently to catch errors early
Time Management
  • Start with simpler programs (grades, table)
  • Build and test incrementally
  • Save the harder ones (guess, prime) for later
  • Leave time for testing and README
Common Mistakes
  • Forgetting break in switch cases
  • Using = instead of == in conditions
  • Infinite loops (forgetting to update counter)
  • Off-by-one errors in loop bounds
09

Pre-Submission Checklist

Code Requirements
Repository Requirements