Tuesday, 5 February 2013

Write a c program to accept n numbers from user,store these numbers into an array & sort the number of an array



#include<stdio.h>
#include<conio.h>
   void main()
{
  int a[10],n,i,j,t;
  clrscr();
  printf("\nEnter the n no of array: ");
  scanf("%d",&n);
  printf("\nEnter the array elements: ");
  for(i=0;i<n;i++)
{
  scanf("%d",&a[i]);
}
  for(i=0;i<n;i++)
{
  for(j=i+1;j<n;j++)
{
  a[0]>a[1];
  if(a[i]>a[j])
 {
   t=a[i];
   a[i]=a[j];
   a[j]=t;
  }
}
}
  printf("\nArray in ascending order:\t ");
  for(i=0;i<n;i++)
{
  printf("%d",a[i]);
}
printf("\nArray in descending order:\t ");
  for(i=n-1;i>=0;i--)
{
  printf("%d",a[i]);
}
  getch();
}

//OUTPUT:-
//Enter the n no of array: 4
//Enter the array elements: 4 3 2 1
//Array in ascending order: 1 2 3 4
//Array in descending order: 4 3 2 1

No comments:

Post a Comment