Lab Task Ralated To For Loop Solution
Lab Task Ralated To For Loop Solution
Lahore Campus
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
#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;
}
#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;
}
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);
return 0;
}
#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;
}
#include <stdio.h>
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;
return 0;
}
int main() {
int num;
int largest, smallest;
int i;
return 0;
}
#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");
return 0;
}
Exercise 6: Print the following shapes by using for loop
#include <stdio.h>
int main() {
int rows = 5; // Number of rows in the pattern
int i, j;
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
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
return 0;
}
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <stdio.h>
int main() {
int rows, i, j, space;
return 0;
}
* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
int main() {
int rows, i, j, space;
return 0;
}
* * * *
* *
* *
* * * *
#include <stdio.h>
int main() {
int rows, i, j;
return 0;
}
int main() {
int i, j, rows, space;
// Print stars
j = 1;
while (j <= 2 * i - 1) {
printf("*");
j++;
}
printf("\n");
// Print stars
j = 1;
while (j <= 2 * i - 1) {
printf("*");
j++;
}
printf("\n");
i--;
}
return 0;
}