Making a number absolute using If – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1861 siteicon   siteicon   siteicon  

Making a number absolute using If

A number is made absolute by removing its negative sign if present.

Learning Objectives

  • Learning to save original values if selection constructs are likely to change the values given by the user.

Source Code

TC++ #1861

 

Source Code

Run Output

Enter a negative or positive integer:-95
The absolute value of -95 is 95

-OR-
Enter a negative or positive integer:34
The absolute value of 34 is 34

Code Understanding

int n; cout<<“Enter a negative or positive integer:”; cin>>n;
Here we collect a positive or negative integer from the user to make it absolute (which means that the negative sign if present would be removed).

int on=n;
Here we reserve the number given by user in the variable n to another variable on so that the original number given can be printed later.

if(n < 0) n=- n;
Here we remove the sign by negating the negative number. For e.g. if the number is -3 , it would be negated as -(-3) which means 3. A negative number is tested by checking its value to be less than 0.

cout<<“The absolute value of ” <<on<<” is “<<n;
Here we print the original number and the new number. If the original number is already positive, then nothing will happen to the number and it will be printed as it is.

Notes

  • The approach of reserving or saving the original value is frequently used before selection or iteration or loops when they act on the given number for making deductions or changes. This we shall see in the topic of “Tricks for Loops”.
  • A programmer has a choice that subsequent code of selection or loops works on the original number or the saved number itself.


Suggested Filename(s): makeabsolute.cpp



Share

sunmitra| Created: 22-Dec-2017 | Updated: 15-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back