C Program to Reverse the Elements of an Array.

Problem Statement: Given an array of integers, write a C program to reverse the elements of the array in place. 

Note: In-place means without using any additional data structure to store the reverse elements. The original array is updated to contain its elements in reverse order, without creating a new array to hold the reversed elements. 


Example:

Input: arr[] = {10, 20, 30, 40, 50}
Output: Reversed array: {50, 40, 30, 20, 10}

Algorithm to Reverse Array Elements.

Below are the steps that we need to follow:

Step 1: Take the size of the array and create an array arr of that size.
Step 2: Input the elements of the array arr.
Step 3: Initialize two variables, start with 0 and end to the size of the array minus 1.
Step 4: Swap the elements at start and end indexes and increment start and decrement end until the start becomes greater than or equal to the end.
Step 5: Print the reversed array.

Program to Reverse the Elements of an Array.

// C program to reverse array elements in-place 
#include <stdio.h>

// Function to swap two elements
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int n;

    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int arr[n]; 

    // Input the elements of the array
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    int start = 0;
    int end = n - 1;

    while (start < end) {
        swap(&arr[start], &arr[end]);
        start++;
        end--;
    }

    printf("Reversed array: {");
    for (int i = 0; i < n; i++) {
        printf("%d", arr[i]);
        if (i < n - 1) {
            printf(", ");
        }
    }
    printf("}\n");

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
11 10 9 20 8
Reversed array: {8, 20, 9, 10, 11}

Time Complexity: The time complexity of this code is O(n) because we need to traverse through the entire array once to reverse the elements.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to solve this problem.

Reverse the Elements of the Array Using Recursion.

We can also reverse the order of the elements stored in the given array by using the Recursive method. 

Algorithm:
1. Base Case: If the array is empty or has only one element, it is already reversed.
2. Recursive Case:
  • Swap the first and last elements of the array.
  • Recursively reverse the remaining subarray.

C Program to Reverse Array Using Recursion.

// C code to reverse order of an array using recursive method
#include <stdio.h>

// Function to swap two elements
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Recursive function to reverse an array
void reverseArray(int arr[], int start, int end) {
    // Base case: if the array is empty or has only one element
    if (start >= end) {
        return;
    }

    // Swap the first and last elements
    swap(&arr[start], &arr[end]);

    // Recursively reverse the remaining subarray
    reverseArray(arr, start + 1, end - 1);
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, n);

    // Reverse the array
    reverseArray(arr, 0, n - 1);

    printf("Reversed array: ");
    printArray(arr, n);

    return 0;
}
Output:
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
  • Time Complexity: O(n)
  • Space Complexity: O(1)

C Program to Find the Smallest element in an Array.

Problem Statement: Given an array of integers, write a C program to find the smallest element in the array. 

Example:

Input: arr[] = {12, 45, 67, 32, 90, 3}
Output: Smallest element: 3

Input: arr[] = {11, 15, 17, 22, 9, 10}
Output: Smallest element: 9

Algorithm to Find Smallest Element in an Array.

Below are the steps that we need to follow:

Step 1: Initialize a variable min to store the smallest element and set it to the first element of the array.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Input the elements of the array arr.
Step 4: Traverse through the array from the second element to the last element.
  • If the current element is smaller than min, update the value of min to the current element.
Step 5: Print the value of min.

Program to Find the Smallest Element from Given Array.

//C program to find smallest element of the array
#include <stdio.h>

