Sample – functions
by Nideesh C on February 8, 2011 · 0 comments
in C++
#include <iostream.h>
typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;
Bool even (int);
Bool odd (int);
int readPosNum();
void testOneNum();
void panic();
void main () {
int i;
char c;
Bool more = TRUE;
while (cin && more) {
testOneNum();
cout << “More? [y = Yes, anything else No]: “;
cin >> c;
if (cin) more = (c == ‘y’);
}
}
void testOneNum () {
int i;
i = readPosNum();
if (even(i)) cout << “The number ” << i << ” is even.” << endl;
else cout << “The number ” << i << ” is odd.” << endl;
}
int readPosNum () {
int j;
cout << “Enter a number >= 0: “;
cin >> j;
while (cin && j < 0) {
cout << “Unacceptable, reenter: “;
cin >> j;
}
if (cin) return(j);
else panic();
}
Bool even (int i) {
if (i == 0) return(TRUE);
else return(odd(i-1));
}
Bool odd (int i) {
if (i == 0) return(FALSE);
else return(even(i-1));
}
void panic() {
cout << “Disaster! Exiting …” << endl;
exit(-1);
}
Not Satisfied ? Just search & get the result
Related posts:
- Sample – example of pass-by-reference
- Sample – factorial
- Sample – understanding call-by-value and call-by-reference
- Sample – check if a number is prime
- Sample – Fibonacci numbers
Tagged as:
c programming examples,
c programming reference,
c tutorial,
c++ program examples,
C++ Programming,
functions,
learning c++,
programming languages,
programming languages c,
Sample - functions,
sample c++ programs
Me, freelance system administrator having the qualification of Diploma in Electronics & Tele-communication + MCSE + CCNA + CST + 5 years of experience in IT field.
If you like This post, you can follow TheOnlineTutorials on Twitter.
Contact me Via email: support@theonlinetutorials.com
Subscribe to feed via Feed or EMAIL to receive instant updates.
Legal Disclaimer:All information found on the site is without any implied warranty of fitness for any purpose or use whatsoever. Content author/site administrator is not responsible for any loss occurred due to mistakes in this tutorial. Use this tutorial website at your own risk.