Assignment 4
Assignment 4
Behram Khan
Assignment # 04
Simple Triangle/Half Pyramid (With C++ Code) Part 1 | Pattern Printing Programs
Simple Triangle/Half Pyramid (With C++ Code) Part 2 | Pattern Printing Programs
Example Scenario: Write code to print this pattern using nested loop
Now think over it, we are going to use two nested for loops here since we already know we need to
print 5 lines rows and inside the 5 rows we have to print columns.
As you can see the outer loop with control variable or loop variable i is going to iterate/repeat 5(n)
times as you can see we need to print 5 lines of stars but inside these 5 lines we need to print
● 1 times when i = 1 (j = 1)
● 2 times when i = 2 (j = 2)
● 3 times when i = 3 (j = 3)
● 4 times when i =4 (j = 4)
● 5 times when i = 5 (j = 5)
● both the variables i and j are executing the same number of times in every iteration.
So the outer loop is executing for the number of rows and the inner loop is executing for the number
of Columns.
First iteration:
Second iteration:
third iteration:
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR
Your Tasks:
Write a C program to print the following patterns and also perform a dry run of the code
(dry code means to write each loop iteration (loop variable values in every iteration) and
their output as shown in above example scenario, Do dry run on paper and upload its
picture in word along with its code).
1.
2.
3.
4.
5.
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR
6.
7. Design a C program that prints a numerical triangle pattern. The pattern should
start with "1" at the top and incrementally increase the numbers in each row up to
a specified number of rows (N). After reaching the Nth row, the pattern should
then decrease back down to "1". The numbers in each row should be separated by
spaces, and the entire pattern should be centered by adding leading spaces to each
row.
4. Inner Loop 2 (Numbers): After printing the spaces, create another loop that prints
the numbers from 1 to the current row number (i).
5. Newline: After the inner loops complete, print a newline character (\n) to move to
the next row.
1. Outer Loop (Rows - Reversed): Create a loop that iterates from N-1 down to 1. This
loop will control the rows of the lower half of the triangle.
2. Inner Loop 1 (Leading Spaces): Similar to Part 1, create a loop that prints spaces. The
number of spaces should increase as the row number decreases. The formula for
spaces is still N-i.
3. Inner Loop 2 (Numbers): Print the numbers from 1 to the current row number (i).
4. Newline: After the inner loops complete, print a newline character (\n).
8.
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR
9.
10. Modify Birthday Calculator app to calculate next birthday from the user if they press y
or Y at the end of the program. Make a flowchart for this question too.