1. Return types and basic algorithms
I. Write a program to find the sum of 10 integer numbers inside a separate function called sum() and print the total inside the main function.
II. Improve the above program to find even numbers in a given array.
III. Which parts of the above program (a) needs to be changed to code linear search?
#include<stdio.h>
int sum(){
int arr[10], total = 0;
int i, j;
printf("Enter 10 numbers\n");
for(i=0; i<10; i++){
scanf("%d", &arr[i]);
total += arr[i];
}
printf("Even numbers are: ");
for(j=0; j<10; j++){
if(arr[j] % 2==0){
printf("%d ", arr[j]);
}
}
printf("\n");
return total;
}
int main(){
int total = sum();
printf("Sum is: %d", total);
}
To do linear search, modify the sum function
Comments
Leave a comment