Search
 
SCRIPT & CODE EXAMPLE
 

CPP

first and last digit of a number in c++

// Program to find first and last
// digits of a number
#include <bits/stdc++.h>
using namespace std;
 
// Find the first digit
int firstDigit(int n)
{
    // Find total number of digits - 1
    int digits = (int)log10(n);
 
    // Find first digit
    n = (int)(n / pow(10, digits));
 
    // Return first digit
    return n;
}
 
// Find the last digit
int lastDigit(int n)
{
    // return the last digit
    return (n % 10);
}
 
// Driver program
int main()
{
    int n = 98562;
    cout << firstDigit(n) << " "
         << lastDigit(n) << endl;
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: round c++ 
Cpp :: shift element to end of vector c++ 
Cpp :: memset in cpp 
Cpp :: why return 0 in int main 
Cpp :: the difference between i++ and ++i 
Cpp :: c++ function pointer variable 
Cpp :: copy constructor for vector c++ 
Cpp :: array of charcter c++ 
Cpp :: insertion overloading in c++ 
Cpp :: C++ Pointers to Structure 
Cpp :: sort an array in c++ 
Cpp :: use declaration to define a variable 
Cpp :: pause the console c++ 
Cpp :: reverse in vector c++ 
Cpp :: set to vector c++ 
Cpp :: Numbers Histogram in c++ 
Cpp :: gtest assert not equal 
Cpp :: taking integer input from file in c++ 
Cpp :: How do you count the occurrence of a given character in a string? c++ 
Cpp :: Road sign detection and recognition by OpenCV in c 
Cpp :: nmake.exe is not found in the windows 
Cpp :: C++ float and double simple example 
Cpp :: how to find common divisors of two numbers in cpp 
Cpp :: overloading templates in cpp 
Cpp :: array di struct 
Cpp :: cpp stacks 
Cpp :: how to use run total in C++ 
Cpp :: hamming c++ 
Cpp :: c++ define function in header 
Cpp :: C++ Creating a Class Template Object 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =