Custom Search

Saturday 16 April 2011

C/C++ Program to sort the integer array in descending order, Program to enter 10 numbers & arrange them in descending order.


C/C++ Program to sort the integer array in descending order,  Program to enter 10 numbers & arrange them in descending order.

Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[10],temp=0;
clrscr();
printf("Enter the 10 Numbers:\n");
for(i=0;i<10;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<10;i++)
{
 for(j=0;j<9;j++)
 {
 if(a[j] < a[j+1])
 { temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
 } } }
printf("Array in descending order is:\n");
for(i=0;i<10;i++)
{
 printf("%d ",a[i]);
}
getch();
}


Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

No comments:

Post a Comment

Laptops