Write a C C++ program to find absolute value

 

Flowchart

Absolute value of given number flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int n;
printf(“Enter a number: “);
scanf(“%d”,&n);
if(n<0)
{
n = -n;
}
printf(“Absolute value = %d”,n);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<“Enter a number: “;
cin>>n;
if(n<0)
{
n = -n;
}
cout<<“Absolute value = “<<n;
return 0;
}

 

Output

Absolute value of given number output

 

Published by

admin

I am a computer teacher, programmer and web developer