The document contains practical exercises for learning C programming, including programs for adding two numbers, assigning students to halls based on gender, calculating real roots of quadratic equations, and listing even and odd numbers. It also includes a simple calculator program that performs basic arithmetic operations. Additionally, it lists commonly used library functions in C.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
11 views16 pages
COS 102 Lecture Note 8
The document contains practical exercises for learning C programming, including programs for adding two numbers, assigning students to halls based on gender, calculating real roots of quadratic equations, and listing even and odd numbers. It also includes a simple calculator program that performs basic arithmetic operations. Additionally, it lists commonly used library functions in C.
Practical 1: A C Program to Add Two Numbers #include <stdio.h> int main() { num2 is just the variable. // Step 1: Declare your variables &num2 means "the address of float num1, num2, sum; num2". // Step 2: Ask the user for input printf("Enter first number: "); scanf("%f", &num1); printf("Enter second number: "); Find the product scanf("%f", &num2); and average of // Step 3: Calculate sum the two numbers sum = num1 + num2; // Step 4: Display the result printf("The sum of %f and %f is %3.3f \n", num1, num2, sum); return 0; } Practical 2: A C Program to assign male students to ADAM HALL and Female students to EVE hall display the name of the respective students #include <stdio.h> int main() { char name[50]; 0 49 char gender; // Ask for student name printf("Enter student name: (no spaces): "); scanf("%s", name); // Ask for student gender printf("Enter gender (M for Male / F for Female): "); scanf(" %c", &gender); // Assign to hall based on gender if (gender == 'M' || gender == 'm’) { printf("Student Name: %s\n", name); printf("Assigned Hall: ADAM HALL\n"); } else if (gender == 'F' || gender == 'f') { printf("Student Name: %s\n", name); printf("Assigned Hall: EVE HALL\n"); } else { printf("Invalid gender entered.\n"); } return 0; } #include <stdio.h> #include <string.h> int main() { sizeof(name) tells fgets the maximum char name[50]; number of characters it can safely read — in char gender; this case, 50 (the size of the array). // Ask for student name stdin means standard input — in this case, printf("Enter student name: "); the keyboard fgets(name, sizeof(name), stdin); // Ask for student gender printf("Enter gender (M for Male / F for Female): "); scanf(" %c", &gender); // Assign to hall based on gender if (gender == 'M' || gender == 'm') { printf("Student Name: %s\n", name); printf("Assigned Hall: ADAM HALL\n"); } else if (gender == 'F' || gender == 'f') { printf("Student Name: %s\n", name); printf("Assigned Hall: EVE HALL\n"); } else { printf("Invalid gender entered.\n"); } return 0; } Practical 3: Write a C program to calculate the real roots (not complex roots) of the quadratic equation ax2 + bx + c Practical 3: Write a C program to calculate the real roots (not complex roots) of the quadratic equation ax2 + bx + c which is given as
In addition to the result:
display “Roots are real and distinct”, if (discriminant > 0) display “Roots are real and equal”, if (discriminant = 0) Else display “No real roots.” Practical 3: #include <stdio.h> else if (discriminant == 0) { #include <math.h> root1 = -b / (2 * a); int main() printf("Roots are real and equal:\n"); { printf("Root = %.2 f\n", root1); int a, b, c; } float discriminant, root1, root2; else { // Ask the user for coefficients printf("No real roots.\n"); printf("Enter coefficients a, b, and c: "); } scanf("%d %d %d", &a, &b, &c); return 0; // Calculate the discriminant } discriminant = b * b - 4 * a * c; if (discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2 * a); root2 = (-b - sqrt(discriminant)) / (2 * a); printf("Roots are real and distinct:\n"); printf("Root 1 = %.2f \n", root1); printf("Root 2 = %.2f \n", root2); } Some Commonly Used Library Functions S/N FUNCTION PURPOSE 1. abs (i) Return the absolute value of variable i 2. cos (d) Return the cosine of variable d 3. log (d) Return the natural logarithm of variable d 4. pow (d1,d2) Return dl raised to the d2 power. 5. sin (d) Return the sine of variable d. 6. sqrt (d) Return the square root of variable d
7. Tan (d) Return the tangent of variable d.
8. tolower (c) Convert content of variable “c” to lowercase. 9. toupper(c) Convert content of variable “c” to uppercase. Practical 4: A simple C program that lists all the EVEN and ODD numbers between 1 and 100: #include <stdio.h> int main() { Practical 4: int i; // Print Even numbers printf("Even numbers between 1 and 100:\n"); for (i = 1; i <= 100; i++) { if (i % 2 == 0) { printf("%d ", i); } } printf("\n\n"); // Add a blank line between the two lists // Print Odd numbers printf("Odd numbers between 1 and 100:\n"); for (i = 1; i <= 100; i++) { if (i % 2 != 0) { printf("%d ", i); } } printf("\n"); return 0; } Class Activity Using if...else control structure , write a C program that works as a simple calculator, your calculator should be able to perform simple addition, subtraction, multiplication and division operation, your results must be in two decimal places. #include <stdio.h> int main () { float value1, value2, ans; char operator; printf("Which operation do you want to perform ?\n" ); scanf("%c", &operator); if( operator == '+') { printf("supply your first value\n" ); scanf("%f", &value1); printf("supply your second value\n" ); scanf("%f", &value2); ans = value1 + value2; printf(" the result of your operation is %.2f \n", ans ); } else if (operator == '-') {
printf("supply your first value\n" );
scanf("%f", &value1); printf("supply your second value\n" ); scanf("%f", &value2); ans = value1 - value2; printf(" the result of your operation is %.2f \n", ans ); } else if (operator == '*') { printf("supply your first value\n" ); scanf("%f", &value1); printf("supply your second value\n" ); scanf("%f", &value2); ans = value1 * value2; printf(" the result of your operation is %.2f \n", ans ); } else { printf("supply your first value\n" ); scanf("%f", &value1); printf("supply your second value\n" ); scanf("%f", &value2); ans = value1 / value2; printf(" the result of your operation is %.2f \n", ans ); } return 0; }