C Programming Using Loops and Nested Loops Class 10

 


Explore the solutions chapter-wise:

SL. No.

Contents

Chapter 1

Introduction to Computer Network

Chapter 2

HTML5 and CSS3

Chapter 3

Database Part - II MySQL

Chapter 4

Introduction to Loops

Chapter 5

Nested Loops in C

Chapter 6

Arrays in C

Chapter 7

Functions in C

Chapter 8

Pointers in C

Chapter 9

Structure in C

Chapter 10

An Introduction to Object Oriented Programming

Chapter 11

Case Studies


C Programming Using Loops – Programs, Examples and Exercises

Loops are one of the most important concepts in C programming. A loop allows a set of instructions to be executed repeatedly without writing the same code multiple times. In C, loops are mainly used to repeat tasks, print patterns, calculate series, and solve mathematical problems efficiently.

In this article, we will learn different types of loops in C along with examples, outputs, and programming exercises.


Types of Loops in C

There are mainly three types of loops in C:

1. For Loop

A for loop is used when the number of iterations is known in advance.

Syntax:

for(initialization; condition; increment/decrement)
{
// statements
}

2. While Loop

A while loop executes a block of code as long as the condition is true.

Syntax:

while(condition)
{
// statements
}

3. Do-While Loop

A do-while loop executes the block of code at least once, even if the condition is false.

Syntax:

do
{
// statements
}
while(condition);

Program 1: Printing Numbers Using All Three Types of Loop

Problem Statement

Write a C program to print numbers using for loop, while loop, and do-while loop.

Program Code

#include <stdio.h>

int main() {
int i;

printf("Using a for loop to print numbers from 1 to 5:\n");

for (i = 1; i <= 5; i++)
{
printf("%d ", i);
}

printf("\n");

printf("Using a while loop to print numbers from 6 to 10:\n");

i = 6;

while (i <= 10)
{
printf("%d ", i);
i++;
}

printf("\n");

printf("Using a do-while loop to print numbers from 11 to 15:\n");

i = 11;

do {
printf("%d ", i);
i++;
} while (i <= 15);

printf("\n");

return 0;
}

Output

Using a for loop to print numbers from 1 to 5:
1 2 3 4 5

Using a while loop to print numbers from 6 to 10:
6 7 8 9 10

Using a do-while loop to print numbers from 11 to 15:
11 12 13 14 15

How It Works

  • The for loop prints numbers from 1 to 5.
  • The while loop prints numbers from 6 to 10.
  • The do-while loop prints numbers from 11 to 15.

This program demonstrates the working of all three loops in a single program.


Program 2: Infinite Loop Example

Problem Statement

Write a C program to create an infinite loop.

Program Code

#include <stdio.h>

int main() {

while (1)
{
printf("This is an infinite loop.\n");
}

return 0;
}

Output

This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
...

How It Works

  • while(1) always remains true.
  • Therefore, the loop never stops unless manually terminated.

An infinite loop is useful in menus, servers, and systems that must run continuously.


Programming Exercises Using Loops

Program 3: Sum of Squares Series

Problem Statement

Write a C program to find the summation of:

1² + 2² + 3² + 4² + ….. + N²

Program Code

#include <stdio.h>

int main() {
int N, sum = 0;

printf("Enter a positive integer N: ");
scanf("%d", &N);

for (int i = 1; i <= N; i++)
{
sum += i * i;
}

printf("The sum of squares from 1² to %d² is: %d\n", N, sum);

return 0;
}

Output

Enter a positive integer N: 5
The sum of squares from 1² to 5² is: 55

How It Works

The loop calculates the square of every number and adds it to sum.


Program 4: Sum of Cubes Series

Problem Statement

Write a C program to find:

1³ + 2³ + 3³ + ….. + N³

Program Code

#include <stdio.h>

int main() {
int i, N, sum = 0;

printf("Enter the value of N: ");
scanf("%d", &N);

for (i = 1; i <= N; i++)
{
int cube = i * i * i;
sum = sum + cube;
}

printf("\nThe summation of cubes is %d\n", sum);

return 0;
}

Output

Enter the value of N: 4
The summation of cubes is 100

How It Works

