Add to Google

Absolute Value In C

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Absolute Value In C

htdefiant
Hey,
I'm writing a program for a friend in Calculus. The goal is for him to be able to input a number, and have the computer return whether it is positive or negative, and its absolute value. Here is what I have so far:
CODE

/* positive.cpp
* The program that gets a number and checks if it is a positive number.
*It prints an appropriate message indicating if it’s positive or negative and also prints the
*absolute value of the number.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main ()

{
int num1;
bool positive;
printf("This program will determine if the user inputted number is positive or negative. \n");
printf("It will also display the number's absolute value. \n");
printf("Please enter a number: \n");
num1=GetInteger ();
if (num1==0)
printf("The number %d is neither positive nor negative. \n", num1);
else
if (num1>0)
{
printf("The number %d is positive and its absolute value is %d. \n", num1, num1);
positive=true;
}
else
{
printf("The number %d is negative and its absolute value is %d. \n", num1, num1);
positive=false;
}
getchar ();
return 0;
}


Obviously the line
CODE
printf("The number %d is negative and its absolute value is %d. \n", num1, num1);

is flawed. How do I fix it so if the number is negative it will display the correct absolute value?

Thank you.

 

 

 


Reply

Tetraca
A function would be what you are looking for. I'm sure there's one in the standard library for it called abs which will change it to an absolute number.

Reply

htdefiant
So what would the line be?
Yes, there is a function called abs.

Something like this:
printf("The number %d is negative and its absolute value is %d. \n", num1, abs:num1);
?

Reply

fffanatics
The correct syntax for that line would be:

printf("The number %d is negative and its absolute value is %d. \n", num1, abs(num1));

also if you wanted to get the absolute value of a floating point number instead you can use fabs(variable).

Reply

htdefiant
Thanks, it worked great.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : absolute


    Looking for absolute, c

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for absolute, c

*MORE FROM TRAP17.COM*
advertisement



Absolute Value In C



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE