9.4.20

array elements in ascending order


Ascending order array elements
#include <stdio.h>
void main()
{

    int a[10],n,i,j,t,flag;
    printf("How many numbers do u want to enter");
    scanf("%d",&n);
    printf("enter the numbers");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)
    {

        flag=1;
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
                flag=0;

            }
        }
        if(flag==1)
            break;

    } printf("in ascending order\n");
    for(i=0;i<n;i++)
    printf("%d\n",a[i]);
}
Output
How many numbers do u want enter 5
Enter the numbers:2
8
6
3
1
In ascending order
1
2
3
6
8


No comments:

Post a Comment

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...