Practice Assignment: Pattern Printing and Number Problems
Practice Assignment: Pattern Printing and Number Problems
Practice loops, nested loops, loop control, conditions, counters, and number logic by solving beginner-friendly pattern printing and number-based programming problems.
Assignment Overview
In this practice assignment, students will solve two types of loop-based problems:
- Pattern Printing Problems: Problems that use loops and nested loops to print stars, numbers, rows, and columns.
- Number Problems: Problems that use loops to calculate sums, factorials, multiplication tables, reverse numbers, check prime numbers, and identify palindromes.
This assignment is designed to strengthen logical thinking. Pattern printing helps students understand rows, columns, nested loops, spacing, and repeated output. Number problems help students practice loop counters, arithmetic operations, conditions, and repeated calculations.
Learning Objectives
After completing this assignment, students should be able to:
Objectives
- Use loops to repeat a task multiple times.
- Use nested loops to print rows and columns.
- Understand how outer loops and inner loops work together.
- Print star patterns and number patterns.
- Use loop counters correctly.
- Apply conditions inside loops.
- Use
BREAKandCONTINUEwhere needed. - Avoid infinite loops.
- Solve basic number problems using repeated calculations.
- Write clean and readable language-neutral pseudocode.
Prerequisites
Before attempting this assignment, students should revise the following topics:
Required Topics
- Input and output.
- Variables and data types.
- Arithmetic operators.
- Comparison operators.
- Logical operators.
- Decision making.
- For loop.
- While loop.
- Nested loops.
- Infinite loop.
- Break statement.
- Loop control best practices.
General Instructions
Solve all problems using language-neutral pseudocode. Do not write code in any specific programming language unless instructed by your teacher.
Student Instructions
- Read each problem carefully.
- Identify whether the problem needs a single loop or nested loops.
- For pattern problems, first identify rows and columns.
- For number problems, identify the input, process, and output.
- Use meaningful variable names.
- Use proper indentation.
- Test each program with at least three inputs.
- Use trace tables for difficult problems.
- Avoid infinite loops.
- Write the expected output clearly.
Pattern Printing Strategy
Most pattern printing problems can be solved by thinking about rows and columns.
| Part | Role | Example |
|---|---|---|
| Outer Loop | Controls the number of rows. | FOR row FROM 1 TO 5 |
| Inner Loop | Controls what is printed in each row. | FOR column FROM 1 TO row |
| New Line | Moves output to the next row after inner loop ends. | DISPLAY NEW LINE |
| Spaces | Used for alignment in pyramid or mirrored patterns. | DISPLAY " " WITHOUT NEW LINE |
Part A: Pattern Printing Problems
Solve the following pattern printing problems using nested loops.
Problem 1: Solid Square Pattern
Write pseudocode to print a solid square pattern of size 5.
Expected Output
*****
*****
*****
*****
*****
Hint
Use one loop for rows and one loop for columns. Each row should print five stars.
Problem 2: Right Triangle Star Pattern
Write pseudocode to print the following right triangle star pattern.
Expected Output
*
**
***
****
*****
Hint
The number of stars in each row is equal to the row number.
Problem 3: Inverted Right Triangle Star Pattern
Write pseudocode to print an inverted right triangle pattern.
Expected Output
*****
****
***
**
*
Hint
The number of stars decreases as the row number increases.
Problem 4: Number Triangle
Write pseudocode to print numbers in a triangle.
Expected Output
1
12
123
1234
12345
Hint
The inner loop should print numbers from 1 to the current row number.
Problem 5: Same Number Row Pattern
Write pseudocode to print the following pattern.
Expected Output
1
22
333
4444
55555
Hint
Print the row number repeatedly in each row.
Problem 6: Hollow Square Pattern
Write pseudocode to print a hollow square pattern of size 5.
Expected Output
*****
* *
* *
* *
*****
Hint
Print * on the first row, last row, first column, and last column. Otherwise print a blank space.
Problem 7: Pyramid Star Pattern
Write pseudocode to print a pyramid pattern.
Expected Output
*
***
*****
*******
*********
Hint
Use one inner loop for spaces and another inner loop for stars.
Part B: Number Problems
Solve the following number-based problems using loops.
Problem 8: Print Numbers from 1 to N
Write pseudocode to input a number N and print numbers from 1 to N.
Example Input
N = 5
Expected Output
1
2
3
4
5
Problem 9: Sum of First N Natural Numbers
Write pseudocode to calculate the sum of numbers from 1 to N.
Example Input
N = 5
Expected Output
Sum = 15
Problem 10: Multiplication Table
Write pseudocode to print the multiplication table of a given number.
Example Input
Number = 7
Expected Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Problem 11: Factorial of a Number
Write pseudocode to calculate the factorial of a number.
Example
5! = 5 x 4 x 3 x 2 x 1 = 120
Example Input
Number = 5
Expected Output
Factorial = 120
Problem 12: Reverse a Number
Write pseudocode to reverse a given number.
Example Input
Number = 1234
Expected Output
Reversed Number = 4321
Problem 13: Check Prime Number
Write pseudocode to check whether a number is prime.
Definition
A prime number is a number greater than 1 that has only two factors: 1 and itself.
Example Input
Number = 7
Expected Output
7 is a prime number
Problem 14: Check Palindrome Number
Write pseudocode to check whether a number is a palindrome.
Definition
A palindrome number reads the same forward and backward.
Example Input
Number = 121
Expected Output
121 is a palindrome number
Problem 15: Fibonacci Series
Write pseudocode to print the first N terms of the Fibonacci series.
Example
0, 1, 1, 2, 3, 5, 8, 13...
Example Input
N = 7
Expected Output
0
1
1
2
3
5
8
Sample Solution 1: Right Triangle Star Pattern
/*
Pattern:
*
**
***
****
*****
*/
ENTRY POINT
DECLARE totalRows AS INTEGER = 5
FOR row FROM 1 TO totalRows
FOR star FROM 1 TO row
DISPLAY "*" WITHOUT NEW LINE
END FOR
DISPLAY NEW LINE
END FOR
END ENTRY POINT
Sample Solution 2: Number Triangle
/*
Pattern:
1
12
123
1234
12345
*/
ENTRY POINT
DECLARE totalRows AS INTEGER = 5
FOR row FROM 1 TO totalRows
FOR number FROM 1 TO row
DISPLAY number WITHOUT NEW LINE
END FOR
DISPLAY NEW LINE
END FOR
END ENTRY POINT
Sample Solution 3: Hollow Square Pattern
/*
Pattern:
*****
* *
* *
* *
*****
*/
ENTRY POINT
DECLARE size AS INTEGER = 5
FOR row FROM 1 TO size
FOR column FROM 1 TO size
IF row == 1 OR row == size OR column == 1 OR column == size THEN
DISPLAY "*" WITHOUT NEW LINE
ELSE
DISPLAY " " WITHOUT NEW LINE
END IF
END FOR
DISPLAY NEW LINE
END FOR
END ENTRY POINT
Sample Solution 4: Sum of First N Natural Numbers
/*
This program calculates sum of numbers from 1 to N.
*/
ENTRY POINT
DECLARE n AS INTEGER = 0
DECLARE sum AS INTEGER = 0
DISPLAY "Enter value of N:"
INPUT n
IF n > 0 THEN
FOR number FROM 1 TO n
SET sum = sum + number
END FOR
DISPLAY "Sum = " + sum
ELSE
DISPLAY "Invalid input. N must be greater than 0."
END IF
END ENTRY POINT
Sample Solution 5: Factorial of a Number
/*
This program calculates factorial of a number.
*/
ENTRY POINT
DECLARE number AS INTEGER = 0
DECLARE factorial AS INTEGER = 1
DISPLAY "Enter a number:"
INPUT number
IF number >= 0 THEN
FOR counter FROM 1 TO number
SET factorial = factorial * counter
END FOR
DISPLAY "Factorial = " + factorial
ELSE
DISPLAY "Invalid input. Factorial is not defined for negative numbers."
END IF
END ENTRY POINT
Sample Solution 6: Reverse a Number
/*
This program reverses a number.
*/
ENTRY POINT
DECLARE number AS INTEGER = 0
DECLARE originalNumber AS INTEGER = 0
DECLARE reversedNumber AS INTEGER = 0
DECLARE digit AS INTEGER = 0
DISPLAY "Enter a number:"
INPUT number
SET originalNumber = number
WHILE number > 0 DO
SET digit = number MOD 10
SET reversedNumber = reversedNumber * 10 + digit
SET number = number DIV 10
END WHILE
DISPLAY "Original Number = " + originalNumber
DISPLAY "Reversed Number = " + reversedNumber
END ENTRY POINT
Sample Solution 7: Check Prime Number
/*
This program checks whether a number is prime.
*/
ENTRY POINT
DECLARE number AS INTEGER = 0
DECLARE divisor AS INTEGER = 2
DECLARE isPrime AS BOOLEAN = true
DISPLAY "Enter a number:"
INPUT number
IF number <= 1 THEN
SET isPrime = false
ELSE
WHILE divisor < number DO
IF number MOD divisor == 0 THEN
SET isPrime = false
BREAK
END IF
SET divisor = divisor + 1
END WHILE
END IF
IF isPrime == true THEN
DISPLAY number + " is a prime number"
ELSE
DISPLAY number + " is not a prime number"
END IF
END ENTRY POINT
Sample Solution 8: Check Palindrome Number
/*
This program checks whether a number is palindrome.
*/
ENTRY POINT
DECLARE number AS INTEGER = 0
DECLARE originalNumber AS INTEGER = 0
DECLARE reversedNumber AS INTEGER = 0
DECLARE digit AS INTEGER = 0
DISPLAY "Enter a number:"
INPUT number
SET originalNumber = number
WHILE number > 0 DO
SET digit = number MOD 10
SET reversedNumber = reversedNumber * 10 + digit
SET number = number DIV 10
END WHILE
IF originalNumber == reversedNumber THEN
DISPLAY originalNumber + " is a palindrome number"
ELSE
DISPLAY originalNumber + " is not a palindrome number"
END IF
END ENTRY POINT
Trace Table Example: Sum of First N Natural Numbers
For N = 5, trace the sum calculation:
| Iteration | number | sum before | sum after |
|---|---|---|---|
| 1 | 1 |
0 |
1 |
| 2 | 2 |
1 |
3 |
| 3 | 3 |
3 |
6 |
| 4 | 4 |
6 |
10 |
| 5 | 5 |
10 |
15 |
Required Test Cases
Students should test number problems using normal, boundary, and invalid inputs.
| Problem | Test Input | Expected Output |
|---|---|---|
| Sum of N numbers | N = 5 |
15 |
| Sum of N numbers | N = 0 |
Invalid input |
| Factorial | 5 |
120 |
| Factorial | 0 |
1 |
| Prime Check | 7 |
Prime |
| Prime Check | 10 |
Not Prime |
| Palindrome Check | 121 |
Palindrome |
| Palindrome Check | 123 |
Not Palindrome |
Bonus Challenges
Students who complete the main assignment can try these bonus challenges.
Bonus Problems
- Print a diamond star pattern.
- Print Floyd's triangle.
- Print Pascal's triangle.
- Find the sum of digits of a number.
- Check whether a number is an Armstrong number.
- Find the greatest common divisor of two numbers.
- Print all prime numbers from
1toN. - Print multiplication tables from
1to10.
Common Mistakes to Avoid
Mistakes
- Putting
DISPLAY NEW LINEinside the wrong loop. - Using the same variable for outer and inner loops.
- Forgetting to update while-loop variables.
- Creating infinite loops accidentally.
- Using wrong loop boundaries.
- Printing too many or too few stars.
- Not validating input before solving number problems.
- Forgetting to store the original number before reversing it.
- Checking prime numbers incorrectly for
0and1. - Not testing boundary cases.
Better Habits
- Use separate variables such as
rowandcolumn. - Keep indentation clean.
- Test patterns row by row.
- Use trace tables for number logic.
- Validate input before calculation.
- Use
BREAKwhen early exit is needed. - Use meaningful variable names.
- Test small values first.
- Check first row, last row, first column, and last column carefully.
- Write expected output before writing pseudocode.
Submission Checklist
Before submitting, students should verify the following:
Checklist
- All pattern problems are solved using nested loops.
- All number problems are solved using loops.
- Proper input and output are shown.
- Expected output is written for each problem.
- Variables have meaningful names.
- Indentation is clear.
- Invalid inputs are handled where required.
- Trace table is included for at least one number problem.
- At least three test cases are used for major number problems.
- No accidental infinite loops exist.
Evaluation Rubric
| Criteria | Marks | What to Check |
|---|---|---|
| Pattern Logic | 20 | Correct use of rows, columns, stars, spaces, and nested loops. |
| Number Problem Logic | 25 | Correct calculation for sum, factorial, reverse, prime, palindrome, and Fibonacci. |
| Loop Usage | 15 | Correct use of for loops, while loops, and nested loops. |
| Input Validation | 10 | Handles invalid or boundary input properly. |
| Readability | 10 | Uses meaningful names and clean indentation. |
| Testing | 10 | Includes normal, boundary, and invalid test cases. |
| Trace Table | 10 | Shows step-by-step logic for at least one problem. |
Mini Viva Questions
Why are nested loops used in pattern printing?
Nested loops are used because pattern printing usually has two levels: rows and columns.
What does the outer loop usually control in pattern printing?
The outer loop usually controls the number of rows.
What does the inner loop usually control?
The inner loop usually controls what gets printed in each row, such as stars, numbers, or spaces.
Why should the original number be stored before reversing a number?
The original number should be stored because the number changes during the reverse process, and we may need the original value for comparison.
Why is BREAK useful in prime number checking?
BREAK is useful because once a divisor is found, the number is not prime and there is no need to continue checking.
Interview Questions
How do you solve a pattern printing problem?
First identify the number of rows, then decide what should be printed in each row using columns, spaces, numbers, or symbols.
What is the difference between a single loop and a nested loop?
A single loop handles one level of repetition, while a nested loop handles repetition inside another repetition.
How do you check whether a number is palindrome?
Reverse the number and compare the reversed number with the original number.
How do you calculate factorial using a loop?
Start with factorial as 1 and multiply it by each number from 1 to the given number.
What is the main risk when using loops?
The main risk is creating an infinite loop if the stopping condition is missing or incorrect.
Quick Summary
| Topic | Key Idea |
|---|---|
| Pattern Printing | Uses nested loops to print rows and columns. |
| Outer Loop | Usually controls rows. |
| Inner Loop | Usually controls columns, stars, numbers, or spaces. |
| Number Problems | Use loops for repeated calculations. |
| Factorial | Repeated multiplication. |
| Reverse Number | Extract digits and build a reversed value. |
| Prime Number | Check whether a number has divisors other than 1 and itself. |
| Palindrome Number | Compare original number with reversed number. |
| Best Practice | Use clear variables, proper indentation, validation, and trace tables. |
Final Takeaway
Pattern printing and number problems are excellent practice for mastering loops. Pattern printing teaches students how rows, columns, spaces, and nested loops work. Number problems teach repeated calculations, conditions, digit extraction, and logical checking. In the Programming Mastery Course, students should treat these problems as logic-building exercises that prepare them for real-world programming and future problem-solving.