top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Fill array allocated by user with logical order prime numbers

0 votes
336 views

208/5000
Write a program that will fill prime numbers starting with the first prime number within a given range in an array?
* Number is a prime if it is is divisible only by 1 and itself.
* Size of array is user-defined.
* User defines number of elemenets in array.(needs to be dinamicly allocated)
* Print created array necessary at the end.

posted Apr 18, 2017 by Leon Martinović

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Following should solve your problem

#include <stdio.h>

int main(void)
{
 int *myarray;
 int sizeofarray;
 int count=0;

 printf("Enter the size of array(example: 5000)\n");
 scanf("%d", &sizeofarray);

 myarray=calloc(sizeofarray, sizeof(int));

 for (int i=208; i<=5000; i++)
 {
  for (int j=2; j<=i; j++) 
  {
    if (i == j)
    {
      myarray[count++] = i;
      printf("%d\n",i);
    }
    else if (i%j == 0)
      break;
  }
 }
}
answer Apr 19, 2017 by Salil Agrawal
Similar Questions
+1 vote

For Example:
the prime divisors of 450 are 4,5,3,3,2.

+2 votes

it is illegal to share some certain prime numbers in US and if you do so,you will get arrested.Follow this link to get more details :
https://www.youtube.com/watch?v=LnEyjwdoj7g
My question is : it is very easy to find multiplication of two prime numbers but tough to get what are the prime numbers which multiplication results this number . Example : 7*11= 77 but you are given 77 = ? * ? where ?= two prime numbers only (ans = 7 and 11).Another example : 11*13= 143 but you need to solve it for 143 and you output : 11 13
So I need a program ,efficient one, to get solutions to such type of mathematical problem.
Adding more examples:
input:1. 21 -> 3,7
2. 189 -> 3,7,9
3.1363 -> 29,47 .... and if is not possible then print -1 example 1. 4 and its factors 2*2 where 2 is repeated so print -1
2. 9-> -1
3. 24->-1 ( prime factors are 2^3 * 3= 8*3 where 8 is not prime so print -1).
Thanks in advance.

+2 votes

How to find prime numbers between given intervals? Sample code along with the complexity would be helpful?

...