1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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

    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

    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

    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

    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