C Programming Logic: Number Processing and Algorithmic Flow
Identifying the Maximum Value Among Three Integers
This exercise demonstrates how to determine the largest of three provided integer values. The solution encapsulates the comparison logic within a separate function to promote modularity.
#include
int findMaximum(int x, int y, int z) {
int max = (x > y) ? x : y;
return (max > z) ? max : z;
}
int main() {
int val1, val2, val3;
printf("Enter three integers separated by spaces: ");
scanf("%d %d %d", &val1, &val2, &val3);
int result = findMaximum(val1, val2, val3);
printf("The maximum value is: %d\n", result);
return 0;
}
Integer Square Root Calculation with Validation
The following program calculates the integer square root of a positive number strictly less than 1000. It utilizes a loop to enforce input validation, ensuring the user complies with the constraints before proceeding.
#include
#include
int main() {
double userNum;
do {
printf("Please enter a positive number smaller than 1000: ");
scanf("%lf", &userNum);
if (userNum >= 1000) {
printf("Error: Input must be less than 1000. Please try again.\n");
}
} while (userNum >= 1000);
int root = (int)sqrt(userNum);
printf("The integer square root of %.0lf is %d\n", userNum, root);
return 0;
}
Piecewise Function Evaluation
This section handles a mathematical function where the calculation of 'y' depends on the range of the input 'x'. The logic branches based on whether x is less than 1, between 1 and 10, or 10 and greater.
#include
int main() {
int x, y;
printf("Enter the value for x: ");
scanf("%d", &x);
if (x < 1) {
y = x;
} else if (x < 10) {
y = 2 * x - 1;
} else {
y = 3 * x - 11;
}
printf("Result y = %d\n", y);
return 0;
}
Integer Analysis: Digit Counting, Printing, and Reversing
Given a positive integer with no more than five digits, the program performs three distinct operations: calculating the total number of digits, printing each digit sequentially, and outputting the digits in reverse order. This implementation uses recursion for clean digit separation.
#include
// Function to calculate the number of digits
int countDigits(int num) {
int len = 0;
do {
num /= 10;
len++;
} while (num != 0);
return len;
}
// Recursive function to print digits from left to right
void printSequential(int num) {
if (num >= 10) {
printSequential(num / 10);
}
printf("%d ", num % 10);
}
// Recursive function to print digits from right to left
void printReversed(int num) {
printf("%d", num % 10);
if (num >= 10) {
printReversed(num / 10);
}
}
int main() {
int inputNum;
printf("Enter a positive integer (up to 5 digits): ");
scanf("%d", &inputNum);
int digitCount = countDigits(inputNum);
printf("%d is a %d-digit number.\n", inputNum, digitCount);
printf("Digits in sequence: ");
printSequential(inputNum);
printf("\nDigits reversed: ");
printReversed(inputNum);
printf("\n");
return 0;
}
Sorting Four Integers in Ascending Order
The final exercise sorts four user-provided integers from smallest to largest. This implementation uses a simple comparison and swap mechanism to organize the array of numbers.
#include
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sortFourNumbers(int *n1, int *n2, int *n3, int *n4) {
// Bubble sort logic for 4 elements
if (*n1 > *n2) swap(n1, n2);
if (*n2 > *n3) swap(n2, n3);
if (*n3 > *n4) swap(n3, n4);
if (*n1 > *n2) swap(n1, n2);
if (*n2 > *n3) swap(n2, n3);
if (*n1 > *n2) swap(n1, n2);
}
int main() {
int a, b, c, d;
printf("Enter four integers: ");
scanf("%d %d %d %d", &a, &b, &c, &d);
sortFourNumbers(&a, &b, &c, &d);
printf("Sorted sequence: %d %d %d %d\n", a, b, c, d);
return 0;
}