C Programming Solutions: Number Theory, Series Summation, and Pattern Generation
Perfect Number Identification
This program scans a range of integers to identify "perfect numbers." A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). The solution involves iterating through the specified range, summing the divisors for each number, and printing the factorization equation if the number is perfect.
#include <stdio.h>
int main() {
int lower, upper, found = 0;
if (scanf("%d %d", &lower, &upper) != 2) return 1;
for (int i = lower; i <= upper; i++) {
int divisor_sum = 0;
// Calculate sum of proper divisors
for (int k = 1; k < i; k++) {
if (i % k == 0) {
divisor_sum += k;
}
}
// Check if perfect and print factorization
if (divisor_sum == i) {
printf("%d = 1", i);
for (int k = 2; k < i; k++) {
if (i % k == 0) {
printf(" + %d", k);
}
}
printf("\n");
found = 1;
}
}
if (!found) {
printf("None\n");
}
return 0;
}
Calculating the Mathematical Constant e
This application approximates the mathematical constant e using the Taylor series expansion: e = 1 + 1/1! + 1/2! + ... + 1/n!. The algorithm calculates the series sum up to the n-th term as specified by the user, maintaining high precision by iteratively updating the factorial term.
#include <stdio.h>
int main() {
int terms;
double result = 1.0;
double factorial_term = 1.0;
if (scanf("%d", &terms) != 1) return 0;
for (int i = 1; i <= terms; ++i) {
factorial_term /= i; // Efficiently calculates 1/n! from 1/(n-1)!
result += factorial_term;
}
printf("%.8f\n", result);
return 0;
}
Calculating Average Scores Removing Outliers
In competitive judging, it is common to discard the highest and lowest scores to mitigate bias. This program reads a set of scores, identifies the maximum and minimum values, sums the remaining scores, and computes the arithmetic mean.
#include <stdio.h>
#include <float.h>
int main() {
int count;
scanf("%d", &count);
double total = 0.0;
double max_score = -DBL_MAX;
double min_score = DBL_MAX;
double score;
for (int i = 0; i < count; ++i) {
scanf("%lf", &score);
total += score;
if (score > max_score) max_score = score;
if (score < min_score) min_score = score;
}
double average = (total - max_score - min_score) / (count - 2);
printf("%.2f\n", average);
return 0;
}
Summation of Repeated Digit Sequences
This task involves generating a sequence where a digit a repeats n times (e.g., a, aa, aaa) and summing these values. The program constructs each term iteratively without using string conversion or power functions, relying instead on arithmetic accumulation.
#include <stdio.h>
int main() {
int digit, repetitions;
scanf("%d %d", &digit, &repetitions);
long long current_term = 0;
long long total_sum = 0;
for (int i = 0; i < repetitions; ++i) {
current_term = current_term * 10 + digit;
total_sum += current_term;
}
printf("s = %lld\n", total_sum);
return 0;
}
Generating Diamond Star Patterns
This logic outputs a diamond shape composed of asterisks. The height n is a user-defined odd integer. The solution calculates the number of spaces and asterisks required for each row by determining the distance of the row index from the center line.
#include <stdio.h>
int main() {
int height;
scanf("%d", &height);
int mid_point = height / 2;
for (int row = 0; row < height; row++) {
// Distance from the center axis
int dist = (row > mid_point) ? (height - 1 - row) : row;
int space_count = mid_point - dist;
// Print leading spaces
for (int s = 0; s < space_count; s++) {
printf(" ");
}
// Print stars
for (int a = 0; a < 2 * dist + 1; a++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Printing the Lower Triangular Multiplication Table
The program generates a multiplication table from 1×1 to N×N in a lower triangular format. It uses nested loops where the outer loop represents the row and the inner loop iterates up to the current row number, formatting the output to align columns.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
// Standard format: j * i = product
printf("%d*%d=%-4d", j, i, i * j);
}
printf("\n");
}
return 0;
}
Summation of Primes by Index Range
This algorithm calculates the sum of prime numbers based on their ordinal position. Given two indices n and m, the program identifies the n-th prime through the m-th prime and returns their cumulative sum.
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int val) {
if (val < 2) return false;
for (int i = 2; i * i <= val; ++i) {
if (val % i == 0) return false;
}
return true;
}
int main() {
int start_index, end_index;
scanf("%d %d", &start_index, &end_index);
int prime_count = 0;
long long sum = 0;
int candidate = 2;
while (prime_count <= end_index) {
if (is_prime(candidate)) {
prime_count++;
if (prime_count >= start_index && prime_count <= end_index) {
sum += candidate;
}
}
candidate++;
}
printf("%lld\n", sum);
return 0;
}
Numeric Digit to Text Conversion
This utility converts an integer into its corresponding English word representation. It handles positive integers, zero, and negative numbers. The approach extracts digits individually and maps them to their string equivalents using a lookup array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_word(int digit) {
const char* map[] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
printf("%s", map[digit]);
}
int main() {
int number;
scanf("%d", &number);
if (number < 0) {
printf("minus ");
number = abs(number);
}
char buffer[20];
sprintf(buffer, "%d", number);
int len = strlen(buffer);
for (int i = 0; i < len; ++i) {
print_word(buffer[i] - '0');
if (i < len - 1) {
printf(" ");
}
}
printf("\n");
return 0;
}