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

Assignment

The document contains a series of programming tasks implemented in C, focusing on various patterns and structures using loops. Each task includes code snippets that demonstrate how to print different shapes and sequences, such as stars and numbers, in a console output. The tasks are designed for a Computer Fundamentals Lab assignment at the Pakistan Institute of Engineering and Applied Sciences.

Uploaded by

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

Assignment

The document contains a series of programming tasks implemented in C, focusing on various patterns and structures using loops. Each task includes code snippets that demonstrate how to print different shapes and sequences, such as stars and numbers, in a console output. The tasks are designed for a Computer Fundamentals Lab assignment at the Pakistan Institute of Engineering and Applied Sciences.

Uploaded by

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

PAKISTAN INSTITUTE OF ENGINEERING AND APPLIED SCIENCES

COMPUTER FUNDAMENTALS LAB


Assignment

Submitted by:
Syeda Maham Ali

11-3-046-2020

BSChE (20-24)
Task 1
Code:
#include <stdio.h>

int main()

int i, j;

int n = 5; // Number of lines

for (i = 1; i <= n; i++)

for (j = 1; j <= i; j++)

{ // Loop to print '*' in each line

printf("*");

printf("\n"); // Move to the next line

return 0;

Task 2
Code:
#include <stdio.h>

int main()

int i, j;

int n = 5; // Number of lines

for (i = 1; i <= n; i++)

// Print spaces

for (j = 1; j <= n - i; j++)

{
printf(" ");

// Print asterisks

for (j = 1; j <= i; j++)

printf("*");

printf("\n"); // Move to the next line

return 0;

Task 3
Code:
#include <stdio.h>

int main() {

int i, j, k;

int n = 5; // Number of lines

int maxStars = 9; // Maximum number of stars in the last row

for (i = 0; i < n; i++) { // Loop for each line

// Print spaces

for (j = 0; j < (maxStars - (2 * i + 1)) / 2; j++) {

printf(" ");

// Print stars

for (k = 0; k < (2 * i + 1); k++) {

printf("*");

printf("\n"); // Move to the next line


}
return 0;

Task 4
Code:
#include <stdio.h>

int main() {

int i, j;

int n = 5; // Number of lines

for (i = 0; i < n; i++) { // Loop for each line

// Print leading spaces

for (j = 0; j < i; j++) {

printf(" ");

// Print stars

for (j = 0; j < n - i; j++) {

printf("*");

printf("\n"); // Move to the next line

return 0;

Task 5
Code:
#include <stdio.h>

int main() {

int i, j;

int n = 5; // Number of lines

for (i = 0; i < n; i++) { // Loop for each line

// Print stars
for (j = 0; j < n - i; j++) {

printf("*");

printf("\n"); // Move to the next line

return 0;

Task 6
Code:
#include <stdio.h>

int main() {

int i, j;

int n = 5; // Number of lines

int stars = 9; // Number of stars in the first row

for (i = 0; i < n; i++) { // Loop for each line

// Print leading spaces

for (j = 0; j < i; j++) {

printf(" ");

// Print stars

for (j = 0; j < stars; j++) {

printf("*");

printf("\n"); // Move to the next line

stars -= 2; // Decrease the number of stars for the next line

return 0;

}
Task 7
Code:
#include <stdio.h>

int main() {

int i, j;

int n = 5; // Number of lines

// Print upper half of the pattern

for (i = 0; i < n; i++) { // Loop for each line

// Print leading spaces

for (j = 0; j < n - i - 1; j++) {

printf(" ");

// Print stars

for (j = 0; j < 2 * i + 1; j++) {

printf("*");

printf("\n"); // Move to the next line

// Print lower half of the pattern

for (i = n - 2; i >= 0; i--) { // Loop for each line in reverse order

// Print leading spaces

for (j = 0; j < n - i - 1; j++) {

printf(" ");

// Print stars

for (j = 0; j < 2 * i + 1; j++) {

printf("*");

}
printf("\n"); // Move to the next line
}

return 0;

Task 8
Code:
#include <stdio.h>

int main() {

char startChar = 'A';

int spaceCount = 0;

int n = 7; // Number of lines

for (int i = 0; i < n; i++) { // Loop for each line

// Print characters

for (char ch = 'A'; ch < startChar + n - spaceCount; ch++) {

printf("%c ", ch);

// Print spaces

for (int j = 0; j < spaceCount * 2; j++) {

printf(" "); // Two spaces for each character

// Print characters again (excluding the last one)

for (char ch = startChar + n - spaceCount - 1; ch >= 'A'; ch--) {

if (ch != startChar + n - spaceCount - 1) {

printf(" "); // One space between characters

printf("%c", ch);

printf("\n"); // Move to the next line

spaceCount++; // Increase space count for the next line


}
return 0;

Task 9
Code:
#include <stdio.h>

int main() {

int n = 5; // Number of lines

// Print upper half of the pattern

for (int i = n; i >= 1; i--) { // Loop for each line

// Print leading spaces

for (int j = 0; j < n - i; j++) {

printf(" ");

// Print stars

for (int j = 0; j < 2 * i - 1; j++) {

printf("*");

printf("\n"); // Move to the next line

// Print lower half of the pattern

for (int i = 2; i <= n; i++) { // Loop for each line

// Print leading spaces

for (int j = 0; j < n - i; j++) {

printf(" ");

// Print stars

for (int j = 0; j < 2 * i - 1; j++) {

printf("*");
}
printf("\n"); // Move to the next line

return 0;

Task 10
Code:
#include <stdio.h>

int main() {

int n = 5; // Number of lines

// Upper half of the pattern

for (int i = 0; i < n; i++) {

// Print stars

for (int j = 0; j <= i; j++) {

printf("* ");

// Print spaces

for (int j = 0; j < 2 * (n - i - 1); j++) {

printf(" ");

// Print stars again

for (int j = 0; j <= i; j++) {

printf("* ");

printf("\n");

// Lower half of the pattern

for (int i = n - 2; i >= 0; i--) {

// Print stars
for (int j = 0; j <= i; j++) {
printf("* ");

// Print spaces

for (int j = 0; j < 2 * (n - i - 1); j++) {

printf(" ");

// Print stars again

for (int j = 0; j <= i; j++) {

printf("* ");

printf("\n");

return 0;

Task 11
Code:
#include <stdio.h>

int main() {

int n = 5; // Number of lines

// Loop for each row

for (int i = 0; i < n; i++) {

// Print stars in the left side

for (int j = 0; j <= i; j++) {

printf("* ");

// Print spaces in the middle

for (int j = 0; j < 2 * (n - i - 1); j++) {

printf(" ");
}
// Print stars in the right side

for (int j = 0; j <= i; j++) {

printf("* ");

printf("\n");

return 0;

Task 12
Code:
#include <stdio.h>

int main() {

int n = 6; // Number of lines

// Loop for each row

for (int i = 0; i < n; i++) {

// Print numbers for each row

for (int j = 0; j <= i; j++) {

printf("%d ", j);

printf("\n");

return 0;

You might also like