Write c program to arrange numbers in ascending order using array

Write c program to arrange numbers in ascending order using array

C program to arrange numbers in ascending order using array

 #include<stdio.h>

#include<conio.h>

void main()

{

          int num[100], i, j, temp, n;

          printf("\n Enter how many numbers");

          scanf("%d",&n);

          printf("\n Enter %d numbers",n);

          for(i=0;i<n;i++)

          {

                   scanf("%d",&num[i]);

          }

          for(i=0;i<n;i++)

          {

                   for(j=i+1;j<n;j++)

                   {

                             if(num[i]>num[j])

                             {

                                      temp=num[i];

                                      num[i]=num[j];

                                      num[j]=temp;

                             }

                   }

          }

          printf("\n Printing in assending order");

          for(i=0;i<n;i++)

          {

                   printf("\n %d",num[i]);

          }

          getch();

}

Output



Here users have to give how many numbers to print in ascending order which is store in the variable n. After giving how many numbers user must give numbers to print in ascending order. 

The reason behind declaring num[100]

        Many people may have come to mind num[n] because the number of integers contains in the array is sore in variable num b. But it is absolutely wrong because an array can not declare by any alphabet, symbols, etc. so we give one integer value which is 100.

Also, check 

C program to arrange number in descending order

Click here


Post a Comment

0 Comments