Question

In: Computer Science

Write a C program that takes a double value and rounds it up or off using...

Write a C program that takes a double value and rounds it up or off using ternary expression in the program. Example, if -2.5 is inputted the value should give -3.0

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


Note: If the below code is missing indentation please refer code Images

Typed Code:

/*header files*/
#include <stdio.h>
int main()
{
//initializing double datatype variables
double x,rem,y;
printf("Enter value: ");
//getting input from the user
scanf("%lf",&x);
//storing decimal value to rem
//rem = x - (int)x
//let us assume x = 4.5 ==> 4.5 - (int) 4.5 ==> 4.5 - 4 = 0.5
rem = x - (int)x;
//for positive numbers
//if x >= 0 then condition is true
// rem < 0.5 then y = int(x)
// otherwise y = int(x)+1
//for negative numbers
//if x < 0 then condition is false
// rem > -0.5 then y = int(x)
// otherwise y = int(x)-1
y = (x>=0)?(rem<0.5)?((int)x):((int)x + 1):(rem>-0.5)?((int)x):((int)x - 1);
//printing y
printf("Rounded value: %.1lf",y);
return 0;
}
//code ended here

Program using if else to check positive or negative number and round it up or off:

/*header files*/
#include <stdio.h>
int main()
{
//initializing double datatype variables
double x,rem,y;
printf("Enter value: ");
//getting input from the user
scanf("%lf",&x);
//storing decimal value to rem
//rem = x - (int)x
//let us assume x = 4.5 ==> 4.5 - (int) 4.5 ==> 4.5 - 4 = 0.5
rem = x - (int)x;
//for positive numbers
//if x >= 0 then
if(x>=0)
{
// if rem < 0.5 then y = int(x)
// otherwise y = int(x)+1
y = (rem<0.5)?((int)x):((int)x + 1);
  
}
//for negative numbers
//if x < 0 then
else
{
// if rem > -0.5 then y = int(x)
// otherwise y = int(x)-1
y = (rem>-0.5)?((int)x):((int)x - 1);
}
//printing y
printf("Rounded value: %.1lf",y);
return 0;
}

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
Write a C++ program that takes in a set of daily average temperatures (up to a...
Write a C++ program that takes in a set of daily average temperatures (up to a maximum of 30): 1.Ask the user for a temperature 2.If the user enters a -1 then stop asking for temperatures. 3. After the user is done entering temperatures: a. Print out the temperatures entered. b. print out the average, high and low temperatures. To get average, use: average = (sum of temps) divided by (count of temps) to get max or min, either keep...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT