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", ¤t);
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.