C Programming • C Basics, Operators, Loops
C Programming / for — Exercises

for — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program using a for loop to display numbers from 1 to 10.

Practical / Solution

Exercise 1: Print Numbers from 1 to 10

Problem Statement

Write a C program using a for loop to display numbers from 1 to 10.

Learning Outcomes

Understand the basic syntax and working of a for loop.

Hint

Initialize the loop variable with 1 and continue until it reaches 10.

Theory

A for loop is useful when the number of iterations is known in advance. It combines initialization, condition and update in a single statement.

Program

#include <stdio.h>

int main()
{
    int i;

    // Display numbers from 1 to 10
    for (i = 1; i <= 10; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

Expected Output

1 2 3 4 5 6 7 8 9 10

Note

The loop runs exactly 10 times because the condition is checked for each value of i.

Exercise 2: Display Even Numbers from 1 to 20

Problem Statement

Write a C program using a for loop to display all even numbers between 1 and 20.

Learning Outcomes

Use a for loop together with the modulus operator to filter values.

Hint

Check whether each number is divisible by 2 using %.

Theory

An even number gives remainder 0 when divided by 2. The for loop can examine each number in the required range.

Program

#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;
}

Expected Output

2 4 6 8 10 12 14 16 18 20

Note

The condition inside the loop ensures that only even numbers are displayed.

Exercise 3: Sum of First N Natural Numbers

Problem Statement

Write a C program to calculate the sum of the first N natural numbers using a for loop.

Learning Outcomes

Understand accumulation of values inside a loop.

Hint

Start the sum with 0 and add each number from 1 to N.

Theory

An accumulator variable stores the result obtained during repeated iterations of a loop.

Program

#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;
}

Expected Output

Enter N: 5
Sum = 15

Note

For N = 5, the calculation is 1 + 2 + 3 + 4 + 5 = 15.

Exercise 4: Multiplication Table

Problem Statement

Write a C program using a for loop to print the multiplication table of a number entered by the user.

Learning Outcomes

Use a counter-controlled loop for repeated arithmetic operations.

Hint

Run the loop from 1 to 10 and multiply the number by the loop variable.

Theory

A for loop is convenient when a task has a fixed number of repetitions, such as printing ten lines of a multiplication table.

Program

#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;
}

Expected Output

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

Note

The loop executes exactly 10 times.

Exercise 5: Factorial of a Number

Problem Statement

Write a C program to calculate the factorial of a number using a for loop.

Learning Outcomes

Understand repeated multiplication and use of an accumulator.

Hint

Initialize factorial to 1 and multiply it by every number from 1 to N.

Theory

The factorial of a positive integer N is the product of all integers from 1 to N. It is written as N!.

Program

#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;
}

Expected Output

Enter a number: 5
Factorial = 120

Note

5! = 5 × 4 × 3 × 2 × 1 = 120.

Exercise 6: Reverse a Number

Problem Statement

Write a C program to reverse a given number using a for loop.

Learning Outcomes

Practice digit extraction and repeated processing using a loop.

Hint

Extract the last digit using % 10 and remove it using integer division by 10.

Theory

A number can be processed digit by digit. Each extracted digit is added to the reverse in the correct position.

Program

#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;
}

Expected Output

Enter a number: 1234
Reversed number = 4321

Note

The initialization part of the for loop is omitted because the required variables are already initialized.

Exercise 7: Find the Largest Number in a Series

Problem Statement

Write a C program to read N numbers and find the largest number using a for loop.

Learning Outcomes

Use a loop for repeated input and comparison.

Hint

Store the first number as the current largest and compare the remaining numbers with it.

Theory

Repeated comparison can be used to find the maximum value in a collection of numbers.

Program

#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;
}

Expected Output

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

Note

The variable largest always stores the largest value found so far.

Exercise 8: Employee Bonus for 12 Months

Problem Statement

Write a C program using a for loop to enter the bonus received by an employee for 12 months and calculate the total bonus.

Learning Outcomes

Apply a for loop to a practical real-world repetitive calculation.

Hint

Run the loop exactly 12 times and add each month's bonus to the total.

Theory

When the number of repetitions is fixed, a counter-controlled for loop provides a simple way to process each item.

Program

#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;
}

Expected Output

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

Note

This is a practical example of a fixed-count loop, where the number of iterations is known in advance.

Exercise 9: Print a Star Pattern

Problem Statement

Write a C program using nested for loops to print the following pattern:

*
* *
* * *
* * * *
* * * * *

Learning Outcomes

Understand nested for loops and repeated operations at two levels.

Hint

Use one for loop for rows and another for printing stars in each row.

Theory

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}

Program

#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;
}

Expected Output

*
* *
* * *
* * * *
* * * * *

Note

The inner loop runs a different number of times for each row.

Exercise 10: Display Prime Numbers from 1 to N

Problem Statement

Write a C program to display all prime numbers from 1 to N using for loops.

Learning Outcomes

Use nested loops for repeated divisibility checking.

Hint

For every number, check whether it is divisible by any number from 2 up to its square root.

Theory

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.

Program

#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;
}

Expected Output

Enter N: 20
Prime numbers: 2 3 5 7 11 13 17 19

Note

This exercise combines a for loop, nested looping, conditional checking and the break statement.