
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
First digit in factorial of a number in C++
In this tutorial, we are going to write a program the finds the first digit of a factorial. Let's see an example.
Input − 7
Output − 5
Let's see the steps to solve the problem.
Initialize the number
Find the factorial of the number.
Divide the number until it becomes a single digit.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void findFirstDigitOfFactorial(int n) { long long int fact = 1; for (int i = 2; i <= n; i++) { fact = fact * i; } while (fact >= 10) { fact = fact / 10; } cout << fact << endl; } int main() { int n = 7; findFirstDigitOfFactorial(n); return 0; }
Output
If you execute the above program, then you will get the following result.
5
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- C++ program to find first digit in factorial of a number
- Maximum number with same digit factorial product in C++
- Find the last digit when factorial of A divides factorial of B in C++
- Sum of the first and last digit of a number in PL/SQL
- Returning number of digits in factorial of a number in JavaScript
- Count of Numbers in Range where first digit is equal to last digit of the number in C++
- Factorial of a large number
- Find the first natural number whose factorial is divisible by x in C++
- Program for factorial of a number in C program
- Function to compute factorial of a number in JavaScript
- Count trailing zeros in factorial of a number in C++
- Find sum of digits in factorial of a number in C++
- Greatest digit of a number in JavaScript
- Check if the first and last digit of the smallest number forms a prime in Python
- How to find the Factorial of a number in Golang?

Advertisements