- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

#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); }

Minimum value of N is: 6

- Related Questions & Answers
- 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++
- Sum of first N natural numbers which are divisible by X or Y
- Find Nth positive number whose digital root is X in C++
- Largest K digit number divisible by X in C++
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- C# Program to find whether the Number is Divisible by 2
- C++ Program for the Largest K digit number divisible by X?
- Count subarrays whose product is divisible by k in C++
- C++ Program for Largest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- How to find whether the Number is Divisible by 2 using C#?
- Find N digits number which is divisible by D in C++
- Smallest number that is divisible by first n numbers in JavaScript

Advertisements