Knowee
Questions
Features
Study Tools

write a c program to print unique prime numbers in array

Question

write a c program to print unique prime numbers in array

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that prints unique prime numbers from an array:

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num) {
    if (num <= 1) return false;
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) return false;
    }
    return true;
}

bool isUnique(int num, int arr[], int n) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == num) return false;
    }
    return true;
}

int main() {
    int arr[] = {2, 3, 5, 7, 2, 3, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
    int n = sizeof(arr) / sizeof(arr[0]);
    int uniquePrimes[n];
    int m = 0;

    for (int i = 0; i < n; i++) {
        if (isPrime(arr[i]) && isUnique(arr[i], uniquePrimes, m)) {
            uniquePrimes[m++] = arr[i];
        }
    }

    printf("Unique prime numbers in the array are: ");
    for (int i = 0; i < m; i++) {
        printf("%d ", uniquePrimes[i]);
    }

    return 0;
}

This program works as follows:

  1. It defines a helper function isPrime to check if a number is prime.
  2. It defines another helper function isUnique to check if a number is unique in the array of unique primes found so far.
  3. In the main function, it iterates over the input array. For each number, it checks if it is prime and unique. If it is, it adds it to the array of unique primes.
  4. Finally, it prints all the unique primes found.

This problem has been solved

Similar Questions

c program to print unique elements in an array

Write a Program to Find the average of all Prime Numbers from a Given Array?

write easies code for me : Write a program in C to find if a number is prime or not.

Unique PrimeSend FeedbackProblem Statement:Write a program that takes a list of numbers as input and prints all the unique prime numbers from the list in a single line. The program should contain two functions: one to check if a number is prime and another to return the unique values from the list.Input:A list of numbers.Output:The function should return a single line containing the unique prime numbers from the list.Sample Input:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13]Sample Output:2 3 5 7 11 13

Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.