C - Checking value passed into a function

Introduction

verify() function checks to confirm whether the value input is within the range from 0 to 100.

The function should return the constant TRUE (defined as 1) if the value is within the range, or FALSE (defined as 0) if not.

When a value is out of range, the program needs to display an error message.

Demo

#include <stdio.h>

#define TRUE 1/*  w ww  . ja v  a2s  . c o  m*/
#define FALSE 0

void display(int stop);
int verify(int check);

int main()
{
    int x;

    printf("Enter a stopping value (0-100): ");
    scanf("%d",&x);
    if(verify(x))
    {
        display(x);
    }
    else
    {
        printf("%d is out of range!\n",x);
    }
    return(0);
}

int verify(int check)
{
    if(check < 0 || check > 100)
        return FALSE;
    return TRUE;
}

void display(int stop)
{
    int x;

    for(x=0;x<=100;x=x+1)
    {
        printf("%d ",x);
        if(x==stop)
        {
            puts("You won!");
            return;
        }
    }
    puts("I won!");
}

Result

Related Topic