Loop & Pattern Programs
1. Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
2. Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when count exceeds num
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter a positive integer: 5
Sum = 15
3. Write a C program to check whether a given number is an Armstrong number or not.
#include <stdio.h>
#include <math.h> // Include the math library for the pow function
int main() {
int num, temp, originalNum, remainder, sum = 0, n = 0; // Declare variables
// Prompt the user to input a number
printf("Input a number: ");
scanf("%d", &num);
originalNum = num;
// Calculate the number of digits in the input number
for (temp = num; temp != 0; n++) {
temp /= 10;
}
// Calculate the sum of the nth powers of each digit
for (temp = num; temp != 0; temp /= 10) {
remainder = temp % 10;
sum += pow(remainder, n);
}
// Check if the sum is equal to the original number
if (sum == originalNum) {
printf("%d is an Armstrong number.\n", originalNum);
} else {
printf("%d is not an Armstrong number.\n", originalNum);
}
return 0;
}
4. Write a program in C to find the prime numbers within a range of numbers.
#include <stdio.h> // Include the standard input/output header file.
void main(){
int num, i, ctr, stno, enno; // Declare variables for the number, loop counters, and range.
printf("Input starting number of range: "); // Prompt the user to input the starting number of the
range.
scanf("%d",&stno); // Read the input from the user.
printf("Input ending number of range : "); // Prompt the user to input the ending number of the
range.
scanf("%d",&enno); // Read the input from the user.
printf("The prime numbers between %d and %d are : \n",stno,enno); // Print the message
indicating the range.
for(num = stno; num <= enno; num++) // Loop through the numbers in the specified range.
{
ctr = 0; // Initialize the counter.
for(i = 2; i <= num/2; i++) // Loop through possible divisors.
{
if(num % i == 0) // If a divisor is found...
{
ctr++; // ...increment the counter.
break; // Exit the loop.
}
}
if(ctr == 0 && num != 1) // If no divisors were found and the number is not 1...
printf("%d ",num); // ...print the prime number.
}
printf("\n"); // Move to the next line after printing all prime numbers.
}
Output
Input starting number of range: 1
Input ending number of range : 100
The prime numbers between 1 and 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
5. Write a program in C to display the first n terms of the Fibonacci series.
The series is as follows:
Fibonacci series 0 1 2 3 5 8 13 .....
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int prv=0, pre=1, trm, i, n; // Declare variables for previous, current, and next terms, as well as
loop counters.
printf("Input number of terms to display : "); // Prompt the user to input the number of terms.
scanf("%d", &n); // Read the input from the user.
printf("Here is the Fibonacci series up to %d terms : \n", n); // Print a message indicating the
number of terms.
printf("% 5d % 5d", prv, pre); // Print the first two terms.
for(i=3; i<=n; i++) // Loop to generate the Fibonacci series starting from the third term.
{
trm = prv + pre; // Calculate the next term.
printf("% 5d", trm); // Print the next term.
prv = pre; // Update the previous term.
pre = trm; // Update the current term.
}
printf("\n"); // Move to the next line after printing the series.
}
Output:
Input number of terms to display : 10
Here is the Fibonacci series upto to 10 terms :
0 1 1 2 3 5 8 13 21 34
6. Write a program in C to display a given number in reverse order.
#include <stdio.h> // Include the standard input/output header file.
void main(){
int num, r, sum = 0, t; // Declare variables for the original number, remainder, reversed number, and
temporary variable.
printf("Input a number: "); // Prompt the user to input a number.
scanf("%d", &num); // Read the input from the user.
for(t = num; num != 0; num = num / 10){ // Loop to reverse the digits of the number.
r = num % 10; // Extract the last digit (remainder when divided by 10).
sum = sum * 10 + r; // Build the reversed number by adding the extracted digit.
}
printf("The number in reverse order is : %d \n", sum); // Print the reversed number.
}
Output:
Input a number: 12345
The number in reverse order is : 54321
7. Write a C program to check whether a number is a palindrome or not.
#include <stdio.h> // Include the standard input/output header file.
void main(){
int num, r, sum = 0, t; // Declare variables for the original number, remainder, reversed number, and
temporary variable.
printf("Input a number: "); // Prompt the user to input a number.
scanf("%d", &num); // Read the input from the user.
for(t = num; num != 0; num = num / 10){ // Loop to reverse the digits of the number.
r = num % 10; // Extract the last digit (remainder when divided by 10).
sum = sum * 10 + r; // Build the reversed number by adding the extracted digit.
}
if(t == sum) // Check if the original number and the reversed number are equal.
printf("%d is a palindrome number.\n", t); // Print a message if it is a palindrome.
else
printf("%d is not a palindrome number.\n", t); // Print a message if it is not a palindrome.
}
Output:
Input a number: 121
121 is a palindrome number.
8. Write a C program to print a string in reverse order.
#include <stdio.h> // Include the standard input/output header file.
#include <string.h>
void main()
{
// Variable declarations
char str1[100], tmp;
int l, lind, rind, i;
// Prompting user for input
printf("\n\nPrint a string in reverse order:\n ");
printf("-------------------------------------\n");
// Getting user input
printf("Input a string to reverse : ");
scanf("%s", str1);
// Calculating the length of the string
l = strlen(str1);
// Initializing left and right indices for swapping
lind = 0;
rind = l - 1;
// Loop for swapping characters from left to right
for(i = lind; i< rind; i++)
{
tmp = str1[i];
str1[i] = str1[rind];
str1[rind] = tmp;
rind--;
}
// Printing the reversed string
printf("Reversed string is: %s\n\n", str1);
}
Output:
Print a string in reverse order:
-------------------------------------
Input a string to reverse : Welcome
Reversed string is: emocleW
9. Write a C program that takes input from the user and counts the number of uppercase and
lowercase letters, as well as the number of other characters.
#include <stdio.h> // Include the standard input/output header file.
#include <ctype.h>
int main(void)
{
// Variable declarations and initialization
int chr;
int uppercase_ctr = 0, lowercase_ctr = 0, other_ctr = 0;
// Prompting the user for input
printf("\nInput characters: On Linux systems and OS X EOF is CTRL+D. For Windows EOF is
CTRL+Z.\n");
// Loop to process characters until EOF (End of File) is encountered
while ((chr = getchar()) != EOF)
{
// Checking if the character is uppercase
if (isupper(chr))
uppercase_ctr++;
// Checking if the character is lowercase
else if (islower(chr))
lowercase_ctr++;
// If it's not uppercase or lowercase, consider it as another character
else
other_ctr++;
}
// Printing the counts of different types of characters
printf("\nUppercase letters: %d\n", uppercase_ctr);
printf("Lowercase letters: %d\n", lowercase_ctr);
printf("Other characters: %d\n", other_ctr);
return 0;
}
Output:
Input characters: On Linux systems and OS X EOF is CTRL+D. For Windows EOF is CTRL+Z.
Uppercase letters: 0
Lowercase letters: 9
Other characters: 1
10. Write a program in C to display a pattern like a right angle triangle using an asterisk.
*
**
***
****
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows of asterisks.
for (j = 1; j <= i; j++) // Nested loop to print asterisks based on the current row.
printf("*"); // Print an asterisk.
printf("\n"); // Move to the next line for the next row.
}
}
11. Right Angle Triangle of Asterisks with Alternating Characters on Each Row
#include <stdio.h>
int main() {
int i, j, n;
char ch1 = '*', ch2 = '#';
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
char ch = (i % 2 == 0) ? ch2 : ch1; // Alternate characters
for(j = 1; j <= i; j++) {
printf("%c ", ch);
}
printf("\n");
}
return 0;
}
Example Output (for n = 5):
*
##
***
####
*****
12. Inverted Right Angle Triangle Pattern
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = n; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Example Output (for n = 5):
*****
****
***
**
*
13. Centered Right Angle Triangle Pattern
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n - i; j++) {
printf(" "); // Spaces before stars
}
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Example Output (for n = 5):
*
**
***
****
*****
14. Right Angle Triangle and Its Mirror Image Horizontally
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
// Left triangle
for(j = 1; j <= i; j++) {
printf("*");
}
// Spaces between the triangles
for(j = 1; j <= (n - i) * 2; j++) {
printf(" ");
}
// Right mirror triangle
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Example Output (for n = 5):
* *
** **
*** ***
**** ****
**********
15. Write a C program to display a pattern like a right angle triangle with a number.
1
12
123
1234
#include <stdio.h> // Include the standard input/output header file.
#include <conio.h>
void main() {
int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
printf("%d", j); // Print the value of 'j'.
printf("\n"); // Move to the next line for the next row.
}
getch();
}
16. Right Angle Triangle Pattern with Numbers in Descending Order
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = i; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output (for n = 5):
1
21
321
4321
54321
17. Triangle Pattern Where Each Row Displays the Same Number Repeated and Then the Row
Sum
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
printf("%d ", i);
sum += i;
}
printf("=> Sum = %d", sum);
printf("\n");
}
return 0;
}
Output (for n = 5):
1 => Sum = 1
2 2 => Sum = 4
3 3 3 => Sum = 9
4 4 4 4 => Sum = 16
5 5 5 5 5 => Sum = 25
18. Right Angle Triangle with Consecutive Numbers (Continuing from Previous Row)
#include <stdio.h>
int main() {
int n, num = 1;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}
Output (for n = 5):
1
23
456
7 8 9 10
11 12 13 14 15
19. Right Angle Triangle with Numbers and Row Sum Calculation
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
printf("%d ", j);
sum += j;
}
printf("=> Sum = %d", sum);
printf("\n");
}
return 0;
}
Output (for n = 5):
1 => Sum = 1
1 2 => Sum = 3
1 2 3 => Sum = 6
1 2 3 4 => Sum = 10
12 3 4 5 => Sum = 15
20. Write a program in C to make such a pattern like a right angle triangle with a number
which will repeat a number in a row.
The pattern is as follows:
1
22
333
4444
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, rows; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
printf("%d", i); // Print the value of 'i'.
printf("\n"); // Move to the next line for the next row.
}
}
21. Triangle of Repeated Numbers and Then Output the Total Sum of All Numbers Printed.
#include <stdio.h>
int main() {
int n, totalSum = 0;
printf("Enter number of rows: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", i);
totalSum += i;
}
printf("\n");
}
printf("Total Sum of All Numbers = %d\n", totalSum);
return 0;
}
Output (for n = 5):
1
22
333
4444
55555
Total Sum of All Numbers = 55
22. Write a program in C to make such a pattern like a right angle triangle with the number
increased by 1.
The pattern is as follows :
1
23
456
7 8 9 10
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, rows, k = 1; // Declare variables 'i' and 'j' for loop counters, 'rows' for user input, 'k' for
incrementing numbers.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (j = 1; j <= i; j++) // Nested loop to print numbers based on the current row.
printf("%d ", k++); // Print the value of 'k' and increment it.
printf("\n"); // Move to the next line for the next row.
}
}
23. Write a program in C to make a pyramid pattern with numbers increased by 1.
The pattern is as follows:
1
23
456
7 8 9 10
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, spc, rows, k, t = 1; // Declare variables 'i' and 'j' for loop counters, 'spc' for spaces, 'rows' for user
input, 'k' for loop counter, 't' for incrementing numbers.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
spc = rows + 4 - 1; // Calculate the initial number of spaces.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (k = spc; k >= 1; k--) { // Loop to print spaces before the numbers.
printf(" ");
}
for (j = 1; j <= i; j++) { // Loop to print numbers based on the current row.
printf("%d ", t++); // Print the value of 't' and increment it.
}
printf("\n"); // Move to the next line for the next row.
spc--; // Decrement the number of spaces for the next row.
}
}
24. Write a C program to make such a pattern as a pyramid with an asterisk.
The pattern is as follows :
*
**
***
****
#include <stdio.h> // Include the standard input/output header file.
void main() {
int i, j, spc, rows, k; // Declare variables 'i' and 'j' for loop counters, 'spc' for spaces, 'rows' for user
input, 'k' for loop counter.
printf("Input number of rows : "); // Print a message to prompt user input.
scanf("%d", &rows); // Read the value of 'rows' from the user.
spc = rows + 4 - 1; // Calculate the initial number of spaces.
for (i = 1; i <= rows; i++) { // Start a loop to generate rows.
for (k = spc; k >= 1; k--) { // Loop to print spaces before the asterisks.
printf(" ");
}
for (j = 1; j <= i; j++) { // Loop to print asterisks based on the current row.
printf("* "); // Print an asterisk followed by a space.
}
printf("\n"); // Move to the next line for the next row.
spc--; // Decrement the number of spaces for the next row.
}
}
25. Centered Pyramid of Asterisks and Then Converted into a Diamond Pattern
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows for the pyramid: ");
scanf("%d", &n);
// Upper Pyramid
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++)
printf(" ");
for (int j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
// Lower Inverted Pyramid (to form Diamond)
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--)
printf(" ");
for (int j = 1; j <= (2 * i - 1); j++)
printf("*");
printf("\n");
}
return 0;
}
Example Output (for n = 5):
*
***
*****
*******
*********
*******
*****
***
*
26. Write a C program to calculate the factorial of a given number.
#include <stdio.h> // Include the standard input/output header file.
void main(){
int i, f = 1, num; // Declare variables 'i' for loop counter, 'f' to store factorial, 'num' for user input.
printf("Input the number : "); // Print a message to prompt user input.
scanf("%d", &num); // Read the value of 'num' from the user.
for(i = 1; i <= num; i++) // Start a loop to calculate factorial.
f = f * i; // Calculate factorial.
printf("The Factorial of %d is: %d\n", num, f); // Print the calculated factorial.
}
Output
Input the number : 5
The Factorial of 5 is: 120
27. Write a C program to make such a pattern like a pyramid with a number which will repeat
the number in the same row.
The pattern is as follows:
1
22
333
4444
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int i, j, spc, rows, k; // Declare variables 'i', 'j' for loop counters, 'spc' for spaces, 'rows' for user input, 'k'
for loop counter.
printf("Input number of rows : "); // Prompt the user to input the number of rows.
scanf("%d", &rows); // Read the value of 'rows' from the user.
spc = rows + 4 - 1; // Calculate the initial number of spaces.
for(i = 1; i <= rows; i++) // Loop through each row.
{
for(k = spc; k >= 1; k--) // Loop to print spaces.
{
printf(" ");
}
for(j = 1; j <= i; j++) // Loop to print numbers.
{
printf("%d ", i);
}
printf("\n"); // Move to the next line.
spc--; // Decrease the number of spaces for the next row.
}
}
28. Write a C program to display the pattern as a pyramid using asterisks, with each row
containing an odd number of asterisks.
The pattern is as below:
*
***
*****
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int i, j, n; // Declare variables to store input and control loop indices.
// Prompt the user to input the number of rows for the pattern.
printf("Input number of rows for this pattern :");
scanf("%d", &n); // Read the value of 'n' from the user.
for (i = 0; i <= n; i++) // Loop for the number of rows.
{
for (j = 1; j <= n - i; j++) // Loop to print spaces before the stars.
printf(" ");
for (j = 1; j <= 2 * i - 1; j++) // Loop to print the stars.
printf("*");
printf("\n"); // Move to the next line after printing each row.
}
}
29. Write a program in C to print Floyd's Triangle.
The Floyd's triangle is as below :
1
01
101
0101
10101
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int i, j, n, p, q; // Declare variables to store input and control loop indices.
printf("Input number of rows : "); // Prompt the user for input.
scanf("%d", &n); // Read the value of 'n' from the user.
for (i = 1; i <= n; i++) // Loop for the number of rows.
{
if (i % 2 == 0) // Check if 'i' is even.
{
p = 1;
q = 0;
}
else // If 'i' is odd.
{
p = 0;
q = 1;
}
for (j = 1; j <= i; j++) // Loop for each element in the row.
{
if (j % 2 == 0) // Check if 'j' is even.
printf("%d", p); // Print 'p' if 'j' is even.
else
printf("%d", q); // Print 'q' if 'j' is odd.
}
printf("\n"); // Move to the next line after printing a row.
}
}
30. Write a C program to display the pyramid pattern using the alphabet.
The pattern is as follows :
A
ABA
ABCBA
ABCDCBA
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int i, j; // Declare variables for iteration.
char alph = 'A'; // Initialize a character variable to 'A'.
int n, blk; // Declare variables for user input and block counter.
int ctr = 1; // Initialize a counter.
printf("Input the number of Letters (less than 26) in the Pyramid : ");
scanf("%d", &n); // Prompt user for input and store it in variable 'n'.
for (i = 1; i <= n; i++) // Outer loop for rows.
{
for(blk = 1; blk <= n - i; blk++) // Loop to print spaces before letters.
printf(" ");
for (j = 0; j <= (ctr / 2); j++) // Loop to print letters in ascending order.
{
printf("%c ", alph++);
}
alph = alph - 2; // Decrement the character after printing half.
for (j = 0; j < (ctr / 2); j++) // Loop to print letters in descending order.
{
printf("%c ", alph--);
}
ctr = ctr + 2; // Increment the counter for the next row.
alph = 'A'; // Reset the character to 'A' for the next row.
printf("\n"); // Move to the next line after printing a row.
}
}