|
CS256 - Principles of Structured Design
Fall 2021
|
Displaying ./code/cs256su21code/jun23/isOdd.c
/*
midterm review:
a review of making a function that returns either a 1(for true) or 0 (for false)
There is a problem on the midterm where I will have you do a function that will return
either a 1 or a 0, but there will also be a loop involved here.
*/
#include <stdio.h>
int isOdd(int n); //function declaration
// this tells the compiler that we've got a function defined
// below the main function
//the function is named isOdd
// it returns an integer (the int before the function name)
// it takes one integer as an argument (hence the "int n" inside the parenthesis)
//the function will be defined below main
int main(){
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(isOdd(num) == 1)
printf("This number is odd\n");
else
printf("This number is even\n");
return 0;
}
//we define the function down here
//this is where we tell the computer how to do this thing.
//this function determines if the given number is odd
int isOdd(int n){
if(n % 2 == 0)
return 0;
else
return 1;
}
|