Archive Pages Design$type=blogging

Write a Program in C to Sort a List of Numbers in Ascending Order

In the program below nested  for loop is used to sort a list of  real numbers in ascending order. This program below asks to enter how many ...


Program in C to Sort a List of Numbers in Ascending Order
In the program below nested  for loop is used to sort a list of  real numbers in ascending order. This program below asks to enter how many numbers want to sort and allow to enter the numbers to sort them in ascending order.
The nested for loop in the program swaps the numbers if first number is greater than the next number. i.e. using (a[j]>a[j+1]) and
       c=a[j];
       a[j]=a[j+1];
       a[j+1]=c;
Here the value of a[j] copied to c and value of a[j+1] copied to a[j] and then value of c is copied to a[j+1].



 Program in C to Sort a List of Numbers in Ascending Order



Steps:
  1. Declare an array with float to insert real numbers.
  2. Declare integers i, j, n, c and flag.
  3. Print the message to enter how many numbers want to insert and allow to enter the number.
  4.  Use for() loop to allow to enter the numbers to sort in ascending order.
  5. Use nested for() loop to to sort the list.
  6. Again use for() loop with printf() command to display the numbers on screen.



Code:



#include <stdio.h>
#include <conio.h>

void main()
{
float a[20];
int i,j,n,c,flag;

clrscr();

printf("how many numbers you want to enter:\n");
scanf("%d", &n);

printf("\Enter the numbers :\n");
for(i=0; i<n; i++)
scanf("%f", &a[i]);


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

printf("Stored elements:\n");
for(i=0; i<n; i++)
printf("%f\n", a[i]);
printf("\n");
getch();

}

 



Related Posts:


تعليقات

الاسم

Artificial Intelligence(AI) Backlinking Basic IT Best List Blogging Tips C C# C++ Computer Architecture Computer Fundamental Computer Security Computer/IT Officer Exam CSS Data Mining and Warehousing Data Recovery Tools Data Structure Database Management System E-commerce E-government Internet & Web Designing IT Law IT Tips and Tricks IT Tutorials Java JavaScript Keyword Research Tools MIS Multiple Choice Question (MCQ) Networking Online Earning Online IT Jobs Operating System Oracle Forms and Reports Programming Guide Programming Language SEO Social Networking Sites Software Download Software Engineering System Analysis and Design Top List VB.Net
false
rtl
item
Max5: Write a Program in C to Sort a List of Numbers in Ascending Order
Write a Program in C to Sort a List of Numbers in Ascending Order
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLg_Wpd6duUc0bT40ozYqSUbDIueG4ZSVc0Okq17nYSNB3VY65JDyG9oRkawSDnHWSSJvBCYOAWCMAuohLQSbSk1JAcHraZXrscqa_R04Oow0e3x3zgu_dWoBXPtAHfdIqOzzLKtoGjQqj/s1600/ascending-order.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLg_Wpd6duUc0bT40ozYqSUbDIueG4ZSVc0Okq17nYSNB3VY65JDyG9oRkawSDnHWSSJvBCYOAWCMAuohLQSbSk1JAcHraZXrscqa_R04Oow0e3x3zgu_dWoBXPtAHfdIqOzzLKtoGjQqj/s72-c/ascending-order.png
Max5
https://maxs27.blogspot.com/2014/07/write-program-in-c-to-sort-list-of.html
https://maxs27.blogspot.com/
http://maxs27.blogspot.com/
http://maxs27.blogspot.com/2014/07/write-program-in-c-to-sort-list-of.html
true
1760660989564394152
UTF-8
لم يتم العثور على اى مواضيع شاهد الكل اقرأ المزيد رد احذف الرد حذف بواسطة الرئيسية صفحات مواضيع شاهد الكل نوصى لك التسميات الارشيف بحث لم يتم العثور على ماتبحث عنه عودة للرئيسية الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت الاحد الاثنين الثلاثاء الاربعاء الخميس الجمعة السبت يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر يناير فبراير مارس ابريل مايو يونيو يوليو اغسطس سبتمبر اكتوبر نوفمبر ديسمبر just now 1 دقيقة مضت $$1$$ دقيقة مضت 1 hour ago $$1$$ ساعة مضت الامس $$1$$ يوم مضى $$1$$ اسبوع مضى اكثر من 5 اسابيع مضت