write a c program to print unique prime numbers in array
Question
write a c program to print unique prime numbers in array
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:
- It defines a helper function
isPrimeto check if a number is prime. - It defines another helper function
isUniqueto check if a number is unique in the array of unique primes found so far. - In the
mainfunction, 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. - Finally, it prints all the unique primes found.
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
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.