Introduction to Computer Programming Lab: Using Loops to Print Numbers and Patterns - Prof, Schemes and Mind Maps of Compilers

This lab exercise focuses on the practical application of loops in computer programming. Students are tasked with implementing various tasks using both 'for' and 'while' loops, demonstrating their understanding of loop syntax, conditional execution, and iterative processes. The lab includes tasks such as calculating simple interest, generating multiplication tables, printing ascii characters, and creating patterns of asterisks. It provides a hands-on approach to learning fundamental programming concepts.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 10/23/2024

aznia-patrick
aznia-patrick 🇵🇰

3 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab: 3
Course: Introduction to Computer Programming (CSC-141)
Instructor: Engr. Imtiaz Ur Rehman
Student:
______________________________________
(Do not write anything here)
ASSESSMENT
In-Lab Performance: /
Post-Lab:
Data Presentation (4) Data Analysis (4) Writing Style (4)
Total: /
Instructor’s remarks:
Lab # 03 Using Loops to Print Number
COMSATS University Islamabad
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Introduction to Computer Programming Lab: Using Loops to Print Numbers and Patterns - Prof and more Schemes and Mind Maps Compilers in PDF only on Docsity!

Lab: 3

Course: Introduction to Computer Programming (CSC-141)

Instructor: Engr. Imtiaz Ur Rehman

Student:

______________________________________

(Do not write anything here)

ASSESSMENT In-Lab Performance: / Post-Lab:

Data Presentation (4) Data Analysis (4) Writing Style (4)

Total: / Instructor’s remarks: Lab # 03 Using Loops to Print Number

COMSATS University Islamabad

Sequences and Patterns

Objectives:

 Learn to use loops for repetitive tasks.

 Practice conditional execution in loops to generate patterns of asterisks.

In Lab:

Task 1:

Code(for):

#include<stdio.h> int main() { int p, n; float r, SI; int i; for (i=1; i<=5; i++) { printf("Enter the value of p, n and r: "); scanf("%d %d %f", &p, &n, &r); SI = (pnr)/100; printf("The Simple Interest for the given values of p,n and r is: %.3f\n", SI); } return 0; }

Procedure:

Code(while):

#include<stdio.h> int main()

Code(while):

#include <stdio.h> int main() { int x,y; printf("The Table of="); scanf("%d",&x); y=1; while(y<=10) { printf("%d%d=%d\n",x,y,xy); y++; } return 0; }

Procedure:

Task 3:

Code(for):

#include<stdio.h> int main() { for (int i=0; i<=255; i++){ printf("%d = %c\n",i,i); } return 0; }

Procedure:

Task 4:

Code(for):

#include <stdio.h> int main() { int red, green, blue; for (red = 1; red <= 3; red++) { for (green = 1; green <= 3; green++) { for (blue = 1; blue <= 3; blue++) { printf("%d, %d, %d\n", red, green, blue); } } } return 0; }

Procedure:

Code(while):

#include<stdio.h> int main() { int red=1; while(red<=3){ int green=1; while(green<=3){ int blue=1; while(blue<=3){ printf("%d,%d,%d\n",red,green,blue); blue++; } green++; } red++; } return 0; }

Procedure:

Code(while):

#include <stdio.h> int main() { int value=1,sum=0; while (value <= 10) { sum = sum + value; value++; } printf("Sum of first 10 natural numbers = %d",sum); return 0; }

Procedure:

Task 6:

Code(for):

#include<stdio.h> int main() { int n, i, pink = 0; printf("Enter a positive integer "); scanf("%d",&n); for(i=2; i<=n/2; ++i) { if(n%i==0){ pink=1; break; } } if (pink==0){ printf("%d is a prime number.",n); } else{ printf("%d is not a prime number.",n); } return 0; }

Procedure:

for(i=1; i<=digit; i++) { factorial = factorial*i; } printf("Factorial of %d is: %d", digit, factorial); return 0; }

Procedure:

Code(while):

#include<stdio.h> int main() { int i,digit,factorial=1; printf("Enter an integer"); scanf("%d",&digit); i=1; while(i<=digit) { factorial=factorial*i; i++; } printf("Factorial of %d is %d",digit,factorial); return 0; }

Procedure:

Task 8:

Code(for):

#include <stdio.h> int main() { int overtime, hours; float rate = 1200; float salary; printf("Enter the number of overtime hours: "); scanf("%d", &overtime); for (int i = 1; i <= 10; i++) { hours = overtime * i; salary = rate * hours; printf("Overtime salary of Employee %d is: Rs. %.2f\n", i, salary); } return 0; }

Procedure:

Code(while):

#include<stdio.h> int main() { int overtime_hours, i=1; float overtime_pay; while(i<=10) { printf("Enter the overtime hours of employee %d: ",i); scanf("%d",&overtime_hours); overtime_pay=overtime_hours*1200;

Code(while):

#include <stdio.h>

int main()

int N, M,num;

printf("Enter the starting point N: ");

scanf("%d", &N);

printf("Enter the ending point M: ");

scanf("%d", &M);

printf("Even numbers from %d to %d are \n", N, M);

num=N;

while(num<=M)

if(num % 2 == 0)

printf("%d, ", num);

num++;

return 0;

Procedure:

Task 10:

Code(for):

A:

#include <stdio.h>

int main(){

int i;

printf("**********************\n");

printf(" *******************\n");

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

printf(" ***************\n");

printf(" ***************\n");

printf(" *******************\n");

printf("**********************\n");

return 0;

B:

Critical Analysis:

In this lab I learned how to apply “for and while loop”. The format of both the loops is quite

similar so it becomes easier if we write a program using one to write the other program with

same values and integers but with a bit different pattern. I also learned that the loop will only

terminate when the condition given becomes false, otherwise the loop goes on and on. In for loop

the initializing statement, testing statement and the increment added is written in bracket just

after for, however in while loop only testing statement is written after while, initializing

statement is witten before while and increment at the last (before return 0). Both the loops give

same answers just the way of witting program is a bit different.In this lab I also learned how to

give the conditions to the program so that it will give the desired answer.