The Program in C to check if a given sequence of values is sorted in increasing order is given below:
#include <stdio.h>
int main() {
int n, i, current, is_sorted = 1;
printf("Enter the number of elements in the sequence: ");
scanf("%d", &n);
printf("Enter the first element of the sequence: ");
scanf("%d", ¤t);
for (i = 1; i < n; i++) {
int next;
printf("Enter the next element of the sequence: ");
scanf("%d", &next);
if (next < current) {
is_sorted = 0;
break;
}
current = next;
}
if (is_sorted) {
printf("The sequence is in increasing order.\n");
} else {
printf("The sequence is not in increasing order.\n");
}
return 0;
}
Output:
Enter the number of elements in the sequence: 5
Enter the first element of the sequence: 98
Enter the next element of the sequence: 91
The sequence is not in increasing order.
Pro-Tips💡
This program prompts the user to enter the number of elements in a sequence, then uses a for loop to iterate through the sequence.
Inside the for loop, it reads each element and compares it to the previous element. If the current element is less than the previous element, the program sets a variable called “is_sorted” to 0 and breaks out of the loop.
After the loop, it checks the value of “is_sorted” and prints whether the sequence is in increasing 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.