A fun thing to do in these exercises is to create a separate function and call it in main. That way, you can have your own library of functions and reuse it if you need. Note that these are suggestions, there are many ways to solve these problems.
Answer:
#include <stdio.h>
int sum(int v, int w, int x, int y, int z);
float average(int v, int w, int x, int y, int z);
int main (void) {
long int a, b, c, d, e;
// prompt user
("Please enter five integers.\n");
printf// getting the numbers
("%ld %ld %ld %ld %ld", &a, &b, &c, &d, &e);
scanf
// printing
("The sum of the numbers you entered is: %d\n", sum(a,b,c,d,e));
printf
("The average is: %.2f\n", average(a,b,c,d,e));
printf
return 0;
}
int sum(int v, int w, int x, int y, int z) {
// adding all of them
return v+w+x+y+z;
}
float average(int v, int w, int x, int y, int z) {
// the average is giving by the sum of all terms / number of all terms
// so we use our sum function and then divide by 5, but since we need the decimals, we need to cast to a float
return (float) sum(v,w,x,y,z) / 5;
}
Answer:
#include <stdio.h>
void isEven(int x);
int main (void) {
int a;
// prompt user
("Please enter an integer\n");
printf
// getting the number
("%d", &a);
scanf
// calling the function that will print for us
(a);
isEven
return 0;
}
void isEven(int x) {
// when the number is even, the remainder of the division by 2 is 0
if(x%2 == 0){
("The number is even.\n");
printf} else {
("The number is odd.\n");
printf}
}
Answer:
#include <stdio.h>
void isDivisibleBy3(int x);
int main (void) {
int a;
// prompt user
("Please enter an integer\n");
printf
// getting the number
("%d", &a);
scanf
// calling the function that will print for us
(a);
isDivisibleBy3
return 0;
}
void isDivisibleBy3(int x) {
// when the number is divisible by 3, the remainder of the division by 3 is 0
if(x%3 == 0){
("The number is divisible by 3.\n");
printf} else {
("The number is not divisible by 3.\n");
printf}
}
Answer:
#include <stdio.h>
int findHighest(int x, int y);
int main (void) {
int a, b;
// prompt user
("Please enter two integers\n");
printf
// getting the numbers
("%d %d", &a, &b);
scanf
// printing
("The highest number is %d\n", findHighest(a,b));
printf
return 0;
}
int findHighest(int x, int y) {
// compare which one is bigger
if(x > y) {
return x;
}
return y;
}
Answer:
#include <stdio.h>
void checkTriangle(int x, int y, int z);
int main (void) {
int a, b, c;
// prompt user
("Please enter three integers\n");
printf
// getting the numbers
("%d %d %d", &a, &b, &c);
scanf
// printing
(a, b, c);
checkTriangle
return 0;
}
void checkTriangle(int x, int y, int z) {
// an equilateral triangle has all equal sides
if (x == y && y == z ) {
("Equilateral Triangle\n");
printf} // an isosceles triangle has 2 equal sides
else if (x == y || y == z || z == x ) {
("Isosceles Triangle\n");
printf} // a scalene triangle has all different sides
else {
("Scalene Triangle\n");
printf}
}
Answer:
float m = (2*x + 7*y - 13) / (37*37 + z);
Answer:
a = a << 3
b = b << 5
c = c >> 2