Each number is cubed and added to the total sum.


Program 5: Product Series

Problem Statement

Write a C program to find:

1×2 + 2×3 + 3×4 + …… + N×(N+1)

Program Code

#include <stdio.h>

int main() {
int N, sum = 0;

printf("Enter a positive integer N: ");
scanf("%d", &N);

for (int i = 1; i <= N; i++)
{
sum += i * (i + 1);
}

printf("The sum of products is: %d\n", sum);

return 0;
}

Output

Enter a positive integer N: 5
The sum of products is: 55

How It Works

The loop multiplies consecutive numbers and adds them to get the final answer.


Program 6: Odd or Even Number Checker

Problem Statement

Write a C program to continuously take a number as input and check whether it is odd or even.

Program Code

#include <stdio.h>

int main() {
int num;

while (1)
{
printf("Enter a number (0 to exit): ");
scanf("%d", &num);

if (num == 0)
{
printf("Exiting the program. Goodbye!\n");
break;
}

if (num % 2 == 0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
}

return 0;
}

Output

Enter a number (0 to exit): 7
7 is an odd number.

Enter a number (0 to exit): 12
12 is an even number.

Enter a number (0 to exit): 0
Exiting the program. Goodbye!

How It Works

  • The loop runs continuously.
  • If user enters 0, the program exits.
  • Otherwise, it checks if the number is divisible by 2.

C Programming Using Nested Loops – Pattern Programs

Nested loops are loops inside another loop. They are mainly used for pattern printing, tables, matrices, and repeated structures.

In this section, we will learn different pattern programs using nested loops in C.


Program 7: Increasing Number Pattern

Problem Statement

Write a C program to display the following pattern:

1
12
123
1234
12345

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}

Output

1
12
123
1234
12345

How It Works

  • The outer loop controls the number of rows.
  • The inner loop prints numbers from 1 to the current row number.
  • Each row prints one additional number.


Program 8: Decreasing Number Pattern

Problem Statement

Write a C program to display the following pattern:

12345
1234
123
12
1

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 5; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}

Output

12345
1234
123
12
1

How It Works

  • The outer loop decreases from 5 to 1.
  • The inner loop prints numbers from 1 up to the current value of i.
  • Each row contains fewer numbers than the previous row.


Program 9: Reverse Star Pattern

Problem Statement

Write a C program to display the following pattern:

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

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 5; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf("*");
}

printf("\n");
}

return 0;
}

Output

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

How It Works

  • The outer loop controls rows.
  • The inner loop prints stars.
  • Number of stars decreases in every row.


Program 10: Increasing Star Pattern

Problem Statement

Write a C program to display the following pattern:

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

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)
{
printf("*");
}

printf("\n");
}

return 0;
}

Output

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

How It Works

  • The outer loop controls rows.
  • The inner loop prints stars equal to row number.
  • Stars increase row by row.


Program 11: Reverse Number Pattern

Problem Statement

Write a C program to display the following pattern:

54321
4321
321
21
1

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 5; i >= 1; i--)
{
for(j = i; j >= 1; j--)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}

Output

54321
4321
321
21
1

How It Works

  • The outer loop decreases row count.
  • The inner loop prints numbers in reverse order.
  • Pattern decreases line by line.


Program 12: Diamond Style Number Pattern

Problem Statement

Write a C program to display the following pattern:

1
12
123
1234
12345
1234
123
12
1

Program Code

#include<stdio.h>

int main()
{
int i, j, k;

for(i = 1; i < 5; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d ", j);
}

printf("\n");
}

for(i = 5; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
printf("%d ", j);
}

printf("\n");
}

return 0;
}

Output

1
12
123
1234
12345
1234
123
12
1

How It Works

  • The first loop prints an increasing pattern.
  • The second loop prints a decreasing pattern.
  • Together, they form a pyramid-like structure.


Loops are an essential part of C programming because they help us execute repeated tasks efficiently. In this article, we learned:

  • Types of loops in C (for, while, do-while)
  • Infinite loops
  • Mathematical series programs
  • Odd/Even checking program
  • Pattern printing using nested loops
  • Number and star patterns

By practicing these programs regularly, students can improve their logic-building and problem-solving skills in C programming.