0% found this document useful (0 votes)
94 views9 pages

Subject: PRF192-PFC Workshop 05 Nguyen Anh Duy - SE160252: Exercise 1

The document contains code for 4 exercises that demonstrate C programming skills. Exercise 1 generates random dice rolls until a target sum is reached. Exercise 2 does the same for random ball picks. Exercise 3 adds menu selection and functions for date validation and character printing. Exercise 4 further expands the menu and functions to include square root calculation and a bank deposit problem. The code uses standard C libraries, input/output, loops, functions, parameters, and conditionals.

Uploaded by

dO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views9 pages

Subject: PRF192-PFC Workshop 05 Nguyen Anh Duy - SE160252: Exercise 1

The document contains code for 4 exercises that demonstrate C programming skills. Exercise 1 generates random dice rolls until a target sum is reached. Exercise 2 does the same for random ball picks. Exercise 3 adds menu selection and functions for date validation and character printing. Exercise 4 further expands the menu and functions to include square root calculation and a bank deposit problem. The code uses standard C libraries, input/output, loops, functions, parameters, and conditionals.

Uploaded by

dO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Subject: PRF192- PFC

Workshop 05
Nguyen Anh Duy – SE160252
Exercise 1:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{ int total, count , x , y ;

printf(" Dice Thrower\n ");

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

do

{ printf("Total sought : "); scanf("%d",& total);

} while ( total < 2 || total > 12);

count = 0;

do

x = (rand() % 6) + 1;

y = (rand() % 6) + 1;

count ++;

printf("Result of throw %d: %d + %d\n",count , x , y);

} while (x+y != total);

if ( x+y == total){

printf ("You got your total in %d throws!",count);

}
return 0;

Exercise 2:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{ int total, count , x , y ;

printf(" Ball Lottery \n ");

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

do

{ printf("Total sought : "); scanf("%d",&total);

} while ( total < 2 || total > 20);

count = -1;

do

{ count += 2;

x = (rand() % 10) + 1;

y = (rand() % 10) + 1;

printf("Result of picks %d and %d: %d + %d\n",count, count + 1 , x , y);


} while (x+y != total);

if ( x+y == total){

printf ("You got your total in %d picks!",count+1);

return 0;

E400xercise 3:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{ int total, count , x , y ;
printf(" Ball Lottery \n ");
printf("============\n ");#
do
{ printf("Total sought : ");
scanf("%d",&total);
} while ( total < 2 || total > 20);
count = -1;
do
{ count += 2;
x = (rand() % 10) + 1;
y = (rand() % 10) + 1;

printf("Result of picks %d and %d: %d +


%d\n",count, count + 1 , x , y);
} while (x+y != total);
if ( x+y == total){
printf ("You got your total in %d
picks!",count+1);
}
return 0;

}#include<stdio.h>

#include<stdlib.h>

int choice()

{ int choice ;

printf("\n1- Processing date data");

printf("\n2- Character data");

printf("\n3- Quit");

printf("\nChoose: ");

scanf("%d%*c",&choice);

return choice;

int validDate (int d, int m, int y){

int maxd=31;

if(d <1 || d>31 || m<1 || m >12) return 0 ;

if( m ==4 || m == 6 || m ==9 || m == 11) maxd = 30;

else if(m==2) {

if ( y%400==0 || y%4==0 && y%100!=0 ) maxd=29;

else maxd=28;

return d<=maxd;

}
void function1()

{ int d , m , y;

printf("Enter the day: " ); scanf("%d", &d);

printf("Enter the month:"); scanf("%d", &m);

printf("Enter the year: "); scanf("%d", &y);

if (validDate(d, m, y)==1) {

printf ("The date of %d/%d/%d is a valid date!\n", d, m ,y);

}else

printf("The date of %d/%d/%d is not a valid date!\n", d, m ,y);

void printAscii(char c1 , char c2)

{ char c;

if (c1 > c2)

{ c=c1 ; c1=c2 ;c2=c ;

for ( c=c1 ; c<= c2; c++)

printf("%c: %3d, %3Xh\n",c,c,c);

void function2()

{ char c1,c2;

printf(" Enter 2 characters contiguously: ");

scanf("%c%c",&c1, &c2);

printAscii(c1,c2);

}+

int main()

{ int userChoice;

do
{ userChoice = choice();

switch(userChoice)

{ case 1: function1(); break;

case 2: function2(); break;

default: printf("Bye!\n");

while(userChoice > 0 && userChoice < 3);

fflush(stdin);

getchar();

return 0;

Exercise 4:
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int choice()

{ int choice ;

printf("\n1- Quadratic equation ");

printf("\n2- Bank deposit problem");

printf("\n3- Quit");

printf("\nChoose: ");

scanf("%d%*c",&choice);

return choice;

void function1()

{ double number, squareRoot;

printf("Enter a number: ");

scanf("%lf", &number);

squareRoot = sqrt(number);

printf("Square root of %.0lf = %.0lf", number, squareRoot);

void function2()

{ double x , r ,P;

int y;

printf("Enter your deposit[a positive number]: ");scanf("%lf",&x);

printf("\nEnter the yearly rate[0.0-1.0]: "); scanf("%lf",&r);

printf("\nHow many years you want to deposit[year>0]:"); scanf("%d",&y);

P = x * pow((1+r),y);

printf("\nAmount at the %d year is %.0lf ",y,P);

int main()
{ int userChoice;

do

{ userChoice = choice();

switch(userChoice)

{ case 1: function1(); break;

case 2: function2(); break;

default: printf("Bye!\n");

while(userChoice > 0 && userChoice < 3);

fflush(stdin);

getchar();

return 0;

You might also like