C For Loop Real-Life Examples
Real-Life Examples
A C for loop is a programming construct used to execute a block of code repeatedly, a certain number of times. It's a powerful tool for automating tasks and iterating through data.
Example
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 100; i += 10) {
printf("%d\n", i);
}
return 0;
}
Output
0
10
20
30
40
50
60
70
80
90
100
C program that prints only even values between 0 and 10. Here are three effective methods you can use:
Example
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 10; i = i + 2) {
printf("%d\n", i);
}
return 0;
}
Output
0
2
4
6
8
10
C program that prints the multiplication table for a specified number:
Example
#include <stdio.h>
int main() {
int number = 2;
int i;
// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20