How to write a C program which reads integers > 1 from standard input until it r
ID: 3709910 • Letter: H
Question
How to write a C program which reads integers > 1 from standard input until it reaches end-of-input.
-The program prints the integers read which are not divisible by any other of the integers read.
-So it should not print an integer if another integer that has already been read is a factor of that number.
-The program's input is asumed to only contain positive integers ( one per line)
-Can assume that all integers are > 1.
-Can assume at most 1000 integers will be read before end-of-input is reached.
Explanation / Answer
#include <stdio.h>
int main() {
int arr[1000];
int size = 0, num, found, i, j;
while(scanf("%d", &num) != EOF) {
arr[size++] = num;
}
printf("Individual numbers: ");
for(i = 0; i < size; ++i) {
found = 0;
for(j = 0; j < size; ++j) {
if(i != j && arr[i] % arr[j] == 0) {
found = 1;
break;
}
}
if(!found) {
printf("%d ", arr[i]);
}
}
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.