Check if a pair of numbers is in ascending/descending order
Check if a pair of numbers is in ascending/descending order
Write a C program to check if two numbers in a pair are in ascending order or descending order.
C Code:
#include <stdio.h>
int main() {
int x, y, i, total = 0; // Declare variables for user input and calculations
printf("\nInput a pair of numbers (for example 10,2 : 2,10):");
printf("\nInput first number of the pair: ");
scanf("%d", &x); // Read the first number
printf("\nInput second number of the pair: ");
scanf("%d", &y); // Read the second number
if (x > y) {
printf("The pair is in descending order!"); // If x is greater than y, print message for descending order
} else {
printf("The pair is in ascending order!"); // Otherwise, print message for ascending order
}
printf("\n");
return 0;
}
Sample Output:
Input a pair of numbers (for example 10,2 : 2,10): Input first number of the pair: 10 Input second number of the pair: 2 The pair is in descending order!
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to check if three numbers provided by the user are in strictly increasing order.
- Write a C program to determine if a list of numbers is sorted in non-decreasing (ascending) order.
- Write a C program to verify whether two consecutive numbers in an array are in ascending or descending order.
- Write a C program to check if two numbers are in descending order, including cases with duplicate values.
Go to:
PREV : Sum odd numbers between two given integers.
NEXT : Validate a password (1234 as correct).
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.