4. Breaking Down Problems
Problem-Solving Approach
1. Understand: Carefully read and analyze the problem statement to clarify
what is required.
2. Plan: Outline the logical steps needed to reach the solution.
3. Write: Implement the code based on the planned logic.
4. Test: Verify the solution with different inputs to ensure correctness.
6. Example 1
Problem: Write a program that determines if a
number is even or odd.
Understand: Even numbers are divisible by 2; odd
numbers are not.
Plan: Use if-else statement and the modulus
operator.
Write: Implement the code
Test: Testing with various cases, e.g., testing both
even and odd numbers.
7. Example 2
Problem: Write a program to find the area of a
rectangle given its length and width.
Understand: Formula for the area of a rectangle is
length * width.
Plan: Get inputs for length and width, multiply
them, and print the result.
Write: Implement the code
Test: Test with different values
8. Exercises (Problem-Solving)
Exercise 1: Find the square of a number.
• Input: A single integer.
• Output: Its square.
Exercise 2: Calculate the sum of two numbers.
• Input: Two integers.
• Output: Their sum.
Exercise 3: Convert minutes to hours.
• Input: Number of minutes.
• Output: Hours and remaining minutes (e.g., 150
minutes is 2 hours, 30 minutes).
Exercise 4: Determine if a number is positive or
negative.
• Input: A single integer.
• Output: Display whether it is positive or
negative.
Exercise 5: Check if a character is uppercase or
lowercase.
• Input: A single character.
• Output: Display "Uppercase" or "Lowercase".
24. Exercises (Control Statements)
Exercise 1: Voting Eligibility Check.
• Description: Use if-else to determine if a person
can vote (age 18 and above).
Exercise 2: Simple Grading System.
• Description: Use if-else to assign a grade based
on a score (90-100: A, 80-89: B, etc.).
Exercise 3: Temperature Check.
• Description: Use if-else to categorize
temperature (e.g., <0: Freezing, 1-10: Cold, etc.).
Exercise 4: Simple Calculator Using Switch.
• Description: Use switch to perform simple
calculator operations (add, subtract, multiply,
divide).
26. Introduction to Loops
A loop is a programming structure that repeats a block of code as long as a
specified condition is met.
Why Use Loops?
• Loops allow us to automate repetitive tasks, making code efficient and
reducing redundancy.
• Useful in situations where actions need to be repeated a specific number of
times or until a condition is satisfied.
27. The for Loop
Structure (Syntax)
Initialization: Sets a starting value, typically for a
counter variable.
Condition: Specifies when the loop should
continue; the loop runs as long as this condition is
true
Update: Adjust the counter after each loop
iteration, usually with increment (++) or decrement
(--).
28. The for Loop Structure
(Flow of Execution)
Initialize: Set the loop variable to its starting value.
Check Condition: If true, the loop body executes;
if false, the loop exits.
Execute: Run the code inside the loop.
Update: Modify the loop variable according to the
update expression, then repeat.
29. Common Uses of the for Loop (Examples)
1. Iterate Through an Array
Example: Print all elements of an array.
30. Common Uses of the for Loop (Examples)
2. Sum of Natural Numbers (1 to N)
Example: Calculate the sum of numbers from 1 to 10.
31. Introduction to Arrays
An array is a collection of variables that are stored in contiguous memory
locations and can be accessed by an index.
Characteristics:
• Arrays store multiple values of the same data type.
• Fixed size, meaning the size is defined at the time of declaration.
• Array index starts at 0.
33. Working with Arrays (Examples)
Example 1: Sum of Array Elements
Calculate the sum of all elements in an array.
34. Working with Arrays (Examples)
Example 2: Finding the Maximum Element
Find the largest value in an array of numbers.
35. Exercises (Arrays)
Exercise 1: Find the Average of Elements
• Write code to calculate the average of an array's
elements.
Exercise 2: Count Occurrences of a Number.
• Count how many times a specific number
appears in an array.
Exercise 3: Print Array in Reverse Order
• Print the array starting from the last element.
Exercise 4: Merge Two Arrays
• Combine two arrays into one larger array
Exercise 5: Find Difference Between Max and Min
Values
• Calculate the difference between the largest and
smallest values in the array.
Exercise 6: Sort an Array
• Sort the array in ascending or descending order
(without using built-in functions if possible).
38. Exercises (Multi-Dimensional Arrays)
Exercise 1: Transpose of a Matrix
• Create a program that transposes a 2D array
(i.e., swaps rows with columns).
Exercise 2: Row and Column Sum
• Write a program to find the sum of each row
and each column in a 2D array.
Exercise 3: Find Maximum and Minimum Elements
• Write a program to find the maximum and
minimum values in a 2D array.
Exercise 4: Diagonal Sum
• Write a program to calculate the sum of the
main diagonal elements of a square matrix.
Exercise 5: Count Odd and Even Numbers
• Write a program to count the number of odd
and even numbers in a 2D array.
Exercise 6: Replace Negative Numbers
• Replace all negative numbers in a 2D array with
0.
39. Exercises (Multi-Dimensional Arrays)
Exercise 7: Count Elements Greater Than a Given
Value
• Write a program that counts how many
elements in a 2D array are greater than a given
value.
Exercise 8: Check Symmetry
• Write a program to check if a 2D array is
symmetric, i.e., (array[i][j] == array[j][i])
Exercise 9: Count Positive and Negative Elements
• Write a program to count the number of
positive and negative elements in a 2D array.
Exercise 10: Increment Elements
• Write a program to increment each element in a
2D array by 1.