The program to find prime numbers in a given range is discussed here. A number is said to be prime if it is divisible by 1 and the number itself.
@@coding::1@@
The range will be specified as command line parameters. The first command line parameter, N1 which is a positive integer, will contain the lower bound of the range. The second command line parameter N2, which is also a positive integer will contain the upper bound of the range. The program should consider all the prime numbers within the range, excluding the upper bound and lower bound. Print the output in integer format to stdout. Other than the integer number, no other extra information should be printed to stdout. Example Given inputs “7†and “24†here N1 = 7 and N2 = 24, expected output as 83.nnSolution:n#include<stdio.h>nint main(int argc, char *argv[])n{nint N1, N2, j, i, count, sum = 0;nN1 =atoi(argv[1]);nN2 =atoi(argv[2]);nfor(i=N1+1; i<N2; ++i)n{ncount = 0;nfor(j=2; j<=(i/2); j++)n{nif(i%j==0)n{ncount++;nbreak;n}n}nif(count==0)nsum = sum + i;n}nprintf(“%dâ€,sum);nreturn 0;n}n
Time complexity: O(n^2)
@@coding::2@@
Time Complexity: O(n^3/2)
Recommended Programs