int main() {
    int n, min;

    printf("Enter the size of the array: ");
    scanf("%d", &n);

    int arr[n]; 

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Initialize 'min' to the first element of the array.
    min = arr[0];

    // Traverse to find the smallest element.
    for (int i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    printf("Smallest element: %d\n", min);

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
12 3 9 10 22
Smallest element: 3

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to find the smallest element.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to find the minimum element.

Related articles:

C Program to Find the Largest element in an array.

Problem Statement: Given an array of integers, write a C program to find the largest element in the array.

Example:

Input: arr[] = {12, 45, 67, 32, 90, 3}
Output: Largest element: 90

Input: arr[] = {10, 9, 12, -6, 11}
Output: Largest element: 11

Algorithm to Find Largest Element from Array.

Below are the steps that we need to follow:

Step 1: Initialize a variable max to store the largest element and set it to the first element of the array.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Take the elements of the array arr from the user.
Step 4: Traverse through the array from the second element to the last element.
  • If the current element is greater than max, update the value of max to the current element.
Step 5: Print the value of max.

Program to Find the Largest Element of the Array.

//C program to find largest element of the array
#include <stdio.h>

int main() {
    int n, max;

    printf("Enter the size of the array: ");
    scanf("%d", &n);
    // Creating an array of size 'n'.
    int arr[n]; 

    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Initialize 'max' to the first element of the array.
    max = arr[0];

    // Traverse to find the largest element.
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    printf("Largest element: %d\n", max);

    return 0;
}
Output:
Enter the size of the array: 6
Enter the elements of the array:
11 23 10 31 9 12
Largest element: 31

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to find the largest element.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to find the smallest elements of the input array.

Similar articles:

C Program to Find Sum of Array Elements.

Problem Statement: Given an array of integers, write a C program to find the sum of all elements in the array.


Example:

Input: arr[] = {1, 2, 3, 4, 5}
Output: Sum of elements: 15

Algorithm to find the Sum of Array Elements.

Below are the steps that we need to follow:

Step 1: Initialize a variable sum to 0 to store the sum of elements.
Step 2: Input the size of the array and create an array arr of that size.
Step 3: Input the elements of the array arr.
Step 4: Traverse through the array from the first element to the last element.
  • Add each element to the sum variable.
Step 5: Print the value of the sum.

Program to Find Sum of Array Elements.

//C program to find sum of array elements
#include <stdio.h>

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

    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    // Creating an array of size 'n'.
    int arr[n]; 

    // Input the elements of the array 'arr'.
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // calculate the sum of elements.
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    printf("Sum of elements: %d\n", sum);

    return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array:
12 10 5 2 9
Sum of elements: 38

Time Complexity: The time complexity of this code is O(n) since we need to traverse through the entire array once to calculate the sum.

Space Complexity: The space complexity of this code is O(1) because we are not using any extra space to solve this problem except for storing the elements of the input array.

Similar articles:

C Programming Examples.

Welcome to the world of C programming! If you are just starting your journey in the realm of programming or looking to sharpen your coding skills, you have come to the right place. This article page is dedicated to providing you with a diverse collection of C program examples to practice and master the language.

C Programming Examples List

C Programs - Basic.

👉 Hello World Program in C.

👉 C Program Input/Output Operation.

👉 C Program to Add Two Integer Numbers.

👉 C Program to Find the ASCII value of a Character.

👉 C Program to Swap Two Numbers

👉 C Program to Find Quotient and Remainder.

👉 C Program to Calculate Fahrenheit to Celsius and vice-versa.

👉 C program to Add Two Complex Numbers.

👉 C Program to Find the Size of int, float, char, and double.

👉 C Program to Calculate Simple Interest.

👉 C Program to Calculate Compound Interest.

👉 C Program to Find Area of a Circle.


C Program - Control Follow.

👉 C Program to Find Greatest Number Among Three.

👉 C Program to Check Whether the Number is Even or Odd.

👉 C Program to Count Vowel and Consonant.

👉 C Program to Check Leap Year.

👉 C Program to Print Sum of Natural Numbers.

👉 C Program to Find Factorial of a Number.

👉 C Program to Print Fabanocci Series.

👉 C Program to Check Armstrong Number.

👉 C Program to Check Palindrome Number.

👉 C Program to Check if a number is prime or not.

👉 C Program to Find GCD for two numbers.

👉 C Program to Reverse a Number.

👉 C Program to Create a Calculator.

👉 C Program to Generate Multiplication Table.


Conversion Programs in C.

👉 C Program to Convert Binary to Octal.

👉 C Program to Convert Octal to Binary.

👉 C Program to Convert Binary to Decimal.

👉 C Program to Convert Octal to Decimal.


Patterns Programs in C.

👉 C Program to Print Pyramid Patterns.

👉 C Program to Print Dymond Patterns.


C Program - Arrays.

👉 C Program to Find Sum of Array Elements.

👉 C Program to Find the Largest Element in an array.

👉 C Program to Find the Smallest Element in an Array.

👉 C Program to Reverse the Elements of an Array.

👉 C Program to Search for a Given Element in an Array.

👉 C Program to Count Number of Odd and Even Elements in an Array.

👉 C Program to Find and Display duplicate elements in an array.

👉 C Program to Calculate Average of All the Elements of an Array.


C Program - String.

👉 C Program to Find Length of a String.

👉 C Program to Copy One String to Another String.

👉 C Program to concatenate two strings.

👉 C Program to Compare Two Strings.

👉 C Program to Reverse Given String.

C Program to Check Palindrome Number.

In this C programming tutorial, we will learn how to check if the given number is a palindrome number or not. Before moving to the code section let us understand what is Palindrome number?

Palindrome Number.

A palindrome number is a number that reads the same backward as forward. In other words, the digits of a palindrome number remain unchanged when read from left to right and from right to left. 

Example: 121, 454, and 12321 are palindrome numbers.

Algorithm to Check Palindrome Number.

Step 1: Input the number from the user.
Step 2: Create a copy of the original number to compare later.
Step 3: Initialize variables to store the reverse of the number and the remainder.
Step 4: Use a loop to reverse the digits of the number:
  • a) Extract the last digit of the number using the modulus (%) operator.
  • b) Add the last digit to the reversed number after moving the previous digits one place left using multiplication (*= 10).
  • c) Remove the last digit from the original number using integer division ( /= 10).
  • d) Repeat the above steps until the original number becomes 0.
