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!!!!
Write a program in C to Determine the following information about a positive integer
|
Newbie Member
|
|
| 17Mar2012,20:27 | #1 |
|
Mentor
|
![]() |
| 18Mar2012,01:21 | #2 |
|
How far have you got and where are you stuck? Do you understand the requirements?
|
|
Newbie Member
|
|
| 18Mar2012,04:16 | #3 |
|
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 |
|
Go4Expert Member
|
|
| 19Mar2012,10:53 | #4 |
|
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;
}
|

