Write a C program that when you type in five numbers, it will print the number from the smallest one to the largest one.(if you type in 2, 1, 3, 5, 4, the output will be 1, 2, 3, 4, 5 ) You may need more than one function to complete this mission.
#include <stdio.h>
//Function declaration
int* sortInAscendingOrder(int arr[]);
int main()
{
//Declaring variable
int i;
//Creating an integer type array of size 5
int arr[5];
/* getting the values entered by the user
* and populate those values into an array
*/
printf("Enter Five numbers(Seperated by space):");
for(i=0;i<5;i++)
{
//Reading each value entered by the user
scanf("%d",&arr[i]);
}
//Calling the function by passing the array as
argument
sortInAscendingOrder(arr);
//Displaying the elements after sorting
printf("Displaying the numbers in Ascending order :");
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
int* sortInAscendingOrder(int arr[])
{
//This Logic will Sort the Array of
elements in Ascending order
int temp,i,j;
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
___________________________
output:
______________Thank You
Get Answers For Free
Most questions answered within 1 hours.