Monday, March 23, 2015

Boolean of C Program



 
#include 
 
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);
}
 

While in C Program



#include 
#include 
 
int exp (int,int);
void readnums (int&, int&);
 
void main ()
 {
  int b, e;
  readnums(b,e);
  cout << b << " to the " << e << " = " << exp(b,e) << endl;
}
 
void readnums (int& b, int& e) 
{
  int correctInput;
 
  cout << "Enter the base and the exponent: ";
  cin >> b >> e;
 
  if (!cin) 
{ 
    cout << "Disaster! Terminating program." << endl;
    exit(-1);
  }
 
  correctInput = (b >= 0) && (e >= 0);
  while (!correctInput)
 {
    cout << "Something wrong! Try again ..." << endl;
    cout << "Re-enter base and exponent: ";
    cin >> b >> e;
    
    if (!cin) 
{ 
      cout << "Disaster! Terminating program." << endl;
      exit(-1);
    }
    correctInput = (b >= 0) && (e >= 0);
  }
  
}
 
int exp (int b, int e) 
{
  int result;
 
  result = 1;
  while (e != 0) 
{
    result = result * b;
    e = e - 1;
  }
  return(result);
}

Initial of C Programs



#include 
 
void main () 
{
  int i;
 
  i=1;
  cout << "Initial value of i = " << i << ". The value of ++i is " << ++i<<". Now i = " << i << endl;
 
  i=1;
  cout << "Initial value of i = " << i 
    << ". The value of i++ is " << i++ 
      << ". Now i = " << i << endl;
}
 

Sum of the Degits on c pgm




#include 
 
int f1 (int, int, int);
int f2 (int&, int&, int&);
int f3 (int, int&, int);
 
void main ()
 {
  int i, j, k;
 
  i=1;
  j=2;
  k=3;
 
  cout << endl;
  cout << "Initial values of i, j, and k are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f1(i,j,k) = " << f1(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f1 are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f2(i,j,k) = " << f2(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f2 are: " 
    << i << ", " << j << ", and " << k << endl << endl;
  cout << "f3(i,j,k) = " << f3(i,j,k) << endl;
  cout << "Values of i, j, and k after the call to f3 are: " 
    << i << ", " << j << ", and " << k << endl;
}
 
int f1 (int x, int y, int z)
{
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}
 
int f2 (int& x, int& y, int& z)
 {
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}
 
int f3 (int x, int& y, int z) 
{
  x=x+5;
  y=y+5;
  z=z+5;
  return(x+y+z);
}