Converting Uppercase to Lowercase Using ASCII Values
The American Standard Code for Information Interchange (ASCII) defines numeric values for characters. Capital letters range from 65 (A) to 90 (Z), while lowercase letters range from 97 (a) to 122 (z). The difference between corresponding uppercase and lowercase letters is exactly 32.
Problem Description
Read a capital letter from keyboard input, convert it to lowercase, display the lowercase letter along with its two adjacent letters, and output their respective ASCII numeric codes.
Implementation
#include <stdio.h>
int main()
{
char original, converted, prev, next;
printf("Enter an uppercase letter:\n");
scanf("%c", &original);
converted = original + 32;
prev = converted - 1;
next = converted + 1;
printf("Resulting letters: %c, %c, %c\n", prev, converted, next);
printf("ASCII values: %d, %d, %d\n", prev, converted, next);
return 0;
}
The conversion relies on the fixed 32-unit gap between uppercase and lowercase ASCII values. By adding 32 to the input character, we obtain its lowercase equivalent.
Reversing a Three-Digit Integer
Problem Description
Accept a three-digit integer from user input and output its digits in reverse order. For instance, input 123 should produce 321.
Implementation
#include <stdio.h>
int main()
{
int input, hundreds, tens, units, reversed;
printf("Enter a three-digit integer:\n");
scanf("%d", &input);
hundreds = input / 100;
tens = (input % 100) / 10;
units = input % 10;
reversed = units * 100 + tens * 10 + hundreds;
printf("Original: %d, Reversed: %d\n", input, reversed);
return 0;
}
Integer division extracts the hundreds digit, while the modulo operator (%) retrieves the tens and units digits. Combining these with positional weighting (100, 10, 1) reconstructs the reversed number.
Hospital Billing Calculator
Problem Description
Create a program that calculates patient bills at a hospital reception desk. The program should accept multiple fee categories (medication, examination, materials, bed charges, observation fees, nursing fees), compute the total amount due, prompt for payment received, and calculate the change to return.
Implementation
#include <stdio.h>
int main()
{
float medication, examFee, materials, bedCharge, observation, nursing;
float total, paymentReceived, change;
printf("Enter medication, exam, materials, bed, observation, nursing fees:\n");
scanf("%f,%f,%f,%f,%f,%f", &medication, &examFee, &materials,
&bedCharge, &observation, &nursing);
total = medication + examFee + materials + bedCharge + observation + nursing;
printf("Amount due: %.2f yuan\n", total);
printf("Payment received: ");
scanf("%f", &paymentReceived);
change = paymentReceived - total;
printf("Receipt:\n");
printf(" Amount due: %6.2f yuan\n", total);
printf(" Payment: %6.2f yuan\n", paymentReceived);
printf(" Change: %6.2f yuan\n", change);
return 0;
}
This sequential program accumulates six distinct fee types into a total, then subtracts the payment received to determine the correct change amount.
Piecewise Function Evaluation
Problem Description
Implement a program that evaluates a piecewise function defined as:
- y = x when x < 1
- y = 2x - 1 when 1 ≤ x < 10
- y = 3x - 1 when x ≥ 10
Implementation
#include <stdio.h>
int main()
{
float x, y;
printf("Enter value for x: ");
scanf("%f", &x);
if (x < 1)
{
y = x;
}
else if (x < 10)
{
y = 2 * x - 1;
}
else
{
y = 3 * x - 1;
}
printf("Result: y = %.2f\n", y);
return 0;
}
The cascading if-else structure evaluates conditions in sequence. Each subsequent condition only executes if previous conditions were false. The second condition (x < 10) inherent means x ≥ 1 due to the else-if chain.
Solving Simultaneous Equations: Chicken and Rabbit Problem
Problem Description
Given the total number of heads (h) and feet (f) for chickens and rabbits in a cage, determine how many of each animal exists. Each chicken has 1 head and 2 feet; each rabbit has 1 head and 4 feet.
Mathematical Formulation
Let x represent chickens and y represent rabbits:
- x + y = h (total heads)
- 2x + 4y = f (total feet)
Solving these equations:
- x = (4h - f) / 2
- y = (f - 2h) / 2
Implementation
#include <stdio.h>
int main()
{
int totalHeads, totalFeet, chickenCount, rabbitCount;
printf("Enter total heads and total feet:\n");
scanf("%d,%d", &totalHeads, &totalFeet);
if (totalHeads > 0 && totalFeet > 0)
{
chickenCount = (4 * totalHeads - totalFeet) / 2;
rabbitCount = (totalFeet - 2 * totalHeads) / 2;
printf("Chickens: %d, Rabbits: %d\n", chickenCount, rabbitCount);
}
else
{
printf("Invalid input values\n");
}
return 0;
}
The solution requires deriving formulas from the system of linear equations before implementation. Input validation ensures both values are positive, as negative or zero values cannot represent actual animal counts.
Point Classification Within Circular Regions
Problem Description
Four circular towers exist with centers at (2,2), (-2,2), (2,-2), and (-2,-2). Each tower has a radius of 1 and height of 10 meters. Given any coordinate point, output the tower height if the point lies within any tower's boundary; otherwise output 0.
Implementation
#include <stdio.h>
#include <math.h>
int main()
{
const int towerHeight = 10;
float pointX, pointY;
float centerX1 = 2, centerY1 = 2;
float centerX2 = -2, centerY2 = 2;
float centerX3 = -2, centerY3 = -2;
float centerX4 = 2, centerY4 = -2;
float dist1, dist2, dist3, dist4;
int resultHeight;
printf("Enter point coordinates (x,y): ");
scanf("%f,%f", &pointX, &pointY);
dist1 = sqrt(pow(pointX - centerX1, 2) + pow(pointY - centerY1, 2));
dist2 = sqrt(pow(pointX - centerX2, 2) + pow(pointY - centerY2, 2));
dist3 = sqrt(pow(pointX - centerX3, 2) + pow(pointY - centerY3, 2));
dist4 = sqrt(pow(pointX - centerX4, 2) + pow(pointY - centerY4, 2));
if (dist1 > 1 && dist2 > 1 && dist3 > 1 && dist4 > 1)
{
resultHeight = 0;
}
else
{
resultHeight = towerHeight;
}
printf("Height at point: %d meters\n", resultHeight);
return 0;
}
The Euclidean distance formula determines how far the input point lies from each tower center. If all distances exceed the radius of 1, the point lies outside all towers and the result is 0. Otherwise, atleast one tower contains the point.