Tuesday, April 7, 2015

True or False in c program

#include
#include
typedef int Bool;
const Bool TRUE = 1;
const Bool FALSE = 0;

int readInt (Bool&);
int sumSquares (int i);

void main ()
{
  int i;
  Bool endOfData;

  i = readInt(endOfData);
  while (!endOfData)
{
    cout << "Sum of squares from 1 to " << i << " = " <<
      sumSquares (i) << endl;
    i = readInt(endOfData);
  }
}

int readInt (Bool& b)
{
  int i;

  cout << "Enter an integer greater than 1 (negative number to exit): ";
  cin >> i;
  while (i == 0)
{
    cout << "Re-enter integer: ";
    cin >> i;
  }
  if (i < 1) b = TRUE; else b = FALSE;
  return(i);
}

int sumSquares (int i)
{
  int count, result;

  count = 1;
  result = 0;
  while (count <= i)
{
    result = result + count * count;
    count = count + 1;
  }
  return(result);
}

No comments:

Post a Comment