C Examples

WAP in C to check if given sequence is ascending order or not

The Program in C that checks if given sequence of numbers is in ascending order or not is given below:

#include <stdio.h>

int main() {
    int n, prev, current;
    printf("Enter the number of integers in the sequence: ");
    scanf("%d", &n);
    printf("Enter the first number: ");
    scanf("%d", &prev);
    for (int i = 2; i <= n; i++) {
        printf("Enter the next number: ");
        scanf("%d", &current);
        if (prev > current) {
            printf("The sequence is not in ascending order.\n");
            return 0;
        }
        prev = current;
    }
    printf("The sequence is in ascending order.\n");
    return 0;
}

Output:

Enter the number of integers in the sequence: 15634567
Enter the first number: 1
Enter the next number: 5
Enter the next number: 7
Enter the next number: 6
The sequence is not in ascending order.

Pro-Tips💡

This program prompts the user to enter the number of integers in the sequence and the values of each integer.

It uses a for loop to iterate through the sequence and check if the current number is greater than the previous number.

It compares the current number to the previous number and if it’s greater than previous, it prints that the sequence is not in ascending order and return 0.

If all numbers are in ascending order it will print that the sequence is in ascending order.

It uses variable prev to store the previous number and current to store the current number.

This way the program can compare the previous and current number to check if the sequence is in ascending order or not.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X