C program to arrange numbers in ascending & descending order if else

C program to arrange numbers in ascending & descending order if else

This page has a C program to arrange numbers in ascending & descending order using if else. This program is designed to display the given numbers in ascending & descending order using if else statement. The program uses if . . . else statement for finding either ascending or descending order. For Example: If the given input numbers are  13 4 87 10 34 -34 28 then the program using if else statement will arrange the given numbers for ascending order as -34  4  10  13  28  34  87 and the program using if else statement will arrange the given numbers for descending order as  87  34  28  13  10  4  -34 .  The below given C program to print the given input numbers in ascending or descending order using if else. Kindly check out the program to arrange numbers in ascending & descending order.


Arrange numbers in ascending & descending order using if else

  #include <stdio.h>
  int main(){
          int nr, p1, q1, select;
          int exchan, ad[100];
          clrscr();
          printf("How much numbers to sort : \n");
          scanf("%d", &nr);
          printf("Give %d  numbers one by one \n", nr);
          for (p1=1; p1<=nr; ++p1)
          scanf("%d", &ad[p1]);
          printf("Select Your Option : 1 for Ascending and 2 Descending \n");
          scanf("%d",&select);
          if(select ==1)
          {
           for (p1=1; p1<=nr-1; ++p1)
           for (q1=p1+1; q1<=nr; ++q1){
           if(ad[p1] > ad[q1])
           {
           exchan = ad[p1];
           ad[p1] = ad[q1];
           ad[q1] = exchan;
           }
           }
           }
           else if(select == 2){
           for (p1=1; p1<=nr-1; ++p1)
           for (q1=p1+1; q1<=nr; ++q1){
           if(ad[p1] < ad[q1])
           {
           exchan = ad[p1];
           ad[p1] = ad[q1];
           ad[q1] = exchan;
           }
           }
           }
           else {
           printf("\n You Entered wrong Option");
           getch();
           exit();
           }
           printf("\n\nThe numbers arranged in order are . . . \n\n");
           for (p1=1; p1<=nr; ++p1)
           printf("%d\n", ad[p1]);
           getch();
           return 0;
           }


Sample Outputs:

How much numbers to sort : 7

Give 7  numbers one by one  4  -2   1  10  5  7   -8

Select Your Option : 1 for Ascending and 2 Descending : 1

The numbers arranged in order are . . .

  -8  

  -2

   1 

   4

   5

   7

   10


Related Popular Posts:

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share