Fun NPTEL Problem on Prime Separation

Complete the C program which takes size of the array and array elements as input and puts the prime and composite elements of the array in two separate arrays (according to their occurrence in the input array) .
For example:
If size of an array is 10 and the elements are 3, 9, 60, 5, 17, 40, 70, 18, 100 and 43 then the output will be

Elements of Prime array: 3 5 17 43
Elements of Composite array: 9 60 40 70 18 100

Use the following print statements

printf(“Elements of Prime array: “);
printArray(Prime, PrimeCount);
printf(“\nElements of Composite array: “);
printArray(Composite, CompositeCount);

Private Test cases used for evaluationInputExpected OutputActual OutputStatus
Test Case 110 6 78 23 49 56 31 51 76 6 5Elements of Prime array: 23 31 5 \n Elements of Composite array: 6 78 49 56 51 76 6Elements of Prime array: 23 31 5 \n Elements of Composite array: 6 78 49 56 51 76 6Passed
#include<stdio.h>
int isPrime(int num);
int main()
{
  int i,j;
/*   j=15;
  for(i=1;i < 10;i++)
    printf("\n %d is divisible by %d because %d",j,i,j%i);
*/
printf("\nEnter a number :");
int num;
scanf("%d",&num);
if(isPrime(num))
  printf("Prime Detected");
else
  printf("NON PRIME");

}

int isPrime(int num)
{
  int i;
  int flagPrime =1;
  if(num ==1)
   return 0; 
  for(i=2;i < num;i++)
  {
    if(num%i == 0)
      flagPrime =0;
  }
  return flagPrime;
}// end isPrime

About Sumant Sumant

I love Math and I am always looking forward to collaborate with fellow learners. If you need help learning math then please do contact me.
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a comment