Step 5: Compare the reversed number with the copy of the original number.
Step 6: If the reversed number is equal to the original number, it is a palindrome. Otherwise, it is not.

Program to Check Palindrome Number.

// c code implementation to check palindrome number
#include <stdio.h>

int main() {
    int num, originalNum, reversedNum = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &num);

    originalNum = num;

    // Reverse the number
    while (num != 0) {
        remainder = num % 10;         
        reversedNum = reversedNum * 10 + remainder;   
        num /= 10;                    
    }

    // Compare with the original number
    if (reversedNum == originalNum) {
        printf("%d is a palindrome number.\n", originalNum);
    } else {
        printf("%d is not a palindrome number.\n", originalNum);
    }

    return 0;
}
Output:
Enter an integer: 12321
12321 is a palindrome number.

Time Complexity: O(n) where n is the number of digits present in the given number.
Space Complexity: O(1) no extra space is required to solve this problem.

Similar articles:

C Program to Print Diamond Patterns.

In this C programming tutorial, we will explore how to print diamond patterns using loops. 


The diamond pattern consists of two inverted right-angled triangles joined at the base. These patterns are a classic example of nested loops, where we use loops inside loops to control the pattern's structure.


Here we are going to print two different types of Diamond Patterns:

Pattern 1: Diamond Pattern with Asterisks.

C Code:

//C program to print diamond patterns
#include <stdio.h>

int main() {
    int rows, i, j, space;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper half of the diamond
    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (i = rows - 1; i >= 1; i--) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *

Pattern 2: Hollow Diamond Pattern.

C Code:

// C program to print hollow diamond shape
#include <stdio.h>

int main() {
    int rows, i, j, space;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper half of the diamond
    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (i = rows - 1; i >= 1; i--) {
        for (space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 6
     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *

C Program to Print Pyramid Patterns.

Patterns help us understand valuable insights into the logic and control flow required in programming. In this C programming tutorial, we are going to learn how to print different kinds of Pyramid patterns.

Here we are going to see 6 different kinds of Pyramid patterns with a C code for each of them:

Pattern 1: Left-aligned Pyramid Pattern.

Below is the C program to print a left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    printf("\nPattern 1:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
Pattern 1:
* 
* * 
* * * 
* * * * 
* * * * * 


Pattern 2: Inverted Left-aligned Pyramid Pattern.

Below is the C program to print an Inverted left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

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

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
* * * * * 
* * * * 
* * * 
* * 
* 

Pattern 3: Center-aligned Pyramid Pattern.

Below is the C program to print a Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

Pattern 4: Inverted Center-aligned Pyramid Pattern.

Below is the C program to print an Inverted Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

    printf("Enter the number of rows for the pyramid pattern: ");
    scanf("%d", &n);

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 6
* * * * * * * * * * * 
  * * * * * * * * * 
    * * * * * * * 
      * * * * * 
        * * * 
          * 


Pattern 5: Right-aligned Pyramid Pattern.

Below is the C program to print a Right-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    *
   **
  ***
 ****
*****


Pattern 6: Number Pyramid Pattern.

Below is the C program to print a Number pyramid pattern.
#include <stdio.h>

int main() {
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (int j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    1
   121
  12321
 1234321
123454321

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson