3). In a math class you learn about the different operations that can be done to a series. Imagine the teacher have asked you to take 3 numbers. Arrange them in an increasing order. If the difference between the third number and the second number is less than the difference between second and first, display the square of each number in the series, or if the difference between the third number and the second number is greater than the difference between second and first, display the cube of each number in the series, else display the series. Write a C program to implement the above logic using suitable if statements and appropriate mathematical function. Sample Inpu and Оuгршt: Input 3 numbers::1O 2 30 Sort 2 1o 30 Cube Series:8 1000 27000 Input 3 numbers::12 10 11 Sort 10 11 12 Series is 10 11 12 %3D
using c_compiler


Below is C program with explanation: -
Explanation: -
For the given problem array is used to sort the 3 numbers and then print the series.
- Including the Header file.
- Defining the method Sort(int x_1[3]) to sort the elements.
- Using the nested for -loop to iterate over the array.
- Inside the nested loop, the if-statement is used to compare the adjacent elements.
- This method will return the sorted array.
- Defining the main method.
- Declaring the array arr[3] to store the three elements entered by the user.
- Prompts the user to enter the three elements. Using the for-loop to fill the array.
- Calling the method Sort and passing the inputted array.
- Displaying the sorted elements (sorted array by using the for-loop).
- The if-elseif-else statement is used to check for the different conditions and print the series (square, cube, or series) accordingly.
C program: -
//Including the Header file
#include <stdio.h>
//Defining the method to sort the elements
int Sort(int x_1[3])
{
//Declaring the variables
int i,j,t;
//Nested loop to iterate over the array
for(i = 0; i<3-1; i++)
{
for (j =0; j<3-i-1; j++)
{
//to compare the adjacent elements
if (x_1[j] > x_1[j+1])
{
//swapping the values
t = x_1[j];
x_1[j] = x_1[j+1];
x_1[j+1] = t;
}
}
}
//return the sorted array
return x_1[i];
}
//Defining the main method
int main()
{
//Declaring the array with size 3
int ar[3];
//Prompts the user to enter the elements of an array
printf("Input 3 numbers::");
//Loop to fll the array
for(int i =0; i<3;i++)
{
scanf("%d",&ar[i]);
}
//Calling the sort method and passing the inputted array
Sort(ar);
printf("Sort ");
for(int i =0; i<3; i++)
{
//Displaying the sorted array
printf("%d ",ar[i]);
}
//Printing the squares of the series
if(ar[2]-ar[1] < ar[1]-ar[0])
{
printf("\nSquare Series:");
for(int i=0;i<3;i++)
{
printf("%d ",ar[i]*ar[i]);
}
}
//Printing cubes of series
else if(ar[2]-ar[1] > ar[1]-ar[0])
{
printf("\nCube Series:");
for(int i=0;i<3;i++)
{
printf("%d ",ar[i]*ar[i]*ar[i]);
}
}
//else printing the series
else
{
printf("\nSeries is :");
for(int i=0;i<3;i++)
{
printf("%d ",ar[i]);
}
}
}
Step by step
Solved in 3 steps with 1 images









