First and last digit of an integer in a function and putting at reference location – Computer Sir Ki Class

Login


Lost your password?

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

Login
[lwa]



Solved Problem #CPP#3018 siteicon   siteicon   siteicon  

Problem Statement - First and last digit of an integer in a function and putting at reference location

Write a c++ program using a function with the following prototype –
void first_last_digit(int n,int first,int last);
In this function pass an integer number as n and then find the first and last digit of n. Then update it on locations first and last passed as reference. Assume values to be passed to demonstrate the working of this function in the main routine.

Solution

TC++ #3018

 

Run Output

First and Last Digit of 12345 are 1 & 

void first_last_digit(int n,int &first,int &last)
In this function n is the input number and &first and &last are two locations where the computed values will be put
{
last=n%10;
Modulus by 10 will give the remainder which will always be the last number of an integer.

while(n>=10) { n=n/10; }
Till the number is reduced to a single digit number (n>=10) we will keep removing the last number.

first=n;
After all the last numbers are removed and single digit is left, that would be the first number
}

 

int a=12345,first,last;
Here we decide about the number to be tested and will name two locations first and last when the function will put values.

first_last_digit(a,first,last);
a will be passed to this function and location first and last are passed as reference. After the function executes the variables first and last will contain the desired values.

cout<<“First and Last Digit of “<<a<<” are “<<first<<” & “<<last<<endl;
Values will be printed here.

 

Notes

  • Some programmers prefer to return 0 after calling such function to confirm that function did run till the end. Although the values returned are actually put in the reference location.

Common Errors

  • Student forget to put the & sign while passing the reference variable to a function.
  • Sometimes people also put additional & while function call. For example:  first_last_digit(a,&first,&last) is incorrect while calling.


Share

sunmitra| Created: 25-Jan-2018 | Updated: 29-Nov-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