Loops and Control Flow Examples
Master loops and control flow statements with these practical C programming examples.
For Loop - Number Pattern
BeginnerCreates a number pattern using nested for loops.
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("\nNumber Pattern:\n");
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}While Loop - Sum of Digits
BeginnerCalculates the sum of all digits in a number using while loop.
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}Do-While Loop - Menu System
BeginnerCreates a menu system that runs until user chooses to exit.
#include <stdio.h>
int main() {
int choice;
do {
printf("\nMenu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Option 3\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("You selected Option 1\n");
break;
case 2:
printf("You selected Option 2\n");
break;
case 3:
printf("You selected Option 3\n");
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 0);
return 0;
}Nested Loops - Multiplication Table
BeginnerGenerates multiplication tables using nested for loops.
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter the number for multiplication table: ");
scanf("%d", &n);
printf("\nMultiplication table for %d:\n", n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
printf("\nFull multiplication table:\n");
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
printf("%4d", i * j);
}
printf("\n");
}
return 0;
}Break Statement - Prime Checker
BeginnerDemonstrates the use of break statement in prime number checking.
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
printf("%d is not a prime number.\n", num);
break; // Exit loop early if factor found
}
}
if (i > num / 2) {
printf("%d is a prime number.\n", num);
}
return 0;
}Continue Statement - Skip Even Numbers
BeginnerUses continue statement to skip even numbers and print only odd numbers.
#include <stdio.h>
int main() {
int i;
printf("Odd numbers from 1 to 20:\n");
for (i = 1; i <= 20; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\n");
return 0;
}Pattern Printing - Pyramid
IntermediateCreates a pyramid pattern using nested loops.
#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("\nPyramid Pattern:\n");
for (i = 1; i <= rows; i++) {
// Print spaces
for (space = 1; space <= rows - i; space++) {
printf(" ");
}
// Print stars
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}Loop with Array - Find Maximum
BeginnerFinds the maximum element in an array using a for loop.
#include <stdio.h>
int main() {
int arr[10], i, max, size;
printf("Enter the number of elements (max 10): ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
max = arr[0]; // Assume first element is maximum
for (i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("Maximum element: %d\n", max);
return 0;
}Mastered Loops? Try More Examples!
Explore our other example categories to continue your C programming journey.