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

Discussion in 'C' started by saint7308, Mar 17, 2012.

  1. saint7308

    saint7308 New Member

    Joined:
    Mar 17, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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!!!!
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  3. pbeyens

    pbeyens New Member

    Joined:
    Mar 17, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.bytelead.be
    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
     
  4. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    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;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice