Docstoc

W02L03

Document Sample
W02L03 Powered By Docstoc
					02-1-IF and ELSE
[Copy this page for students]

PROBLEM 4: Write the program to do the following. Call the program 02-1.c

PROGRAM  Accept 2 numbers
         Display only what user wants SUM, PRODUCT – will add more choices later
         02-1.c


OUTPUT:
Enter first integer:
Enter second integer:

SELECT from this menu
     1 TOTAL
     2 PRODUCT

Enter 1 or 2:

… Then display what was requested with some text




Document1 by rt -- 12/31/2011                                               1 of 4
/* 02-1.c
demonstrate sum and product
have user select which one to accept
*/
#include <stdio.h>

main()
{
         int    number1,
                number2,
                total,
                product,
                choice;


         /* get inputs and total        */

         printf ("\n\n");
         printf ("Enter the first integer: ");
         scanf ("%d", &number1);

         printf ("\n\n");
         printf ("Enter the second integer: ");
         scanf ("%d", &number2);

         /* Ask if want total or product of 2 integers */
         printf("\n\n");
         printf( "       Select what you want to see\n"
                 "       1 TOTAL                \n"
                 "       2 PRODUCT                 \n\n"
                 "       Enter 1 or 2 : ");

         scanf ("%d", &choice);

         if (choice == 1)
                 {
                 total = number1 + number2;
                 printf("\n\n");
                 printf ("     The TOTAL is %d", total);
                 }
         else {
                 product = number1 * number2;
                 printf("\n\n");
                 printf ("     The PRODUCT is %d", product);
                 }

         printf ("\n\n\n\n");

} /* End of Main */




Document1 by rt -- 12/31/2011                                  2 of 4
CHANGE THE PROGRAM 02-1.c and call it 02-1b.c
Accept       T for TOTAL P for PRODUCT
/* 02-1b.c Demonstrate sum and product -- have user select
which one to do but using a character input */
#include <stdio.h>

main()
{    int  number1,
         number2,
         total,
         product;
     char choice;


     /* get inputs and total */

     printf ("\n\n");
     printf ("Enter the first integer: ");
     scanf ("%d", &number1);

     printf ("\n\n");
     printf ("Enter the second integer: ");
     scanf ("%d", &number2);

     /* Ask if want total or product of 2 integers */
     printf("\n\n");
     printf( "       Select what you want to see\n"
           "       T TOTAL                \n"               Flush buffer
           "       P PRODUCT                  \n\n"
           "       Enter T or P : ");

/* because of newline left after number we need to flush the buffer*/
     fflush(stdin);
     scanf ("%c", &choice);

     if (choice == 'T')                                       Watch single
           {                                                  quotes here
           total = number1 + number2;
           printf("\n\n");
           printf ("     The TOTAL is %d", total);
           }
     else {
           product = number1 * number2;
           printf("\n\n");
           printf ("     The PRODUCT is %d", product);
           }

      printf ("\n\n\n\n");
} /* End of Main */




Document1 by rt -- 12/31/2011                                                3 of 4
Modify the program so that the printf line states “The ANSWER is %d” -- 02-1c.c

Note where it was in the IF – now outside the IF since both sides do the same thing
If both sides have the same code it is better to put it outside of the IF where possible.
/* 02-1c.c
Demonstrate sum and product have user select
which one to do but using a character input */
#include <stdio.h>
main()
{      int number1,
           number2,
           answer;
      char choice;

     /* get inputs and total */

     printf ("\n\n");
     printf ("Enter the first integer: ");
     scanf ("%d", &number1);

     printf ("\n\n");
     printf ("Enter the second integer: ");
     scanf ("%d", &number2);

     /* Ask if want total or product of 2 integers */
     printf("\n\n");
     printf( "       Select what you want to see\n"
           "       T TOTAL                \n"                 Flush buffer
           "       P PRODUCT                  \n\n"
           "       Enter T or P : ");

/* because of newline left after number we need to flush the buffer*/
     fflush(stdin);
     scanf ("%c", &choice);

     if (choice == 'T')                                         Watch single
           {                                                    quotes here
           answer = number1 + number2;
           }
     else
           {
           answer = number1 * number2;
           }

      printf("\n\n");
      printf ("     The ANSWER is %d", answer);

      printf ("\n\n\n\n");
} /* End of Main */




Document1 by rt -- 12/31/2011                                                   4 of 4

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:1
posted:12/31/2011
language:
pages:4