Write a program in C to Determine the following information about a positive integer

saint7308's Avatar, Join Date: Mar 2012
Newbie Member
Determine the following information about a positive integer that is passed into a function:
(a) is the value a multiple of 7, 11, or 13 (a single yes or no)
(b) is the sum of the digits of the value even or odd
(c) is the value a prime number




Help me Please!!!!
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
How far have you got and where are you stuck? Do you understand the requirements?
pbeyens's Avatar, Join Date: Mar 2012
Newbie Member
for (a) and (b) you can easily use the modulo operator
e.g. to test whether it is a multiple of 7: (x%7)==0
e.g. to test even/odd: (x%2)

Pieter
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
Code:
#include <stdio.h>
#include <math.h>

void test(const int aIntNumber)
{
	// Test for divisibility
	if(aIntNumber % 7 == 0||
	   aIntNumber % 11 == 0||
	   aIntNumber % 13 == 0)
	{
		printf("\n%d is divisible!\n", aIntNumber);
	}
	else
	{
		printf("\n%d is not divisible!\n", aIntNumber);
	}

	// Calculate the sum of the digits
	int anotherIntNum = aIntNumber;
	int sumOfDigit = 0;
	while(anotherIntNum > 0)
	{
		sumOfDigit += anotherIntNum % 10;
		anotherIntNum = anotherIntNum / 10;
	}
	sumOfDigit += anotherIntNum;
	// Test if the sum of digits of number is even or odd
	if(sumOfDigit % 2 == 0)
	{
		printf("\nsum of digits in %d is even\n", aIntNumber);
	}
	else
	{
		printf("\nsum of digits in %d is odd\n", aIntNumber);
	}
	// Test if the number is prime or not
	int s = (int)sqrt((double)aIntNumber);
	if(aIntNumber > 0 && aIntNumber < 4)
	{
		printf("\n%d is prime!\n", aIntNumber);
	}
	else if(aIntNumber < 0)
	{
		printf("\n%d is not a natural number!\n", aIntNumber);
	}
	else if(aIntNumber > 4)
	{
		for(int i = 4; i < s; i++)
		{
			if(aIntNumber % i == 0)
			{
				printf("\n%d is prime!\n", aIntNumber);
				return;
			}
		}
		printf("\n%d is Not prime!\n", aIntNumber);
	}
	else
	{
		printf("\n%d is Not prime!\n", aIntNumber);
	}

}

int main(int argc, char* argv[])
{
    test(13);

    return 0;
}