C program to sort an integer array in Ascending order

Today we write a program to sort an integer array in ascending order, so ascending means number started from lower and end to higher.

For example:

5,9,11,13,14,17,21…

Above example arrange in ascending order and this is very basic that you all know about.

So, before we write our program you should know that  

  • Keywords
  • Array (1-D)
  • Loop (in this program we use ‘for’ loop)

So, now we write a program to sort an integer array in ascending order.

SOURCE CODE:


#include<stdio.h>
int main(){
    int i,j,a,n[10];
    printf("enter number for sorting in Ascending order:");
    for(i=0;i<10;i++){
       scanf("%d",&n[i]);
    }
   for(i=0;i<10;i++){
       for(j=i+1;j<10;j++){
           if(n[i]>n[j]){

               a=n[i];
               n[i]=n[j];
               n[j]=a;
               
           }
       }
   } 
   printf("Ascending order are:");
   for(i=0;i<10;i++){
       printf("%d\n",n[i]);
   } 
   return 0;
}

OUTPUT:

enter number for sorting in Ascending order:12
10
45
34
55
76
23
44
22
89
Ascending order are:10
12
22
23
34
44
45
55
76
89

 In this program we use

Keyword:

 keyword have fixed meaning in c language and these meaning cannot changed. There are almost 32 keywords in c language which has a fixed meaning. Here in this program we use ‘if’ keyword which run when given condition is true.

Array:

Array basically store multiple constant in single variable and it expressed in square bracket for example a[5] means in ‘a’ variable you can store ‘5’ constant.

Array is very useful when we scan (or take input of multiple number) multiple number. in c program three types of array that is

  • 1-Dimension array (1-D)
  • 2-Dimension array (2-D)
  • 3-Dimension array (3-D)

Loop:

Loop is basically perform one task multiple times, until condition is true. For example if you want to print number from 1 to 10.

There are four types of loop in c language

  • While loop
  • Do-while loop
  • For loop
  • Nested-loop

0 Comments