Skip to content
The Programming School

The Programming School

  • Home
  • About
  • Contact Us
  • Toggle search form

Loops examples in C Language

Here are some Loops examples in C Language

Working with For Loops

Printing Numbers 1 to 5:

#include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

printf("%d ", i);

}

printf("\n");

return 0;

}

• Initializes a loop variable ' i ' to 1.

• The loop continues as long as ' i ' is less than or equal to 5.

• Increments ' i ' in each iteration.

• Prints the value of ' i ' and a space.

• After the loop, a newline is printed.

Computing Factorial:

#include <stdio.h>

int main() {

int n, factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &n);

for(int i = 1; i <= n; i++) {

factorial *= i;

}

printf("Factorial of %d = %d\n", n, factorial);

return 0;

}

• Calculates the factorial of a positive integer entered by the user.

• Initializes ' factorial ' to 1.

• The loop iterates from 1 to ' n ' and multiplies ' factorial ' by the loop variable in each iteration.

Displaying Multiplication Table:

#include <stdio.h>

int main() {

int num;

printf("Enter a number to display its multiplication table : ");

scanf("%d", &num);

for(int i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", num, i, num * i);

}

return 0;

}

• Displays the multiplication table for a number entered by the user.

• Uses a ' for ' loop to iterate from 1 to 10.

• Prints the multiplication expression and result in each iteration.

Working with While Loops

Countdown:

#include <stdio.h>

int main() {

int count = 5;

while (count > 0) {

printf("%d", count);

count--;

}

printf("Go!\n");

return 0;

}

• Initializes ' count ' to 5.

• Executes the loop while ' count ' in each iteration and prints its value.

• Prints "Go!" after the loop.

Sum of Natural Numbers:

#include <stdio.h>

int main() {

int n, sum = 0, i = 1;

printf("Enter a positive integer : ");

scanf("%d", &n);

while (i <= n) {

sum += i;

i++;

}

printf("Sum of first %d natural numbers = %d\n", n, sum);

return 0;

}

• Calculates the sum of the first ' n ' natural numbers using a ' while ' loop.

• Initializes ' sum ' to 0 and ' i ' to 1.

• Adds the value of ' i ' to ' sum ' in each iteration until ' i ' become greater than ' n ' .

Password Validation:

#include <stdio.h>

int main() {

int password = 1234;

int input;

printf("Enter the password: ");

scanf("%d", &input);

while(input != password) {

printf("Incorrect Password. Try again: ");

scanf("%d", &input);

}

printf("Access granted!\n");

return 0;

}

• Implements a simple password validation using a ' while ' loop.

• Continues prompting the user for the correct password until the entered password matches the predefined password.

Working with Do-While Loop

Menu Driven Program:

#include <stdio.h>

int main() {

int choice;

do {

printf("1. Print Hello\n");

printf("2. Print World\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Hello\n");

break;

case 2:

printf("World\n");

break;

case 3:

printf("Exiting the program\n");

break;

default:

printf("Invalid choice\n");

}

} while (choice != 3);

return 0;

}

• Implements a menu-drive program using a 'do-while' loop.

• Display a menu and prompts the user for a choice.

• Executes the corresponding action based on the user's choice.

• Continues to display the menu until the user chooses to exit (entering 3).

Dice Roll Simulation:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

int dice;

srand(time(NULL));

do {

printf("Press '1' to rool the dice , or enter 0 to exit: ");

scanf("%d", &dice);

if (dice == 1) {

int result = rand() % 6 + 1;

printf("You rolles a %d\n", result);


} else if (dice != 0) {

printf("Please try again.\n");

} else {

printf("Exiting the program\n");

}

} while (dice != 0 );

return 0;

}

• Simulates rolling a six-sided dice using a 'do-while' loop.

• Prompts the user to press Enter to roll the dice or enter 0 to exit.

• Generates a random number between 1 and 6 and displays the result.

• Continues to roll the dice until the user chooses to exit.

Number Guessing Game:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

int secretNumber, guees;

srand(time(NULL));

secretNumber = rand() %% 100 + 1;

do {

printf("Enter your guess or enter 0 to exit: ");

scanf("%d", &guess);

if(guess == 0) {

printf("Exiting the game. The secret number was %d.\n", secretNumber);

} else if (guess < secretNumber) {

printf("Too low! Try again.\n");

} else if (guess > secretNumber) {

printf("Too high! Try again.\n");

} else {

printf("Congratulations! You guessed the secret number (%d). \n", secretNumber);

}

} while (guess != 0 && guess != secretNumber);

return 0;

}

• Implements a simple number guessing game using a 'do-while' loop.

• Generates a random number between 1 and 100 that the user needs to guess.

• Prompts the user to enter their guess or enter 0 to exit.

• Provide feedback on whether the guess in too low, too high, or correct.

• Continues until the user correctly guesses the number or chooses to exit.

  • Home
  • About
  • Contact Us

Copyright © 2025 The Programming School.

Powered by PressBook Masonry Dark