Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Find minimum sum of factors of number
Next article icon

C Program to Find All Factors of Number

Last Updated : 07 Aug, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn to write a C program to print all distinct divisors of a number n. Factors of a number are the positive integers that divide the number without leaving a remainder. For example, for n = 10, the factors will be 1, 2, 5 and 10.

Divisor of Natural Numbers

Note: This problem is different from finding all prime factors.

Recommended Practice
Count Numbers in Range
Try It!

A Simple Solution to find all the factors of a number is to iterate all the numbers from 1 to n check if the current number divides n and print the current number.

C Program to Display Factors of a Number

C




// C implementation of Naive
// method to print all divisors
#include <stdio.h>
  
// Function to print the divisors
void printDivisors(int n)
{
    for (int i = 1; i <= n; i++)
        if (n % i == 0)
            printf("%d ", i);
}
  
// Driver code
int main()
{
    printf("The divisors of 100 are: ");
    printDivisors(100);
  
    return 0;
}
 
 
Output
The divisors of 100 are: 1 2 4 5 10 20 25 50 100 

Complexity Analysis

  • Time Complexity: O(n)
  • Auxiliary Space: O(1)

We can optimize this time complexity to O(sqrt(n)) by observing that all the divisors are present in pairs. For example if n = 100, then the pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10). Refer to the article Find all factors of a Natural Number for O(sqrt(n)) solution.

But this solution will not print the factors in the sorted order. Please refer to the article Find all divisors of a natural number | Set 2 for an O(sqrt(n)) solution that prints divisors in sorted order.



Next Article
C Program to Find minimum sum of factors of number
author
kartik
Improve
Article Tags :
  • C Language
  • C Programs
  • C Misc Programs

Similar Reads

  • C Program for Find sum of odd factors of a number
    Write a C program for a given number n, the task is to find the odd factor sum. Examples: Input : n = 30Output : 24Explanation: Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18Output : 13Explanation: Odd dividers sum 1 + 3 + 9 = 13 C Program for Find sum of odd factors of a number using Prime Factori
    3 min read
  • C Program to Find minimum sum of factors of number
    Write a C program for a given number N, the task is to find the minimum sum of its factors. Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3
    2 min read
  • C/C++ Program to find Product of unique prime factors of a number
    Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples: Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5
    3 min read
  • C Program for Find largest prime factor of a number
    Given a positive integer 'n'( 1 <= n <= 1015). Find the largest prime factor of a number. Input: 6 Output: 3 Explanation Prime factor of 6 are- 2, 3 Largest of them is '3' Input: 15 Output: 5Method 1: The approach is simple, just factorise the given number by dividing it with the divisor of a
    3 min read
  • C Program for efficiently print all prime factors of a given number
    Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". First Approach: Following are the steps to find all prime factors. 1) While n is divi
    5 min read
  • C Program for Number of elements with odd factors in given range
    Given a range [n, m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output :
    2 min read
  • Factorial Program in C
    Given a number N, the task is to find the factorial of it. A factorial is the product of all the natural numbers less than or equal to the given number N. Examples Input: N = 5Output: 120Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120 Input: N = 0Output: 1Explanation: 0! = 1 by definition Find Factorial U
    3 min read
  • GCD of Two Numbers in C
    GCD stands for Greatest Common Divisor and is also known as HCF (Highest Common Factor). The GCD of two numbers is the largest positive integer that completely divides both numbers without leaving a remainder. In this article, we will learn to calculate the GCD of two numbers in the C programming la
    4 min read
  • Number of distinct prime factors of first n natural numbers
    In this article, we study an optimized way to calculate the distinct prime factorization up to n natural number using O O(n*log n) time complexity with pre-computation allowed.Prerequisites: Sieve of Eratosthenes, Least prime factor of numbers till n. Key Concept: Our idea is to store the Smallest P
    9 min read
  • C++ Program To Find All Factors of A Natural Number
    Given a natural number n, print all distinct divisors of it. Examples: Input : n = 10 Output: 1 2 5 10 Input: n = 100 Output: 1 2 4 5 10 20 25 50 100 Input: n = 125 Output: 1 5 25 125 Note that this problem is different from finding all prime factors. Recommended PracticeCount Numbers in RangeTry It
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences