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

Lab Task Ralated To For Loop Solution

The document contains examples of C programs using for loops to print sequences of numbers, calculate sums and tables, find largest/smallest values, and print shapes. It provides code snippets to solve programming exercises on these topics.

Uploaded by

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

Lab Task Ralated To For Loop Solution

The document contains examples of C programs using for loops to print sequences of numbers, calculate sums and tables, find largest/smallest values, and print shapes. It provides code snippets to solve programming exercises on these topics.

Uploaded by

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

COMSATS University Islamabad,

Lahore Campus

DEPARTMENT OF COMPUTER SCIENCE

Programming Fundamentals
LAB TASK (CSC103)
Learning Objectives: The objective of this exercise is to get you to write, compile and run a
number of simple programs in C, which make use of iteration with the help of loops

Exercise 1: Print the following series using for loop

Print numbers from 1 to 100 with increment of 1

#include <stdio.h>

int main() {
// Using a for loop to print numbers from 1 to 100
int i;
for (i = 1; i <= 100; i++) {
printf("%d\n", i);
}

return 0;
}

Print numbers from 100 to 1 with decrement of 1

#include <stdio.h>

int main() {
// Using a for loop to print numbers from 100 to 1 with decrement of 1
int i;
for (i = 100; i >= 1; i--) {
printf("%d\n", i);
}

return 0;
}

Print numbers from 20 to 2 in steps of -2


#include <stdio.h>

int main() {
// Using a for loop to print numbers from 20 to 2 in steps of -2
int i;
for (i = 20; i >= 2; i -= 2) {
printf("%d\n", i);

CSC103 Programming Fundamentals-Lab Page 2 of 12


}

return 0;
}

Print sequence of numbers: 2, 5, 8, 11, 14, 17, 20

#include <stdio.h>

int main() {
// Using a for loop to print the sequence of numbers: 2, 5, 8, 11, 14, 17, 20
int i;
for (i = 2; i <= 20; i += 3) {
printf("%d\n", i);
}

return 0;
}

Print sequence of numbers: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0

#include <stdio.h>

int main() {
int i;

// Using a for loop to print the sequence of numbers: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0
for (i = 99; i >= 0; i -= 11) {
printf("%d\n", i);
}

return 0;
}

Exercise 2: Code a Program to calculate the sum of first n natural


numbers

#include <stdio.h>

CSC103 Programming Fundamentals-Lab Page 3 of 12


int main() {
int n, i, sum = 0;

// Input the value of n


printf("Enter a positive integer: ");
scanf("%d", &n);

// Calculate the sum of first n natural numbers


for (i = 1; i <= n; i++) {
sum += i;
}

// Print the sum


printf("The sum of first %d natural numbers is: %d\n", n, sum);

return 0;
}

Exercise 3: Write a program that ask the input from user a number and
then print the table of that number.
#include <stdio.h>

int main() {
int number;
int i;

printf("Enter a number: ");


scanf("%d", &number);

printf("Table of %d:\n", number);


for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}

return 0;
}

CSC103 Programming Fundamentals-Lab Page 4 of 12


Exercise 4: Write a program that reads in five integers using for loop and
then determines and prints the largest and smallest integers in the group.
#include <stdio.h>

int main() {
int num;
int largest, smallest;
int i;

printf("Enter integer 1: ");


scanf("%d", &num);
largest = num;
smallest = num;

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


printf("Enter integer %d: ", i);
scanf("%d", &num);

// Check if the entered number is larger than the current largest


if (num > largest) {
largest = num;
}

// Check if the entered number is smaller than the current smallest


if (num < smallest) {
smallest = num;
}
}

// Print the largest and smallest numbers


printf("The largest number is: %d\n", largest);
printf("The smallest number is: %d\n", smallest);

return 0;
}

CSC103 Programming Fundamentals-Lab Page 5 of 12


