Q:

write a C program using sub-functions , to check whether it is an odd, prime, and perfect square number, and also display its all divisors and factorial

0

write a C program using sub-functions that inputs an integer number n from user in main() and then makes function calls to check and tell whether it is an odd, prime, and perfect square number,

and also display its all divisors and factorial.

-----------

the functions have prototypes as follows:

int isOdd(int n);

int isPrime(int n);

int isPerfectSquare(int n);

int showDivisors(int n);

int factorial(int n); //recursive

------------

sample output:

Enter an integer... : 13

Is it Odd: YES

perfect Square: NO

divisors: 1, 13

factorial: 6227020800

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <math.h>

int isOdd(int n)
{
    if(n%2==0)
    return 0; //odd number
    else return 1;  //even number
}

int isPrime(int n)
{
	for(int i=2;i<=n/2;i++)
	{
		if(n%i!=0)
			continue;
		else
			return 1;
	}
	return 0;
}

int isPerfectSquare(int n)
{
    int iVar;
    float fVar;
 
    fVar=sqrt((double)n);
    iVar=fVar;
 
    if(iVar==fVar)
        return 1;
    else
        return 0;  
}

void showDivisors(int n)
{
    for (int i=1;i<=n;i++)
        if (n%i==0)
            printf("%d ",i);
}

int factorial(int n)
{
  int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}

int main()
{
    printf("Enter an integer... :");
    int number;
	scanf("%d", &number);
	printf("Is it Odd:");
    if(isOdd(number)==1)
    printf("YES");
    else printf("NO");
    
    printf("\nperfect Square:");
    if(isPerfectSquare(number)==1)
    printf("YES");
    else printf("NO");
    
	printf("\ndivisors:");
    showDivisors(number);
    
    printf("\nfactorial: %d",factorial(number));
    
    printf("\nis it prime:");
    if(isPrime(number)==1)
    printf("YES");
    else printf("NO");
    
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now