Write a C++ program to find the absolute value of a number entered by the programmer.
This is an example of if-else control statement, It can be used to execute one block of statement at one time among two blocks in C++ language.
if and else are the keyword in C++.
Syntax of if-else statement:
if(condition)
{
statement_1.....n;
// if condition is true then this block is executed
}
else
{
statement_1.....n;
//if condition is false then this block is executed
}
Program Code:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter your number to find its absolute number :";
cin>>x;
if(x>0)
{
cout<<"The absolute value of the given number is :";
cout<<x;
}
else
{
cout<<"The absolute value of the given number is :";
cout<<-(x);
}
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter your number to find its absolute number : -9
The absolute value of the given number is :9
Thanks
Mukesh Rajput
Post A Comment:
0 comments: