Monday, 29 February 2016

Absolute value of a number using C++

The absolute value of 0 is 0. The absolute value of −156 is 156. No Negatives! So in practice "absolute value" means to remove any negative sign in front of a number, and to think of all numbers as positive (or zero).

Source code:

#include<iostream>
using namespace std;


int main()
{
        float num;
cout<<"Enter any number:";
cin>>num;

if(num>0)
    {
        cout<<"The absolute value of number is:"<<num;
    }
else
    {
       cout<<"The absolute value of number is:"<<-(num);
    }
      return 0;

}

No comments:

Post a Comment