Wednesday, April 8, 2015

Natural Numbers. in c pgm

#include

int mult (int x, int y)
{
  int result;
  result = 0;
  while (y != 0)
{
    result = result + x;
   y = y - 1;
  }
  return(result);
}

int main ()
{
  int x, y;
  cout << "Enter two natural numbers: ";
  cin >> x >> y;
  cout << x << " * " << y << " = " << mult(x,y) << endl;
  return(0);
}



Leap in Year in c Program

#include
#include
#include
typedef int Bool;
char whatDay (int);

Bool isLeapYear (int);

int numOfDaysInMonth (int,Bool);

void printHeader (int);

void printMonth (int, int&);

void skip (int);

void skipToDay (int);

void disaster ();

void main ()
 {

  int year, firstDayInCurrentMonth;
  Bool leap;
  int currentMonth = 1;

  cout << "What year do you want a calendar for? ";
  cin >> year;
  cout << "What day of the week does January 1 fall on?" << endl;
  cout << "(Enter 0 for Sunday, 1 for Monday, etc.) ";
  cin >> firstDayInCurrentMonth;

  leap = isLeapYear(year);
  skip(9); cout << year << endl;

  while (currentMonth <= 12)
 {
    numDays = numOfDaysInMonth(currentMonth,leap);
    printHeader(currentMonth);
    printMonth(numDays, firstDayInCurrentMonth);
    cout << endl << endl << endl;
    currentMonth = currentMonth + 1;
  }
  cout << endl;
}

void disaster ()
{
  cout << "Disaster! Exiting ..." << endl;
  exit (-1);
}

void skip (int i)
{
  while (i > 0)
{
    cout << " ";
    i = i - 1;
  }
}

char whatDay (int d)
{
  if (d == 0) return('S');
  else if (d == 1) return('M');
  else if (d == 2) return('T');
  else if (d == 3) return('W');
  else if (d == 4) return('T');
  else if (d == 5) return('F');
  else if (d == 6) return('S');
  else disaster();
}

Bool isLeapYear (int y)
{
  return ((y % 4) == 0);
}

void printHeader (int m)
{

  if (m == 1) { skip(7); cout << "January" << endl;
}
  else if (m == 2) { skip(7); cout << "February" << endl;
}
  else if (m == 3) { skip(7); cout << "March" << endl;
}
  else if (m == 4) { skip(7); cout << "April" << endl;
}
  else if (m == 5) { skip(7); cout << "May" << endl;
}
  else if (m == 6) { skip(7); cout << "June" << endl;
}
  else if (m == 7) { skip(7); cout << "July" << endl;
}
  else if (m == 8) { skip(7); cout << "August" << endl;
}
  else if (m == 9) { skip(7); cout << "September" << endl;
}
  else if (m == 10) { skip(7); cout << "October" << endl;
}
  else if (m == 11) { skip(7); cout << "November" << endl;
}
  else if (m == 12) { skip(7); cout << "December" << endl;
}
  else disaster();

  cout << " S  M  T  W  T  F  S" << endl;
  cout << "____________________" << endl;
}

int numOfDaysInMonth (int m, Bool leap)
{
  if (m == 1) return(31);
  else if (m == 2) if (leap) return(29); else return(28);
  else if (m == 3) return(31);
  else if (m == 4) return(30);
  else if (m == 5) return(31);
  else if (m == 6) return(30);
  else if (m == 7) return(31);
  else if (m == 8) return(31);
  else if (m == 9) return(30);
  else if (m == 10) return(31);
  else if (m == 11) return(30);
  else if (m == 12) return(31);
  else disaster();
}

void skipToDay (int d)
{
  skip(3*d);
}

void printMonth (int numDays, int& weekDay)
{
  int day = 1;

  skipToDay(weekDay);
  while (day <= numDays)
{
    cout << setw(2) << day << " ";
    if (weekDay == 6)
 {
      cout << endl;
      weekDay = 0;
    }
    else weekDay = weekDay + 1;
    day = day + 1;
  }
}

Boolean in c Program

#include
#include
#include

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

void readLetter(char&);
int computeCorrespondingDigit(char);
Bool doOneLetter();

void main ()
{
  Bool flag = FALSE;

  while (!flag)
{
    flag = doOneLetter();
  }
}

int exitLetter (char c)
{
  return ((c == 'Q') || (c == 'Z'));
}

Bool doOneLetter()
{
  char l;
  int i;

  readLetter(l);
  if (exitLetter(l))
{
    cout << "Quit." << endl;
    return(TRUE);
  }
  Else
 {
    i = computeCorrespondingDigit(l);
    cout << "The letter " << l << " corresponds to " << i << " on the telephone"<< endl;
    return(FALSE);
  }
}

