
- 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
Find the first natural number whose factorial is divisible by x in C++
We have to find the first natural number whose factorial is divisible by x. The x is given by the user. So if the x = 16, then output will be 6. as 6! mod 16 = 0. We will use general approach to solve this problem. iteratively count 1!, 2!, …. n! and check divisibility using x. If modulus is 0, then stop and return the number.
Example
#include<iostream> using namespace std; int getNumber(int x) { int fact = 1; int i = 0; while(fact % x != 0){ i++; fact = fact * i; } return i; } int main() { int x = 16; cout << "Minimum value of N is: " << getNumber(x); }
Output
Minimum value of N is: 6
- Related Articles
- PHP program to find the first natural number whose factorial can be divided by a number ‘x’
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Sum of first N natural numbers which are divisible by X or Y
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- C++ program to find first digit in factorial of a number
- Largest K digit number divisible by X in C++
- First digit in factorial of a number in C++
- What is the smallest natural number which is divisible by 25 and 55?
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- C# Program to find whether the Number is Divisible by 2
- Count subarrays whose product is divisible by k in C++
- Find N digits number which is divisible by D in C++
- Smallest number that is divisible by first n numbers in JavaScript

Advertisements