Wednesday

Write a program to ask any n numbers from the user. Sort them in ascending order and display.

#include<stdio.h>
#include<conio.h>
void main()
  {
    int n,i,j,num[10],temp;
    clrscr();
    printf("Enter how many numbers?");
    scanf("%d",&n);
    printf("\nEnter %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 The sorted numbers in ascending order are\n");
   for(i=0;i<n;i++)
    printf("%d\t",num[i]);
 getch();
 }

No comments:

Post a Comment

Rahul-Notes