int computeCorrespondingDigit (char l)
{
  switch (l)
{
  case 'A' :
  case 'B' :
  case 'C' :
    return(2);
  case 'D' :
  case 'E' :
  case 'F' :
    return(3);
  case 'G' :
  case 'H' :
  case 'I' :
    return(4);
  case 'J' :
  case 'K' :
  case 'L' :
    return(5);
  case 'M' :
  case 'N' :
  case 'O' :
    return(6);
  case 'P' :
  case 'R' :
  case 'S' :
    return(7);
  case 'T' :
  case 'U' :
  case 'V' :
    return(8);
  case 'W' :
  case 'X' :
  case 'Y' :
    return(9);
  }
}

void readLetter(char& c)
{
  cout << "Enter a letter: ";
  cin >> c;
  while (cin && !isalpha(c))
{
    cout << "You have not entered a letter. Try again: ";
    cin >> c;
  }
  if (!cin)
{
    cout << "Unrecoverable error... Exiting" << endl;
    exit(-1);
  }
  c = toupper(c);
}

Tuesday, April 7, 2015

RGB Color in C Program

 #include
#include
#include

enum Color
{Red, Blue, White, Green, Yellow};

const double DUMMY = 0.0;
const Color DUMMYC = White;
void show (Color);
void point (char, double);
void colorPoint (char, double, Color);

void main ()
{
  point('O', DUMMY);
  point('P', DUMMY);
  point('X', 8.0);
  point('P', DUMMY);
  point('Y', 7.0);
  point('P', DUMMY);
  point('X', 2.0);
  point('P', DUMMY);
  point('X', 3.0);
  point('P', DUMMY);
  point('O', DUMMY);
  point('P', DUMMY);
  colorPoint('O',DUMMY,DUMMYC);
  colorPoint('P',DUMMY,DUMMYC);
  colorPoint('C',DUMMY,Blue);
  colorPoint('P',DUMMY,DUMMYC);
  colorPoint('X',2.0,DUMMYC);
  colorPoint('P',DUMMY,DUMMYC);
}

void point (char message, double data)
{
  static double x_coord = 0.0;
  static double y_coord = 0.0;
  static double distance = 0.0;

  switch (message)
 {
  case 'X':
          x_coord = x_coord + data;
    break;
  case 'Y':  
          y_coord = y_coord + data;
    break;
    case 'O':
    x_coord = 0.0;
    y_coord = 0.0;
    break;
  case 'P':
    cout << "I am currently at x = " << x_coord << ", y = " << y_coord << endl;
    cout << "My distance from the origin is " << distance << endl;
    break;
  default:
    cout << "Unknown message to point" << endl;
    exit(-1);
  }
  distance = sqrt (x_coord * x_coord + y_coord * y_coord);
  return;
}

void colorPoint (char message, double data, Color color)
 {
  static Color col = White;

  switch (message)
{
  case 'X':
  case 'Y':
  case 'O':
    point(message,data);
    break;
  case 'C':
    col = color;
    break;
  case 'P':
    point('P',DUMMY);
    cout << "Also my color is ";
    show(col);
    cout << endl;
    break;
  default:
    cout << "Unknown message to colorPoint" << endl;
    exit(-1);
  }
  return;
}

void show (Color col)
{
  switch (col)
{
  case Red:
    cout << "Red";
    break;
  case Blue:
    cout << "Blue";
    break;
  case White:
    cout << "White";
    break;
  case Green:
    cout << "Green";
    break;
  case Yellow:
    cout << "Yellow";
    break;
  }
}

Simple Program of C Program

#include
void f ();
int i = 0;
void main ()
 {
  cout << i << endl;
  int i = 1;
  cout << i << endl;
  {
    int i = 2;
    cout << i << endl;
    {
      int i = 3;
      cout << i << endl;
      {
int i = 4;
cout << i << endl;
      }
      cout << i << endl;
    }
    cout << i << endl;
  }
  cout << i << endl;
  f(); f(); f();
}

void f ()
{
  cout << i << endl;
  int i = 5;
  cout << i++ << endl;
}

C Program of Simple Sum

#include
void inc (int, int&);
void main ()
 {
  int i = 3;
  int j;

  inc(i,j);
  cout << j << endl;
}

void inc (int a, int& b)
{
  b = a+1;
}


Sum of the Digit in c program

#include
void main () 
{
  int num, count = 1;
  float sum = 0.0;

  cout << endl;
  while (count <= 3) 
{
    cout << "Enter a number: ";
    cin >> num;
    sum = sum + num;
    count = count + 1;
  }
  cout << "Average is " << sum / count << endl;
}

There are several ways of fixing the problem. Here is one of them:
*/

void main () 
{
  int num, count = 1;
  float sum = 0.0;

  cout << endl;
  while (count <= 3)
 {
    cout << "Enter a number: ";
    cin >> num;
    sum = sum + num;
    count = count + 1;
  }
  cout << "Average is " << sum / (count - 1) << endl;
}


#include
int inc (int);
void main () 
{
  int i = 3;
  int j;

  j = inc (i);
  cout << j << endl;
}

int inc (int a) 
{
  return(a+1);
}