Selection Sort for Integer Arrays
This implementation sorts a sequence of $N$ integers in descending order using the selection sort algorithm. The program identifies largest remaining element in each iteration and swaps it into its correct position.
#include <stdio.h>
int main() {
int count, list[10];
if (scanf("%d", &count) != 1) return 0;
for (int i = 0; i < count; i++) {
scanf("%d", &list[i]);
}
for (int i = 0; i < count - 1; i++) {
int pivot = i;
for (int j = i + 1; j < count; j++) {
if (list[j] > list[pivot]) {
pivot = j;
}
}
int temp = list[i];
list[i] = list[pivot];
list[pivot] = temp;
}
for (int i = 0; i < count; i++) {
printf("%d%s", list[i], (i == count - 1) ? "" : " ");
}
return 0;
}
Frequency Analysis of Individual Digits
This logic processes a set of integers to determine which digit occurs most frequently across all input numbers. It outputs the highest frequency followed by the digits that achieved that frequency.
#include <stdio.h>
int main() {
int n, digit_counts[10] = {0}, peak = 0;
scanf("%d", &n);
while (n--) {
int val;
scanf("%d", &val);
if (val == 0) digit_counts[0]++;
while (val > 0) {
digit_counts[val % 10]++;
val /= 10;
}
}
for (int i = 0; i < 10; i++) {
if (digit_counts[i] > peak) peak = digit_counts[i];
}
printf("%d:", peak);
for (int i = 0; i < 10; i++) {
if (digit_counts[i] == peak) printf(" %d", i);
}
return 0;
}
Validating Upper Triangular Matrices
A square matrix is upper triangular if all elements below the main diagonal are zero. This program checks multiple test cases for this property.
#include <stdio.h>
int main() {
int tests;
scanf("%d", &tests);
while (tests--) {
int size, flag = 1;
scanf("%d", &size);
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
int val;
scanf("%d", &val);
if (r > c && val != 0) flag = 0;
}
}
printf("%s\n", flag ? "YES" : "NO");
}
return 0;
}
Computing Row Sums in a Matrix
This utility reads an $M \times N$ matrix and calculates the sum of each individual row, printing the results sequentially.
#include <stdio.h>
int main() {
int rows, cols;
scanf("%d %d", &rows, &cols);
for (int i = 0; i < rows; i++) {
int current_row_sum = 0;
for (int j = 0; j < cols; j++) {
int element;
scanf("%d", &element);
current_row_sum += element;
}
printf("%d\n", current_row_sum);
}
return 0;
}
Locating Matrix Saddle Points
A saddle point is defined as an element that is the maximum in its row and the minimum in its column. This program finds and outputs the coordinates of such a point if it exists.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int grid[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &grid[i][j]);
int found = 0;
for (int i = 0; i < n; i++) {
int max_col = 0;
for (int j = 1; j < n; j++) {
if (grid[i][j] >= grid[i][max_col]) max_col = j;
}
int is_saddle = 1;
for (int k = 0; k < n; k++) {
if (grid[k][max_col] < grid[i][max_col]) {
is_saddle = 0;
break;
}
}
if (is_saddle) {
printf("%d %d", i, max_col);
found = 1;
break;
}
}
if (!found) printf("NONE");
return 0;
}
Counting Uppercase Consonants
This program reads a string and counts how many characters are uppercase consonants (uppercase letters excluding A, E, I, O, U).
#include <stdio.h>
int main() {
char c;
int consonants = 0;
while ((c = getchar()) != '\n' && c != EOF) {
if (c >= 'A' && c <= 'Z') {
if (c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U') {
consonants++;
}
}
}
printf("%d", consonants);
return 0;
}
Alphabetical Character Replacement
This script performs a substitution cipher where each uppercase letter is replaced by its opposite in the alphabet (e.g., 'A' to 'Z', 'B' to 'Y').
#include <stdio.h>
int main() {
char input_char;
while ((input_char = getchar()) != '\n' && input_char != EOF) {
if (input_char >= 'A' && input_char <= 'Z') {
input_char = 'A' + 'Z' - input_char;
}
putchar(input_char);
}
return 0;
}
Converting Hexadecimal Strings to Decimal
Extracts hexadecimal digits from a string (terminated by '#') and converts the sequence into a signed decimal integer.
#include <stdio.h>
int get_hex_digit(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
int main() {
char raw[100];
long long result = 0;
int sign = 1, started = 0, minus_pending = 0;
scanf("%[^#]", raw);
for (int i = 0; raw[i]; i++) {
int val = get_hex_digit(raw[i]);
if (raw[i] == '-' && !started) minus_pending = 1;
if (val != -1) {
if (!started && minus_pending) sign = -1;
result = result * 16 + val;
started = 1;
}
}
printf("%lld", result * sign);
return 0;
}
Sorting a List of Strings
Accepts five strings and sorts them in lexicographical order using a standard string comparison and swapping method.
#include <stdio.h>
#include <string.h>
int main() {
char items[5][80], buffer[80];
for (int i = 0; i < 5; i++) scanf("%s", items[i]);
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (strcmp(items[i], items[j]) > 0) {
strcpy(buffer, items[i]);
strcpy(items[i], items[j]);
strcpy(items[j], buffer);
}
}
}
printf("After sorted:\n");
for (int i = 0; i < 5; i++) printf("%s\n", items[i]);
return 0;
}
Student Score Statistics
Uses dynamic memory allocation to store student scores and calculates the average, maximum, and minimum values.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
double *grades, sum = 0, high = -1.0, low = 101.0;
if (scanf("%d", &n) != 1 || n <= 0) return 0;
grades = (double*)malloc(n * sizeof(double));
for (int i = 0; i < n; i++) {
scanf("%lf", &grades[i]);
sum += grades[i];
if (grades[i] > high) high = grades[i];
if (grades[i] < low) low = grades[i];
}
printf("average = %.2f\nmax = %.2f\nmin = %.2f", sum / n, high, low);
free(grades);
return 0;
}
Time Increment Calculator
Given a time in HH:MM:SS format and an integer $n$ (seconds), this program calculates the new time after adding $n$ seconds, handling wrap-around for minutes, hours, and days.
#include <stdio.h>
struct Clock {
int h, m, s;
};
int main() {
struct Clock t;
int offset;
scanf("%d:%d:%d %d", &t.h, &t.m, &t.s, &offset);
long total_sec = t.h * 3600 + t.m * 60 + t.s + offset;
total_sec %= 86400;
printf("%02ld:%02ld:%02ld", total_sec / 3600, (total_sec % 3600) / 60, total_sec % 60);
return 0;
}
Adding 2D Vectors
Computes the sum of two 2D vectors and formats the output. If a coordinate's absolute value is less than 0.05, it is rounded to 0.0 to avoid displaying negative zero.
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
double res_x = x1 + x2, res_y = y1 + y2;
if (fabs(res_x) < 0.05) res_x = 0.0;
if (fabs(res_y) < 0.05) res_y = 0.0;
printf("(%.1f, %.1f)\n", res_x, res_y);
return 0;
}
Book Search by Price
Stores book information in a structure and identifies the books with the highest and lowest prices from a list of $N$ entries.
#include <stdio.h>
#include <string.h>
struct Catalog {
char title[35];
double cost;
};
int main() {
int count;
scanf("%d", &count);
struct Catalog list[10];
int high_idx = 0, low_idx = 0;
for (int i = 0; i < count; i++) {
getchar();
fgets(list[i].title, 35, stdin);
list[i].title[strcspn(list[i].title, "\n")] = 0;
scanf("%lf", &list[i].cost);
if (list[i].cost > list[high_idx].cost) high_idx = i;
if (list[i].cost < list[low_idx].cost) low_idx = i;
}
printf("%.2f, %s\n", list[high_idx].cost, list[high_idx].title);
printf("%.2f, %s", list[low_idx].cost, list[low_idx].title);
return 0;
}
Sorting Contacts by Birth Date
Maintains a list of contacts and sorts them chronologically based on their birth dates.
#include <stdio.h>
struct User {
char name[15];
int dob;
char phone[20];
};
int main() {
int n;
scanf("%d", &n);
struct User group[10], swap;
for (int i = 0; i < n; i++) {
scanf("%s %d %s", group[i].name, &group[i].dob, group[i].phone);
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (group[j].dob > group[j + 1].dob) {
swap = group[j];
group[j] = group[j + 1];
group[j + 1] = swap;
}
}
}
for (int i = 0; i < n; i++) {
printf("%s %d %s\n", group[i].name, group[i].dob, group[i].phone);
}
return 0;
}