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 Print Descending Number Pattern Using Loops

Descending Number Pattern in C

Descending Number Pattern in C

This C program prints a number pattern where each row contains numbers in ascending order, but the total number of elements decreases with each row. This is done using nested for loops.

✅ C Program Code:


#include <stdio.h>

int main()
{
    int num;
    printf("Enter the number:\n");
    scanf("%d", &num);
    for (int i = num; i >= 1; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d ", j);
        }
        printf("\n");
    }
}
  

💡 Explanation:

  • User Input: The program asks for a number (e.g., 5).
  • Outer Loop: Runs from the input number down to 1.
  • Inner Loop: Prints numbers from 1 to the current value of i.

💻 Sample Output:

Enter the number:
5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

🔍 Keywords:

C pattern program, descending number pattern in C, reverse triangle pattern C, number logic in C, beginner friendly C programs, nested loop patterns

Comments

Popular Posts

🌙