You are on page 1of 5

/* /* /* /*

EXCERSICE 1 program style.c for FE1008 hands-on Name: */ Group: */ Date: */

*/

#include <stdio.h> #define P 3.14159 int main(void) { /* declaration */ double r, a, c; /* get the input */ printf("Enter the radius of the circle= \n"); scanf("%lf", &r); /* compute area and circumference */ a = P * r * r; c = 2 * P * r; /* display the results */ printf("Radius of circle = %8.3f \n", r); printf("Area of circle = %8.3f \n", a); printf("Circumference of circle = %8.3f \n", c); return 0; } EXCERCISE 2 #include <stdio.h> int main(void) { char ch = '#'; int i = 1 , n = 5 ; while (i <= n ) { printf("%*c \n",i, ch); i = i + 1; } while (i >= 1 ) { printf("%*c \n",i, ch); i = i - 1; } return 0; }

/* /* /* /*

EXCERISE 3 Program int_char.c for fe1008 hands-on Name: */ Group: */ Date: */

*/

#include <stdio.h> int main(void) { /* declaration */ char c1, c2, c3; /* c1 c2 c3 define values */ = 'A'; = 'a'; = c2 - c1;

printf("The ASCII code of %c is %d\n", c1, c1); printf("The ASCII code of %c is %d\n", c2, c2); printf("The ASCII code of %c is %d\n", c3, c3); return 0; }

EXCERCISE 4 #include <stdio.h> #include <ctype.h> #include <stdlib.h> int main(void) { char c; printf("Type in a character: \n"); scanf("%c",&c); if (islower(c)) printf("%c is a lowercase letter. \n", c); else printf("%c is a uppercase letter. \n", c); system("PAUSE"); return 0; }

EXCERSIE 5 MATHS #include <stdio.h> #include <math.h> #include <stdlib.h> int main(void) { double a; printf("Enter a real number x = \n"); scanf("%lf", &a);

printf("The absolute value of x is %8.5f \n\n", fabs(a)); printf("Sine of x is %8.5f \n\n", sin(a)); printf("Cosine of x is %8.5f \n\n", cos(a)); printf("Tangent of x is %8.5f \n\n", tan(a)); if (a > 709) printf("Exponential of x is undefined. \n\n"); else printf("Exponential of x is %e \n\n", exp(a)); if (a < 0) printf("Natural Logarithm of x is undefined. \n\n"); else printf("Natural Logarithm of x is %8.5f \n\n", log10(a)); if (a < 0) printf("Square root of x is undefined. \n\n"); else printf("Square root of x is %8.5f \n\n", sqrt(a)); printf("ceil(x) is %8.1f \n\n", ceil(a)); printf("floor(x) is %8.1f \n\n", floor(a)); system("PAUSE"); return 0; } EXCERSIE 6 #include <stdio.h> #include <math.h> #include <stdlib.h> int main(void) { double a; printf("Enter a numerical value: \n"); scanf("%lf", &a); printf("%lf round off to the nearest integar is %8.1f \n\n", a, floor(a)); system("PAUSE"); return 0; }

EXCERSICE 7 #include <stdio.h> #include <math.h> #include <stdlib.h> #define pi 3.14159 int main(void)

{ double a, b, c, dang, rang; printf("Enter the length a of the triangle: \n"); scanf("%lf", &a); if (a < 0) printf("The length a is negative. Please check again. \n\n"); else { printf("Enter the length b of the triangle: \n"); scanf("%lf", &b); if (b < 0) printf("The length b is negative. Please check again. \n\n"); else { printf("Enter the included angle in degree of the triangle: \n"); scanf("%lf", &dang); if (dang < 0) printf("The angle is negative. Please check again. \n\n"); else { rang = (pi/180)*dang; c = sqrt(a*a + b*b - 2*a*b*cos(rang)); printf("The length of the third side c of the triangle is %8.3f \n\n", c); } } } system("Pause"); return 0; }

EXCERSIE 8 /*PROGRAMME 5.3 Example on the use of rand() */ #include <stdio.h> #include <stdlib.h> int main(void) { printf("Three random integers are:\n %d %d %d\n", rand(), rand(), rand()); system("PAUSE"); return 0; } /*PROGRAMME 5.4 Example on the use of srand() */ #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { srand((unsigned) time(NULL)); printf("Three random integers are:\n %d %d %d\n", rand(),rand(),rand()); system("PAUSE");

return 0; }

EXCERSIE 8 PART B #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int times = 1, max_time = 100, interval = 1; printf("No. of times Outcome\n"); printf("-----------------------\n"); srand ( time(NULL) ); while (times <= max_time) { printf("%d %d\n", times, rand() % 6 + 1); times = times + interval; } system("PAUSE"); return 0; }

You might also like