Home » Programming » TCS CodeVita Divine Divisors Program in C with Explanation

# TCS CodeVita Divine Divisors Program in C with Explanation

In this post, we will solve the divine divisors program in C with explanation. This problem was asked in TCS CodeVita competition.

Let’s look at the problem first and then we will discuss how to solve it.

Problem

Find and print all the divisors of a number ‘N’ in increasing order.

Input Format

The first line of input will contain an integer ‘N’ which denotes the number.

Output Format

Print a line containing all the divisors in the increasing order separated by space.

Constraints

1 <= N <= 10^8

Sample input

10

Sample Output

1 2 5 10

## What is a divisor?

A divisor of an integer N, is an integer which can divide the number without leaving a remainder.

For example:

Divisor of 50 are,
1, 2, 5, 10, 25, 50

## How will we solve the Divine Divisors Program?

To solve this, one of the solutions is to iterate over all the numbers starting from 1 to N. Then we will check if the number N is perfectly divisible by the iterating numbers or not. If yes, then we will print it. Since, we are iterating from 1 to N so, the resulting printed output will be in increasing order.

## Algorithm to Solve Divine Divisors Program

Algorithm is given as follows,

Step 1: Start.
Step 2: Take an integer as input in the variable N.
Step 3: Initialize i = 1.
Step 4: Repeat until the i <= N,
Check for the divisor i.e. N % i == 0
print i
i = i + 1;
Step 5: End.

In short,

Start
Take an integer as input in N
i = 1
Repeat until i <= N,
if N % i == 0,
print i
i = i + 1
End

## Program to solve Divine Divisors Problem in C

#include<stdio.h>
int main() {
// variable declaration
long int N, i = 1;

// take input
printf("Enter the number: ");
scanf("%ld", &N);

// find the divisors
while(i <= N) {
// check for divisor
if(N % i == 0)
// print the divisor
printf("%ld ", i);
i++;
}

return 0;
}

Output

Enter the number: 10
1 2 5 10

Explanation

•  At first the user enters an integer number which is stored in the variable “N”.
• Then to check for all the possible divisors we are using while loop,
while(i <= N) {
. . .
}

Here, the loop runs until i <= N which means that we are checking all the numbers from 1 to N.

• Them we check for the divisor using if statement,
if(N % i == 0) {
. . .
}

The condition N % i == 0 means that the given number does not have a remainder when it gets divided by i.

• Finally, we print the number if it is the divisor of the number,
printf("%d ", i);

Here, we are printing extra space ” “ since we have to separate each divisor with a space.

You can also check other Example Programs