Logic:
- Divisor A number is called divisor of another no n if it divides the number completely i.e leaving the remainder 0
- Enter the number n
- Example : n=15
- We will check whether number is divisible by any number from 1 to n here 15.
- If it is divisible by any number (example 15 is divisible by 1, 3, 5 and 15) , then print it.
Algorithm
- Enter the number as n
- for(i=1;i<=n;i++)
- {
- if(n%i==0)
- printf(“%d”,i);
- }
Time Complexity: O(n)
Space Complexity: O(1)
Program
#include <stdio.h>
void printdivisors(n);
int main()
{
printf("****************************************\n");
printf("****************************************\n");
printf("** WAP to print Divisors of a number **\n");
printf("** Created by Sheetal Garg **\n");
printf("** Assistant Professor **\n");
printf("** Phone No:9467863365 **\n");
printf("****************************************\n");
printf("****************************************\n");
int n;
printf("\nEnter no to find divisors\n");
scanf("%d", &n);
printdivisors(n);
}
void printdivisors(n)
{
int i;
printf("\nDivisors of no are\n");
for (i = 1; i <= n; i++)
if (n % i == 0)
printf("%d\t", i);
}
Output
**************************************** **************************************** ** WAP to print Divisors of a number ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************** **************************************** Enter no to find divisors 15 Divisors of no are 1 3 5 15