Professional Documents
Culture Documents
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
int main()
{
int iGuessNum;
int iRandomNum;
const int iUPPER = 10;
const int iLOWER = 1;
srand(time(NULL));
if (scanf("%d", &iGuessNum) == 1)
{
iRandomNum = (rand() % (iUPPER - iLOWER + iLOWER)) +
iLOWER;
if (iGuessNum == iRandomNum)
{
printf("You guessed correctly!\n");
}
else
{
printf("You guessed wrong!\n");
}
}
else
{
printf("Please enter a number.\n");
}
return 0;
}
/*
Create a dice game that uses two six-sided dice. Each time the
program runs, use random numbers to assign values to each die
variable. Output a “player wins” message to the user if the sum
of the two dice is 7 or 11. Otherwise output the sum of the two
dice and thank the user for playing.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int iDie1;
int iDie2;
const int iUPPER = 6;
const int iLOWER = 1;
srand(time(NULL));
iDie1 = (rand() % (iUPPER - iLOWER + iLOWER)) + iLOWER;
iDie2 = (rand() % (iUPPER - iLOWER + iLOWER)) + iLOWER;
/* initialization phase */
total = 0; /* initialize total */
counter = 1;
/* processing phase */
while (counter <= 5)
{ /* loop 5 times */
printf("Enter grade: "); /* prompt for input */
scanf("%d", &grade); /* read grade from
user */
total = total + grade; /* add grade to total
*/
counter = counter + 1;
} /* end while */
/* termination phase */
average = total / 10; /*
integer division */
printf("Class average is %d\n", average); /*
display result */
return 0; /*
indicate program ended successfully */
} /* end function main */
/* Fig. 3.6: fig03_06.c
2 Class average program with */
#include <stdio.h>
/* initialization phase */
total = 0; /* initialize total */
counter = 1;
/* processing phase */
while (counter <= 5)
{ /* loop 5 times */
printf("Enter grade: "); /* prompt for input */
scanf("%d", &grade); /* read grade from
user */
total = total + grade; /* add grade to total
*/
counter = counter + 1;
} /* end while */
/* termination phase */
average = total / 10; /*
integer division */
printf("Class average is %d\n", average); /*
display result */
return 0; /*
indicate program ended successfully */
} /* end function main */
/* Fig. 3.8: fig03_08.c
Class average program with sentinel-controlled repetition */
#include <stdio.h>
/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */
/* processing phase */
/* get first grade from user */
printf("Enter grade, -1 to end: "); /* prompt for input */
scanf("%d", &grade); /* read grade from user */
/* termination phase */
/* if user entered at least one grade */
if (counter != 0)
{
/* calculate average of all grades entered */
average = (float)total / counter; /* avoid truncation */
void main()
{
int iResponse = 0;
if (iResponse == 1)
printf("\nAC is now on\n");
if (iResponse == 2)
printf("\nAC is now off\n");
if (iResponse == 3)
printf("\nTimer is now on");
if (iResponse == 4)
printf("\nTimer is now off");
}
#include <stdio.h>
void main()
{
int iSelection = 0;
float fTransAmount = 0.0;
float fBalance = 100.25;
printf("\n\tATM\n");
printf("\n1\tDeposit Funds\n");
printf("2\tWithdraw Funds\n");
printf("3\tCheck Balance\n");
printf("4\tTransfer Funds\n");
printf("\nEnter your selection: ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
printf("\nEnter fund amount to deposit: ");
scanf("%f", &fTransAmount);
printf("\nYour new balance is: $%.2f\n", fBalance +
fTransAmount);
} //end if
if (iSelection == 2)
{
printf("\nEnter fund amount to withdraw: ");
scanf("%f", &fTransAmount);
if (fTransAmount > fBalance)
printf("\nInsufficient funds\n");
else
printf("\nYour new balance is $%.2f\n", fBalance -
fTransAmount);
} //end if
if (iSelection == 3)
{
printf("\nYour balance is: $%.2f\n", fBalance);
} //end if
if (iSelection == 4)
{
printf("\nYour Transfer Fund is: $%.2f\n", fTransAmount);
} //end if
void main()
{
int iRandomNum = 0;
srand(time(NULL));
iRandomNum = (rand() % 4) + 1;
printf("\nFortune Cookie - Chapter 3\n");
switch (iRandomNum)
{
case 1:
printf("\nYou will meet a new friend
today.\n");
break;
case 2:
printf("\nYou will enjoy a long and happy
life.\n");
break;
case 3:
printf("\nOpportunity knocks softly. Can you
hear it?\n");
break;
case 4:
printf("\nYou'll be financially rewarded for
your good deeds.\n");
break;
default:
printf("\nYou've gained zero fortune");
} //end switch
printf("\nLucky lotto numbers: ");
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
} //end main function
/* Fig. 3.8: fig03_08.c
Class average program with sentinel-controlled repetition */
#include <stdio.h>
/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */
/* processing phase */
/* get first grade from user */
printf("Enter grade, -1 to end: "); /* prompt for input */
scanf("%d", &grade); /* read grade from user
*/
do
{
total = total + grade; /* add grade to total */
counter = counter + 1; /* increment counter */
/* termination phase */
/* if user entered at least one grade */
if (counter != 0)
{
/* calculate average of all grades entered */
average = (float)total / counter; /* avoid truncation */
C Code:
Output:
Figure 3.8
C Code:
Output:
Question 1:
C Code:
Output:
Guessed correctly
Guessed incorrectly
Validation test
Question 3:
C Code:
Output:
C Codes With Output from C How to Program
Figure 3.5
Modified to execute for only five students
C Code:
Output:
Is repetition in Figure 3.5 bounded or unbounded? It is bounded
While loop converted to a Do While Loop:
C Code:
Output:
Figure 3.8
Modified to execute for only five students
C Code:
Output:
Is repetition bounded or unbounded? - It is unbounded
C Code:
Output: