Saturday, August 9, 2014

Program to sort numbers in ascending and descending order

Simple C program to arrange numbers in an array in ascending and descending order. If you need help, let me know.

#include<stdio.h> main() { int a[50],n,i,j,t; printf("neter the number of numbers in aray"); scanf("%d",&n); printf("enter the number"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=0;j<(n-i)-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("array in ascending"); for(i=0;i<n;i++) { printf("%d",a[i]); } printf("decending"); for(i=0;i<n;i++) { for(j=0;j<(n-i)-1;j++) { if(a[j]<a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } for(i=0;i<n;i++) { printf("%d",a[i]); } printf("\n"); }

No comments:

Post a Comment