0% found this document useful (0 votes)
6 views

Assignment 4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 4

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY

UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR

PROGRAMMING FUNDAMENTALS (THEORY) [CS-102]

Behram Khan

Assignment # 04

Refer to this video and learn how pattern printing works

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.

Now think why the loops are designed this way?

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 star in first line


● 2 stars in second line
● 3 stars in third line
● 4 stars in fourth line
● 5 stars in the fifth line.

So the inner loop with variable j is going to execute

● 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.

See Loop1.c file attached


DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR

First iteration:

Second iteration:

third iteration:
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR

Similarly for iteration 4 and iteration 5.


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).

Write dry code like this:

Don’t take help from CHATGPT, Try to solve them yourselves!


I have already linked videos to help you
Some questions are solved in this playlist, refer to it if you need help.
https://www.youtube.com/playlist?list=PLIY8eNdw5tW8TmAF1Xkez1CY7HE4X9KRL
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR

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.

Breaking Down the Problem:

Part 1: Printing the Upper Half of the Triangle

1. Input: Ask the user to enter the number of rows (N).


2. Outer Loop (Rows): Create a loop that iterates from 1 to N (inclusive). This loop will
control the rows of the upper half of the triangle.
3. Inner Loop 1 (Leading Spaces): Inside the outer loop, create another loop that prints
spaces. The number of spaces should decrease as the row number increases. The
formula for spaces is N - i (where i is the current row number).
DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY
UNIVERSITY OF ENGINEERING AND TECHNOLOGY PESHAWAR

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.

Part 2: Printing the Lower Half of the Triangle

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.

Don’t dry run the below question

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.

1. do it using while loop using flags


2. do it using do-while loop

You might also like