In C programming, a for loop is a repetition control structure that allows you to execute a block of code repeatedly for a fixed number of times.
for (initializationStatement; testExpression; updateStatement) {
// statements inside the body of loop
}
Here, `initializationStatement` is executed only once at the beginning of the loop. `testExpression` is evaluated before each iteration of the loop. If it evaluates to true, the statements inside the body of the loop are executed. After each iteration, `updateStatement` is executed.
Loops can solve the problem of code repetition by allowing us to execute the same line or group of lines multiple times without explicitly writing each repetition. Here's how a loop can be used to solve the problem in the provided program:
// C program to illustrate the concept of loops #include <stdio.h> int main() { int i,n = 10; // Number of repetitions for ( i = 0; i < n; i++) { printf("Lit Mentor\n"); } return 0; }
Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor Lit Mentor
In this modified program, a for loop is used to print "Lit Mentor" ten times without repeating the printf statement manually. The loop iterates n times (where n is set to 10), and in each iteration, it prints "Lit Mentor" to the console. This approach significantly reduces the length of the code and makes it more concise and manageable.
For loop examples and applications include tasks like iterating through arrays, generating sequences, and more. Below is a sample code snippet for printing numbers from 1 to 10:
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d\t", i); } return 0; }
1 2 3 4 5 6 7 8 9 10
#include <stdio.h> int main() { int i,n; int sum = 0; printf("Enter the number of values you want to input: "); scanf("%d", &n); if (n <= 0) { printf("Invalid input. Please enter a positive number of values.\n"); return 1; // Exit with an error code } for ( i = 1; i <= n; i++) { int value; printf("Enter value %d: ", i); scanf("%d", &value); sum += value; } printf("Sum of the %d values is: %d\n", n, sum); return 0; // Exit with success }
Enter the number of values you want to input: 5 Enter value 1: 6 Enter value 2: -7 Enter value 3: 8 Enter value 4: 2 Enter value 5: -1 Sum of the 5 values is: 8
Range value can't be negative but the values which you want to add can be negative.
Enter the number of values you want to input: -5 Invalid input. Please enter a positive number of values.
#include <stdio.h> int main() { int i, num1, num2; printf("Enter num1: "); scanf("%d", &num1); printf("Enter num2: "); scanf("%d", &num2); if (num1 <= num2) { printf("Numbers in the range from %d to %d:\n", num1, num2); for ( i = num1; i <= num2; i++) { printf("%d\t", i); } printf("\n"); } else { printf("Invalid range. num1 should be less than or equal to num2.\n"); } return 0; }
Enter num1: 10 Enter num2: 20 Numbers in the range from 10 to 20: 10 11 12 13 14 15 16 17 18 19 20
num1 should be less than or equal to num2.
Enter num1: 20 Enter num2: 10 Invalid range. num1 should be less than or equal to num2.
#include <stdio.h> int main() { int i,num; printf("Enter a number for the multiplication table: "); scanf("%d", &num); printf("Multiplication table for %d:\n", num); for ( i = 1; i <= 10; i++) { printf("%d x %d = %d\n", num, i, num * i); } return 0; }
Enter a number for the multiplication table: 3 Multiplication table for 3: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
A classic example of a program that is typically implemented using a for loop is the calculation of the factorial of a number. The for loop is well-suited for this task because it involves a known number of iterations.
#include <stdio.h> int main() { int n,i; long long factorial = 1; printf("Enter a non-negative integer: "); scanf("%d", &n); if (n < 0) { printf("Factorial is not defined for negative numbers.\n"); } else { for ( i = 1; i <= n; i++) { factorial *= i; } printf("Factorial of %d = %lld\n", n, factorial); } return 0; }
Enter a non-negative integer: 4 Factorial of 4 = 24
Factorial of a negative number is not allowed
Enter a non-negative integer: -5 Factorial is not defined for negative numbers.
#include <stdio.h> #include <limits.h> int main() { int i,n, num, max = INT_MIN, min = INT_MAX; printf("Enter the number of elements: \t"); scanf("%d", &n); if (n <= 0) { printf("Invalid input. Please enter a positive number of elements.\n"); return 1; // Exit with an error code } printf("\n"); for (i = 0; i < n; i++) { printf("Enter number %d: ", i + 1); scanf("%d", &num); if (num > max) { max = num; } if (num < min) { min = num; } } printf("\nMaximum value: %d\n", max); printf("Minimum value: %d\n", min); return 0; // Exit with success }
Enter the number of elements: 5 Enter number 1: 5 Enter number 2: 8 Enter number 3: 3 Enter number 4: 6 Enter number 5: 1 Maximum value: 8 Minimum value: 1
#include<stdio.h> void main() { int num, i; printf("Enter a number :\t"); scanf("%d", &num); if (num > 1) { for (i = 2; i < num; i++) { if (num % i == 0) { printf("%d is not a prime number.\n", num); break; } } } if (i == num) { printf("%d is a prime number.\n", num); } }
Enter a number : 7 7 is a prime number.
Enter a number : 55 55 is not a prime number.
#include <stdio.h> int main() { int i, num1, num2, sum = 0; printf("Enter num1: "); scanf("%d", &num1); printf("Enter num2: "); scanf("%d", &num2); if (num1 <= num2) { for ( i = num1; i <= num2; i++) { sum += i; } printf("The sum of numbers in the range from %d to %d is: %d\n", num1, num2, sum); } else { printf("Invalid range. num1 should be less than or equal to num2.\n"); } return 0; }
Enter num1: 5 Enter num2: 10 The sum of numbers in the range from 5 to 10 is: 45
#include <stdio.h> int main() { int i, num1, num2, sum = 0, count = 0; float avg = 0.0; printf("Enter the starting number: "); scanf("%d", &num1); printf("Enter the ending number: "); scanf("%d", &num2); if (num1 <= num2) { for ( i = num1; i <= num2; i++) { count++; sum += i; } printf("The sum of numbers in the range from %d to %d is: %d\n", num1, num2, sum); if (count > 0) { avg = (float)sum / count; printf("The average is: %.2f\n", avg); } else { printf("No numbers in the specified range to calculate the average.\n"); } } else { printf("Invalid range. The starting number should be less than or equal to the ending number.\n"); } return 0; }
Enter the starting number: 1 Enter the ending number: 10 The sum of numbers in the range from 1 to 10 is: 55 The average is: 5.50
You can use nested for loops in C programming to iterate through multiple levels of data structures, such as two-dimensional arrays, to perform repetitive tasks. The basic syntax of a nested for loop is as follows:
for (initialization; condition; update) { // Outer loop code for (inner_initialization; inner_condition; inner_update) { // Inner loop code } // More code in the outer loop }
#include <stdio.h> int main() { int i,j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 4; j++) { printf("%2d ", i * j); } printf("\n"); } return 0; }
1 2 3 4 2 4 6 8 3 6 9 12
#include <stdio.h> int main() { int i,j; for ( i = 1; i <= 10; i++) { for ( j = 1; j <= 10; j++) { printf("%d x %d = %d\n", i, j, i * j); } printf("\n"); // Add a newline to separate rows } return 0; }
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10 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 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50 6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54 6 x 10 = 60 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72 8 x 10 = 80 9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100
In this example, the outer loop iterates from 1 to 10 for the first number in the multiplication, and the inner loop iterates from 1 to 10 for the second number. The result of each multiplication is printed, and a newline is added after each row to format the output.
Nested for loops are commonly used when working with multi-dimensional arrays or when you need to perform repetitive tasks within repetitive tasks. Just make sure to keep track of the loop indices and conditions to control the flow of your program correctly.
#include <stdio.h> #include <math.h> int main() { int num,i,start, end; printf("Enter the starting and ending numbers to find prime numbers: "); scanf("%d %d", &start, &end); if (start < 2) { start = 2; } for ( num = start; num <= end; num++) { int isPrime = 1; for ( i = 2; i * i <= num; i++) { if (num % i == 0) { isPrime = 0; break; } } if (isPrime) { printf("%d is prime\n", num); } } return 0; }
Enter the starting and ending numbers to find prime numbers: 1 10 2 is prime 3 is prime 5 is prime 7 is prime
#include <stdio.h> int main() { char user_choice = 'y'; int number; for (; user_choice == 'y'; ) { printf("Enter a number: "); scanf("%d", &number); printf("Square of %d is %d\n", number, number * number); printf("Would you like to enter another number? (y/n): "); fflush(stdin); scanf(" %c", &user_choice); } return 0; }
Enter a number: 5 Square of 5 is 25 Would you like to enter another number? (y/n): y Enter a number: 8 Square of 8 is 64 Would you like to enter another number? (y/n): n