Assign 7
Assign 7
Write a function which takes a list of integers and its length and returns the
sum of all the prime numbers in the list.
#include <stdio.h>
#include <stdbool.h>
int main() {
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int s = sum_primes(x, 10);
printf("Sum of primes: %d\n", s); // Output: 17
return 0;
}
-----------------------------------------------------------------------------------
-----------------
2. Write a function which takes an unsigned integer and returns the sum of all the
factors of the number.
#include <stdio.h>
int main() {
int s = sum_factors(10);
printf("Sum of factors: %d\n", s); // Output: 18
return 0;
}
3. Write a function which takes an unsigned integer and returns the sum of all the
digits in the number.
#include <stdio.h>
int main() {
int s = sum_digits(1278);
printf("Sum of digits: %d\n", s); // Output: 18
return 0;
}
4. Write a function which takes a string and returns the number of words in the
string. Assume
that there is always one space separating the words and there are no spaces at the
begining
or the end of the string.
#include <stdio.h>
int main() {
int s = count_words("hello world, how are you?");
printf("Number of words: %d\n", s); // Output: 5
return 0;
}
-----------------------------------------------------------------------------------
--------------
5. Write a function to find the hamming distance between the bit representation of
two unsigned
integers. Hamming distance is the number of places the two bit representations
differ. e.g.
#include <stdio.h>
int main() {
printf("Hamming distance: %d\n", hamming_distance(7, 9)); // Output: 3
return 0;
}
-----------------------------------------------------------------------------------
----------------