Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Write a program in C to Determine the following information about a positive integer (http://www.go4expert.com/forums/write-program-c-determine-following-t28046/)

saint7308 17Mar2012 20:27

Write a program in C to Determine the following information about a positive integer
 
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 18Mar2012 01:21

Re: Write a program in C to Determine the following information about a positive inte
 
How far have you got and where are you stuck? Do you understand the requirements?

pbeyens 18Mar2012 04:16

Re: Write a program in C to Determine the following information about a positive inte
 
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 19Mar2012 10:53

Re: Write a program in C to Determine the following information about a positive inte
 
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;
}



All times are GMT +5.5. The time now is 17:53.