Program to print Divisors of a number Using Function

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

  1. Enter the number as n
  2. for(i=1;i<=n;i++)
  3. {
  4. if(n%i==0)
  5. printf(“%d”,i);
  6. }

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
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.