Exercise 5: A person invests $1000.0 in a savings account yielding 5%
interest. Assuming that all interest is left on deposit in the account,
calculate and print the amount of money in he account at the end of each
year for 10 years. Use the for loop and following formula for
determining these amounts:
a = p(1+r)n
where
p is the original investment(i.e. the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.
Sample output

Year Amount on deposit


1. 1050.00
2. 1102.50
3. 1157.63
4. 1215.51
5. 1276.28
6. 1340.10
7. 1407.10
8. 1477.46
9. 1551.33
10. 1628.89

#include <stdio.h>
#include <math.h>

int main() {
double principal = 1000.0;
double rate = 0.05; // 5% interest rate
double amount;
int year; // Declare loop control variable outside the loop

printf("Year\tAmount on deposit\n");

// Calculate and print amount on deposit for each year


for (year = 1; year <= 10; year++) {
amount = principal * pow(1.0 + rate, year);
printf("%d.\t%.2f\n", year, amount);
}

return 0;
}
Exercise 6: Print the following shapes by using for loop

CSC103 Programming Fundamentals-Lab Page 6 of 12


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>

int main() {
int rows = 5; // Number of rows in the pattern
int i, j;

// Outer loop for rows


for (i = 1; i <= rows; i++) {
// Inner loop for each row
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n"); // Move to the next line after printing each row
}

return 0;
}

*
* *
* * *
* * * *
* * * * *

#include <stdio.h>

int main() {
int rows = 5; // Number of rows in the pattern
int i, j; // Declare loop control variables outside the loop

// Outer loop for rows


for (i = 1; i <= rows; i++) {
// Inner loop for each column in the row
for (j = 1; j <= i; j++) {
printf("* ");
}

CSC103 Programming Fundamentals-Lab Page 7 of 12


printf("\n"); // Move to the next line after printing each row
}

return 0;
}

* * * * *
* * * *
* * *
* *
*

#include <stdio.h>

int main() {
int rows = 5; // Number of rows in the pattern
int i, j; // Declare loop control variables outside the loop

// Outer loop for rows


for (i = rows; i >= 1; i--) {
// Inner loop for each column in the row
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n"); // Move to the next line after printing each row
}

return 0;
}

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

#include <stdio.h>

int main() {
int rows, i, j, space;

// Ask the user to enter the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

CSC103 Programming Fundamentals-Lab Page 8 of 12


// Outer loop for rows
for (i = 1; i <= rows; i++) {
// Print spaces
for (space = 1; space <= rows - i; space++) {
printf(" ");
}

// Inner loop for printing stars


for (j = 1; j <= 2 * i - 1; j++) {
printf("* ");
}

printf("\n"); // Move to the next line after printing each row


}

return 0;
}

* * * * *
* * * *
* * *
* *
*

#include <stdio.h>

int main() {
int rows, i, j, space;

// Ask the user to enter the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

// Outer loop for rows


for (i = rows; i >= 1; i--) {
// Print spaces
for (space = 1; space <= rows - i; space++) {
printf(" ");
}

CSC103 Programming Fundamentals-Lab Page 9 of 12


// Inner loop for printing stars
for (j = 1; j <= i; j++) {
printf("* ");
}

printf("\n"); // Move to the next line after printing each row


}

return 0;
}

* * * *
* *
* *
* * * *

#include <stdio.h>

int main() {
int rows, i, j;

// Ask the user to enter the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

// Outer loop for rows


for (i = 1; i <= rows; i++) {
// Inner loop for columns
for (j = 1; j <= rows; j++) {
// Print '*' if it's first or last row or first or last column, otherwise print ' '
if (i == 1 || i == rows || j == 1 || j == rows)
printf("* ");
else
printf(" ");
}
printf("\n"); // Move to the next line after printing each row
}

return 0;
}

CSC103 Programming Fundamentals-Lab Page 10 of 12


#include <stdio.h>

int main() {
int i, j, rows, space;

printf("Enter number of rows: ");


scanf("%d", &rows);

// Upper part of the diamond


i = 1;
while (i <= rows) {
// Print spaces
space = 1;
while (space <= rows - i) {
printf(" ");
space++;
}

// Print stars
j = 1;
while (j <= 2 * i - 1) {
printf("*");
j++;
}

printf("\n");

CSC103 Programming Fundamentals-Lab Page 11 of 12


i++;
}

// Lower part of the diamond


i = rows - 1;
while (i >= 1) {
// Print spaces
space = 1;
while (space <= rows - i) {
printf(" ");
space++;
}

// Print stars
j = 1;
while (j <= 2 * i - 1) {
printf("*");
j++;
}

printf("\n");
i--;
}

return 0;
}

CSC103 Programming Fundamentals-Lab Page 12 of 12

You might also like