For Statement
The for statement in C is a control flow statement used for looping, allowing you to execute a block of code multiple times. It’s commonly used when the number of iterations is known beforehand. It combines initialization, condition checking, and iteration in a single line.
for Syntax
for (initialization; condition; increment/decrement) {
// Code to execute in each iteration
}
Example:1
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d/n", i);
}
return 0;
}
Output
0
1
2
3
4
Example 2: Sum of Numbers from 1 to N
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Add i to sum
}
printf("The sum of first %d numbers is: %d\n", n, sum);
return 0;
}
Output
Nested for Loop
A nested for loop is a loop inside another loop. The outer loop will run first, and then the inner loop will run for each iteration of the outer loop.This is often used for multidimensional arrays or tables. The syntax for a nested for loop is as follows:
Syntax
#include <stdio.h>
for (initialization1; condition1; increment/decrement1) { // Outer loop
for (initialization2; condition2; increment/decrement2) { // Inner loop
// Code to execute in the inner loop
}
// Code to execute after the inner loop
}
- Explanation
- Outer Loop:
- Executes its body once for every iteration.
- Controls the overall flow and determines how many times the inner loop will run.
- Inner Loop:
- Fully executes for every iteration of the outer loop.
- Executes its block repeatedly based on its own condition.
- Sequence:
- The outer loop starts.
- The inner loop runs to completion for each iteration of the outer loop.
- The outer loop then increments and repeats.
- Outer Loop:
Example 1: Multiplication Table
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) { // Outer loop for rows
for (int j = 1; j <= 5; j++) { // Inner loop for columns
printf("%4d", i * j); // Print the product of i and j
}
printf("\n"); // Move to the next row
}
return 0;
}
Output
We use Nested for loop for printing patterns. Some of the examples are given below:
Example 2: Square Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the square: ");
scanf("%d", &\n);
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= n; j++) { // Inner loop for columns
printf("* ");
}
printf("\n");
}
return 0;
}
Output(for n = 5):
Example 2: Right-Angled Triangle Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for columns
printf("*");
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Output(for n = 5):
Example 3: Pyramid Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= n - i; j++) { // Inner loop for spaces
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) { // Inner loop for stars
printf("*");
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Output(for n = 5):
Example 4: Inverted Pyramid Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = n; i >= 1; i--) { // Outer loop for rows
for (int j = 1; j <= n - i; j++) { // Inner loop for spaces
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) { // Inner loop for stars
printf("*");
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Output(for n = 5):
A diamond pattern is a combination of the pyramid and inverted pyramid patterns. The upper half is a normal pyramid, and the lower half is an inverted pyramid.
Example 5: Diamond Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
// Upper half of the diamond
for (int i = 1; i <= n; i++) { // Outer loop for upper half
for (int j = 1; j <= n - i; j++) { // Inner loop for spaces
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) { // Inner loop for stars
printf("*");
}
printf("\n");
}
// Lower half of the diamond
for (int i = n - 1; i >= 1; i--) { // Outer loop for lower half
for (int j = 1; j <= n - i; j++) { // Inner loop for spaces
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) { // Inner loop for stars
printf("*");
}
printf("\n");
}
return 0;
}
Output(for n = 5):
Number Patterns Using Nested for
Example 6: Simple Number Triangle
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for numbers
printf("%d ", j);
}
printf("\n"); // Newline after each row
}
return 0;
}
Output
Example 7: Repeated Number Pattern.
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) { // Outer loop for rows
for (int j = 1; j <= i; j++) { // Inner loop for numbers
printf("%d ", i);
}
printf("\n"); // Newline after each row
}
return 0;
}
Output
Character Patterns Using Nested for
Example 8: Inverted Character Triangle.
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) {
for (char ch = 'A'; ch < 'A' + i; ch++) {
printf("%c ", ch); // Print characters in decreasing order of rows
}
printf("\n");
}
return 0;
}
Output
Example 9: Pyramid Character Pattern.
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) { // Print spaces
printf(" ");
}
for (char ch = 'A'; ch < 'A' + (2 * i - 1); ch++) { // Print characters
printf("%c ", ch);
}
printf("\n");
}
return 0;
}