Question: PROGRAM C (CORRECT MY CODE) /* 7. Write a function named sum that accepts an integer n and returns an integer. Declare an integer m
PROGRAM C (CORRECT MY CODE)
/* 7. Write a function named sum that accepts an integer n and returns an integer. Declare an integer m an initialize it to zero. This will accumulate the sum. Write a loop to calculate the sum of the first n integers and print each integer to the terminal. Return m to the calling function. Test your code by calling the function from main. After it runs correctly, comment the call in main and start the next problem: 8.
The output should look like below for function call sum(20):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 sum = 210
The first line above should be printed in the sum function's loop. The second line above should be printed in the main function. */ /* int sum(int n){ int m = 0; for(int i=1;i<=n;i++){ printf("sum = %d",sum(20)); printf(" "); } return m; }
/* 8. Copy the sum function and paste it below, and change the name to sum5. Add code to only calculate the sum of and print integers that are divisible by 5. Test your code by calling the function from main. After it runs correctly, comment the call in main and start the next problem: 9.
The output should look like below for function call sum5(20):
5 10 15 20 sum5 = 50
The first line above should be printed in the sum5 function's loop. The second line above should be printed in the main function. */ /* int sum5(int n){ int m=0; for(int i=1;i<=n;i++){ if(i%5 == 0){ printf("%d ",i); m += i; } } printf(" "); return m; }
/* 9. Copy the sum5 function and paste it below, and change the name to sum57. Add code only calculate the sum of and print integers that are divisible by 5 OR 7. Test your code by calling the function from main. After it runs correctly, comment the call in main and start the next problem: 10.
The output should look like below for function call sum57(20):
5 7 10 14 15 20 sum57 = 71
The first line above should be printed in the sum57 function's loop. The second line above should be printed in the main function. */ /* int sum57(int n){ int m=0; for(int i=1;i<=n;i++){ if(i%5 == 0||i%7==0){ printf("%d ",i); m += i; } } printf(" "); return m; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
