Program to Find the Divisors of a Positive Integer [C, C++, Python & Java]

Posted in

Program to Find the Divisors of a Positive Integer [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on February 11, 2025

Problem Statement

We need to write a program that accepts a positive integer value from the user and prints all the possible divisors of that number (excluding the number itself).

For example

Input: 100
Output : 1 2 4 5 10 25 50

Before we move to the algorithm and programs to find the divisors of a positive number, let us first have a brief introduction to what a divisor is.

What is a Divisor?

A number that divides an integer exactly without leaving any remainder is called a divisor.

For example, 2 is the divisor of 10, as 10 ÷ 2 = 5, exactly without leaving no remainder. However, 3 is not the divisor of 10, as 10 ÷ 3 = 3, leaving 1 as a remainder.

Note: A divisor is also a factor of an original integer.

Algorithm

The problem statement is quite straightforward and beginner-level. We only need a loop and a conditional statement to solve this problem. As the problem statement states, all we need to do is find all the divisors except for the number itself.

So we need a loop from range 1 to half of the number entered by the user because a number's all possible divisors can be the number itself and less than half of it.

C Program to Find the Divisors of a Positive Integer

#include <stdio.h>

int main()
{	
	int num, i;
	//input the number
	printf("Enter a positive integer value: ");
	scanf("%d",&num);
	
	for(i=1; i<=num/2; i++)
	{
		if(num%i==0)
		{
			printf("%d ", i);
		}
	}
	

	return 0;
}

Output

Enter a positive integer value: 100
1 2 4 5 10 20 25 50

C++ Program to Find the Divisors of a Positive Integer

#include<iostream>

using namespace std;
int main()
{
	int num;
	
	//input the positive integer
	cout<<"Enter a Positive Integer Number: "; cin>>num;
	
	for(int i=1; i<=num/2; i++)
	{
		if(num%i==0)
		{
			cout<<i<<" ";
		}
	}
	return 0; 
}

Output:

Enter a Positive Integer Number: 100
1 2 4 5 10 20 25 50

Python Program to Find the Divisors of a Positive Integer

# input number
num = int(input("Enter a Positive Integer Number: "))

for i in range(1, (num//2)+1):
    if num%i==0:
        print(i, end =" ")

Output:

Enter a Positive Integer Number: 100
1 2 4 5 10 20 25 50

Java Program to Find the Divisors of a Positive Integer

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
       int num;
       Scanner sc = new Scanner(System.in);

	System.out.println("Enter a Positive Integer Number: "); 
	num = sc.nextInt();
	
	for(int i=1; i<=num/2; i++)
	{
		if(num%i==0)
		{
			System.out.println(+i+ " ");
		}
	}
}
}

Output:

Enter a Positive Integer Number: 
34
1 
2 
17 

Wrapping Up!

Now let's wrap up this programming tutorial on "Write a program to find the divisors of a positive integer". To find all the divisors of a number n , we need to calculate all the smaller numbers that can divide the number n completely.

If we look closely, all the divisors of a number lie between 1 to half of the number itself, saving time, and we need to create a loop that would iterate n/2 time.

People are also reading:

Leave a Comment on this Post

0 Comments