Question

# C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...

C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED )

Q]. Write a program that displays all the prime numbers in the given array with the following constraint.
Constraint: Only those prime numbers should be displayed whose location is a composite number.
Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0.

Note: Negative integers, 0 and 1, are neither prime nor composite.

Sanity checks required:
The number of input parameters should be in accordance with the exercise number.
You can assume the input parameters to be integers. However, you should verify them for their sign (positive/negative).

Input format: ./a.out [exercise number] [size of the array] [Elements of the array (space separated)]
Output format: Elements separated by space
Print # if there is no element that satisfies the given constraints.

Test Case1:
./a.out 3 9 2 64 7 83 67 7 3 9 -15    (67 and 3 occur at locations 4 and 6, respectively)
67 3
Test Case2
: ./a.out 3 7 5 7 346 1 2 19 17
2 17
Test Case 3
: ./a.out 3 9 10 20 2 2 6 1 0 8 1     (no number will be displayed)
#
Test Case 4: ./a.out 3 4 10 20 2140 2 20 7       (Incorrect number of total arguments)
error

Note the following points.
1] In the test case 1, -15 is not considered for printing since it is not prime.
2] In the test case 2, 5 and 7 are not printed because their locations are 0 and 1, none of which is a composite number.
3 ] In the test case 3, even though 0 and 1 occur at composite locations, they are not printed because they are not prime.

``````

#include <stdio.h>
#include<math.h>

int prime(int n){
// if n<2 return false (0,1 are not primes.)
if(n < 2){
return 0;
}

// Check for factors of a number in range (2,sqrt(n)), as for a
// number to be composite there should be atleast one factor in this range.
for(int i=2;i<=(int)(sqrt(n));i++){
if(n%i == 0){
return 0;
}
}
return 1;
}

int main(int argc, char *argv[])
{
//argc gives number of inputs and char* argv[] stores each input.
// n stores size of array.
int n = atoi(argv[2]);

// I am assuming the excersize number is provided correctly as your
// have not provided the meaning of it.

// Checking if total number of arguments are correctly given.
if(argc != 3 + n){
printf("error\n");
}

else{

// Copying numbers from argv to int array.
// atoi(x) converts string x to integer.
int arr[n];
for(int i=3;i<argc;i++){
arr[i-3] = atoi(argv[i]);

}

// Setting primes_present = 0, which stores number of primes present in composite locations.
int primes_present = 0;
for(int i=2;i<n;i++){
// Checking if number is greater than 0, if it is prime and present in composite location.
// If yes print it.
// Use prime() function to check if a number is prime.
if(arr[i] > 0 && prime(arr[i]) == 1 && prime(i) == 0){
printf("%d ",arr[i]);
primes_present++;
}
}

// If no primes are present in composite locations, print #.
if(primes_present == 0){
printf("#");
}

}

return 0;
}
``````

I have added comments for better understanding. I would love to resolve any queries in the comments.

Please consider dropping an upvote to help a struggling college kid :)

Happy Coding !!

#### Earn Coins

Coins can be redeemed for fabulous gifts.