Extracting and printing the first and last digit of a number – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#2119 siteicon   siteicon   siteicon  

Extracting and printing the first and last digit of a number

Using a loop technique and successive division by 10 to extract first and last digit of a given number and then printing it.

Learning Objectives

  • Using successive division by 10 and stopping the loop when the first digit is achieved.

Source Code

TC++ #2119


Source Code

Run Output

Give a positive integer: 4356
First Digit = 4
Last Digit = 6

Code Understanding

int nc=n;
We save the input number in a copy so that later operations can be performed on it.

int last_digit=nc%10;
A last digit is simply extracted by modulus division by 10 as we just need the remainder of this division.

Since we need the first digit so we run the loop till only first digit is left which is bound to be less than 10. So the loop continues till >=10.

Here we keep reducing the number by cutting the last digit one by one using integer division by 10.

int first_digit=nc;
We save the number thus achieved in the aptly named variable. This step could be avoided, but sometime neat programming practices require this.

cout<<“First Digit = “<<first_digit<<endl;
cout<<“Last Digit = “<<last_digit<<endl;
As desired we print the first and last digit of the given number.


  • This approach can help us find any digit from a number.

Common Errors

  • Students  make mistakes in putting right comparison sign here nc>=10. It is important to understand for analysis that we need to find just a single digit so the loop should run till more than single digit is available.

Suggested Filename(s): first-last-dig.cpp


sunmitra| Created: 2-Jan-2018 | 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