Write a C program using a for loop to display numbers from 1 to 10.
Write a C program using a for loop to display numbers from 1 to 10.
Understand the basic syntax and working of a for loop.
Initialize the loop variable with 1 and continue until it reaches 10.
A for loop is useful when the number of iterations is known in advance. It combines initialization, condition and update in a single statement.
#include <stdio.h>
int main()
{
int i;
// Display numbers from 1 to 10
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
The loop runs exactly 10 times because the condition is checked for each value of i.
Write a C program using a for loop to display all even numbers between 1 and 20.
Use a for loop together with the modulus operator to filter values.
Check whether each number is divisible by 2 using %.
An even number gives remainder 0 when divided by 2. The for loop can examine each number in the required range.
#include <stdio.h>
int main()
{
int i;
// Check every number from 1 to 20
for (i = 1; i <= 20; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
}
return 0;
}
2 4 6 8 10 12 14 16 18 20
The condition inside the loop ensures that only even numbers are displayed.
Write a C program to calculate the sum of the first N natural numbers using a for loop.
Understand accumulation of values inside a loop.
Start the sum with 0 and add each number from 1 to N.
An accumulator variable stores the result obtained during repeated iterations of a loop.
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter N: ");
scanf("%d", &n);
// Add numbers from 1 to N
for (i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}
Enter N: 5 Sum = 15
For N = 5, the calculation is 1 + 2 + 3 + 4 + 5 = 15.
Write a C program using a for loop to print the multiplication table of a number entered by the user.
Use a counter-controlled loop for repeated arithmetic operations.
Run the loop from 1 to 10 and multiply the number by the loop variable.
A for loop is convenient when a task has a fixed number of repetitions, such as printing ten lines of a multiplication table.
#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
// Generate multiplication table
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Enter a number: 5 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
The loop executes exactly 10 times.
Write a C program to calculate the factorial of a number using a for loop.
Understand repeated multiplication and use of an accumulator.
Initialize factorial to 1 and multiply it by every number from 1 to N.
The factorial of a positive integer N is the product of all integers from 1 to N. It is written as N!.
#include <stdio.h>
int main()
{
int n, i;
long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
// Calculate factorial
for (i = 1; i <= n; i++)
{
factorial = factorial * i;
}
printf("Factorial = %lld", factorial);
return 0;
}
Enter a number: 5 Factorial = 120
5! = 5 × 4 × 3 × 2 × 1 = 120.
Write a C program to reverse a given number using a for loop.
Practice digit extraction and repeated processing using a loop.
Extract the last digit using % 10 and remove it using integer division by 10.
A number can be processed digit by digit. Each extracted digit is added to the reverse in the correct position.
#include <stdio.h>
int main()
{
int n, digit, reverse = 0;
printf("Enter a number: ");
scanf("%d", &n);
// Process digits until the number becomes zero
for (; n != 0; n = n / 10)
{
digit = n % 10;
reverse = reverse * 10 + digit;
}
printf("Reversed number = %d", reverse);
return 0;
}
Enter a number: 1234 Reversed number = 4321
The initialization part of the for loop is omitted because the required variables are already initialized.
Write a C program to read N numbers and find the largest number using a for loop.
Use a loop for repeated input and comparison.
Store the first number as the current largest and compare the remaining numbers with it.
Repeated comparison can be used to find the maximum value in a collection of numbers.
#include <stdio.h>
int main()
{
int n, i, num, largest;
printf("Enter how many numbers: ");
scanf("%d", &n);
printf("Enter number 1: ");
scanf("%d", &largest);
// Compare remaining numbers
for (i = 2; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num > largest)
{
largest = num;
}
}
printf("Largest = %d", largest);
return 0;
}
Enter how many numbers: 5 Enter number 1: 12 Enter number 2: 45 Enter number 3: 7 Enter number 4: 31 Enter number 5: 20 Largest = 45
The variable largest always stores the largest value found so far.
Write a C program using a for loop to enter the bonus received by an employee for 12 months and calculate the total bonus.
Apply a for loop to a practical real-world repetitive calculation.
Run the loop exactly 12 times and add each month's bonus to the total.
When the number of repetitions is fixed, a counter-controlled for loop provides a simple way to process each item.
#include <stdio.h>
int main()
{
float bonus, total = 0;
int month;
// Read bonus for 12 months
for (month = 1; month <= 12; month++)
{
printf("Enter bonus for month %d: ", month);
scanf("%f", &bonus);
total = total + bonus;
}
printf("Total annual bonus = %.2f", total);
return 0;
}
Enter bonus for month 1: 1000 Enter bonus for month 2: 1200 Enter bonus for month 3: 900 ... Enter bonus for month 12: 1500 Total annual bonus = 13200.00
This is a practical example of a fixed-count loop, where the number of iterations is known in advance.
Write a C program using nested for loops to print the following pattern:
* * * * * * * * * * * * * * *
Understand nested for loops and repeated operations at two levels.
Use one for loop for rows and another for printing stars in each row.
A nested loop is a loop placed inside another loop. It is useful for patterns, matrices, rows and columns, and other multi-level repetition tasks. :contentReference[oaicite:2]{index=2}
#include <stdio.h>
int main()
{
int i, j;
// Outer loop controls rows
for (i = 1; i <= 5; i++)
{
// Inner loop controls stars in each row
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
* * * * * * * * * * * * * * *
The inner loop runs a different number of times for each row.
Write a C program to display all prime numbers from 1 to N using for loops.
Use nested loops for repeated divisibility checking.
For every number, check whether it is divisible by any number from 2 up to its square root.
A prime number has exactly two positive divisors: 1 and itself. A loop can be used to test divisibility for every number in the required range.
#include <stdio.h>
int main()
{
int n, i, j, isPrime;
printf("Enter N: ");
scanf("%d", &n);
printf("Prime numbers: ");
// Check every number from 2 to N
for (i = 2; i <= n; i++)
{
isPrime = 1;
// Check divisibility
for (j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
isPrime = 0;
break;
}
}
if (isPrime)
{
printf("%d ", i);
}
}
return 0;
}
Enter N: 20 Prime numbers: 2 3 5 7 11 13 17 19
This exercise combines a for loop, nested looping, conditional checking and the break statement.