Learn C programming in 11 days

Day 3 If and Switch

Maximam

 

/*********************************************************/

/*  maximum.cpp - VC5.0                                  */

/*  Maximum function                                     */

/*  J Luo, 07-Mar-2001                                   */

/*********************************************************/

 

#include<stdio.h>

 

void main()

{

    int a, b, c;

 

printf("Enter two integers: ");

scanf("%d %d", &a, &b);

printf("a=%d, b=%d \n", a, b);

 

    if ( a>b ) c=a; else c=b;

 

    printf("Maximum of %d and %d is: %d\n", a, b, c);

}

 

 

How about:

 

    scanf("a=%d b=%d", &a, &b);

 

Area of a triangle

 

/*********************************************************/

/*  area.cpp - VC5.0                                     */

/*  Arae of a triangle                                   */

/*  J Luo, 22-Feb-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <math.h>

 

void main()

{

     double a, b, c;        // sides of the triangle

     double s;

     double area;

 

     cout << "The sides of a triangle: ";

     cin >> a >> b >> c;

 

     if ( (a+b>c) && (b+c)>a && (c+a)>b )

     {

         s = 0.5*(a+b+c);

         area = sqrt(s*(s-a)*(s-b)*(s-c));

         cout << "Area = " << area << endl;

     } else {

         cout << "Not a triangle! \n";

     }

}

 

Output

 

The sides of a triangle: 3 4 7

Not a triangle!

 

The sides of a triangle: 3 4 8

Not a triangle!

 

 

Divide by 2, 3, 5

 

/*********************************************************/

/*  divide.cpp - VC5.0                                     */

/*  divide by 2, 3, 5                                    */

/*  J Luo, 28-Feb-2001                                   */

/*********************************************************/

 

#include <iostream.h>

 

void main()

{

     int m;

 

     cout << " Enter an integer: ";

     cin >> m;

 

     if ( m%2 == 0 )

     {

         cout << m << " can be divided by 2\n";

         if ( m%3 == 0 )

         {

              cout << m << " can be divided by 3\n";

              if ( m%5 == 0 )

              {

                   cout << m << " can be divided by 5\n";

              } else {

                   cout << m << " can Not be divided by 5\n";

              }

         } else {

              cout << m << " can Not be divided by 3\n";

              if ( m%5 == 0 )

              {

                   cout << m << " can be divided by 5\n";

              } else {

                   cout << m << " can Not be divided by 5\n";

              }

         }

     } else {

         cout << m << " can Not be divided by 2\n";

         if ( m%3 == 0 )

         {

              cout << m << " can be divided by 3\n";

              if ( m%5 == 0 )

              {

                   cout << m << " can be divided by 5\n";

              } else {

                   cout << m << " can Not be divided by 5\n";

              }

         } else {

              cout << m << " can Not be divided by 3\n";

              if ( m%5 == 0 )

              {

                   cout << m << " can be divided by 5\n";

              } else {

                   cout << m << " can Not be divided by 5\n";

              }

         }

     }

}

 

Output

 

 Enter an integer: 2

2 can be divided by 2

2 can Not be divided by 3

2 can Not be divided by 5

 

 

Wgat day today

 

/*********************************************************/

/*  what_day.cpp - VC6.0                                 */

/*  What day today?                                      */

/*  J Luo, 21-Feb-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <stdio.h>

 

void main()

{

     int year, months, weeks, days;

     double hours, minutes, seconds;

     int number;

 

     cout << "                ***** Happy New Millennium 2001! ***** \n\n";

 

     year = 2001;

 

     cout << "\n OK! Now I can tell you the number of: \n";

     cout << " Months, weeks, days, hours, minutes and seconds, \n";

     cout << " since the 1st January 1, till 1 Jan 2001 \n\n";

     cout << " Believe so? \n";

 

     year = year - 1;

     months = year * 12;

     days = year * 365;

     days = days + year/4;

     days = days - year/100;

     days = days + year/400;

     weeks = days/7;

     hours = days * 24;

     minutes = hours * 60;

     seconds = minutes * 60;

 

     cout << " Here we are: \n\n";

     cout << " Months:  " << months  << endl;

     cout << " Weeks:   " << weeks   << endl;

     cout << " Days:    " << days    << endl;

     cout << " Hours:   " << hours   << endl;

     cout << " Minutes: " << minutes << endl;

     cout << " Seconds: " << seconds << endl;

     cout << " Am I clever? :-) \n";

     cout << " Any more information? \n\n";

     getchar();

 

     cout << " Yes, I want to know what day is today? \n\n";

     cout << " Let me try ... \n";

 

     days = days + 1;

     number = days%7;

 

     getchar();

 

     cout << " OK! I can tell you the number of the day. \n";

     cout << " It is : " << number << endl;

 

     switch ( number )

     {

         case 0: cout << " Sunday "    << endl; break;

         case 1: cout << " Monday "    << endl; break;

         case 2: cout << " Tuesday"    << endl; break;

         case 3: cout << " Wednesday " << endl; break;

         case 4: cout << " Thursday "  << endl; break;

         case 5: cout << " Friday "    << endl; break;

         case 6: cout << " Saturday "  << endl; break;

         default: cout << " ??? "   << endl; break;

     }

 

     cout << "                ***** Good-Bye *****     \n\n";

}