0

Write a program using user defined function that will display all prime integers that the user enters

#include <stdio.h> int checkPrime(int number); int main() { int m[6]; int k; printf("Enter 6 numbers\n"); for(k=0;k<6;k++){ scanf("%d", &m[k]); } for(k=0;k<6;k++){ printf("%d\n", checkPrime(m[k])); } return 0; } int checkPrime(int number) { int count = 0; for(int i=2; i<=number/2; i++) { if(number%i == 0) { count=1; break; } } if(number == 1) { count = 1; return number; } } the code has an error please help out

11th Dec 2021, 7:52 PM
Kubasu Ivan
Kubasu Ivan - avatar
1 Resposta
+ 1
Lets assume input is 3,4,5,6,7,8 These get place in m printf("%d\n", checkPrime(m[k])); This line requires a return value of int from int checkPrime(int number) First iteration: int checkPrime(3) { int count = 0; for(int i=2; i<=number/2; i++) { if(number%i == 0) { count=1; break; } } if(number == 1) { count = 1; return number; } } Processing: 2 is greater than 3 / 2 in integer math.. 3 does not equal 1.. the function has no return value creates an errror. Your function should printf if checkprime is true. rewrite it in a: if(checkPrime(int number) == someValueYouChooseToReturn) printf("%d\n", m[k]);
11th Dec 2021, 8:46 PM
William Owens
William Owens - avatar