Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

C Program to Reverse an Array

 

๐Ÿ” C Program to Reverse an Array

#include<stdio.h>
int main()
{
    int num, a[100];
    printf("Enter size of the array:\n");
    scanf("%d", &num);
    if(num <= 0)
    {
        printf("!invalid Array Size:\n");
        return 1;
    }
    printf("Enter %d elements:\n", num);
    for(int i = 0; i < num; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("\nBefore reverse:\n");
    for(int i = 0; i < num; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\nAfter reverse:\n");
    for(int i = num - 1; i >= 0; i--)
    {
        printf("%d ", a[i]);
    }
}
  

๐Ÿ“ Explanation:

This program takes an array of integers from the user, displays the original array, and then prints the elements in reverse order. It uses a loop to first store the input values, and then another loop from the end of the array to the beginning for reverse display.

๐Ÿ’ก Sample Output:

Enter size of the array:
5
Enter 5 elements:
10 20 30 40 50

Before reverse:
10 20 30 40 50
After reverse:
50 40 30 20 10
  

๐Ÿ” Keywords:

Reverse array in C, C program for reverse, loop reverse logic, array operations in C, beginner array program

Comments

Popular Posts

๐ŸŒ™