Generating Pascal's Triangle
The following implementation generates Pascal's Triangle for a specified number of rows. It uses a 2D array to store values where each number is the sum of the two numbers directly above it. The output includes formatting to center-align the triangle.
#include <stdio.h>
int main() {
int rows, i, j, space;
scanf("%d", &rows);
int pascal[rows][rows];
// Construct the triangle
for (i = 0; i < rows; i++) {
for (j = 0; j <= i; j++) {
if (j == 0 || j == i) {
pascal[i][j] = 1;
} else {
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
}
}
}
// Print the triangle with formatting
for (i = 0; i < rows; i++) {
for (space = 0; space < rows - i - 1; space++) {
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%5d", pascal[i][j]);
}
printf("\n");
}
return 0;
}
Product of Digits Calculation
This program accepts an integer and calculates the product of its individual digits. It extracts each digit using the modulo operator and accumulates the product.
#include <stdio.h>
int main() {
int number, digit;
long product = 1;
scanf("%d", &number);
while (number != 0) {
digit = number % 10;
product *= digit;
number /= 10;
}
printf("%ld\n", product);
return 0;
}
Recursive Sequence Analysis
Given a sequence where f(1)=1, f(2)=1, and f(n)=3*f(n-1)+2*f(n-2) for n>2, this code calculates the nth term and the sum of the first n terms.
#include <stdio.h>
int calculateTerm(int n) {
if (n == 1 || n == 2) return 1;
if (n == 3) return 5;
if (n == 4) return 17;
return 3 * calculateTerm(n - 1) + 2 * calculateTerm(n - 2);
}
int main() {
int n, currentTerm, totalSum = 0;
scanf("%d", &n);
if (n >= 1) {
for (int i = 1; i <= n; i++) {
currentTerm = calculateTerm(i);
totalSum += currentTerm;
}
}
printf("%d\n%d", currentTerm, totalSum);
return 0;
}
Digit Frequency Counter
This solution counts how many times a specific digit (x) appears in all integers from 1 to n. It iterates through the range and checks every digit of each number.
#include <stdio.h>
int main() {
int limit, target, count = 0, current, digit;
scanf("%d %d", &limit, &target);
for (int i = 1; i <= limit; i++) {
current = i;
while (current > 0) {
digit = current % 10;
if (digit == target) {
count++;
}
current /= 10;
}
}
printf("%d", count);
return 0;
}
Diamond Shape Generator
Based on an integer input n, this program prints a diamond pattern consisting of 2n-1 rows. It manages spacing and asterisks to form the upper and lower halves of the diamond.
#include <stdio.h>
int main() {
int n, i, j, k;
scanf("%d", &n);
// Upper half including middle
for (i = 1; i <= n; i++) {
for (k = 1; k <= n - i; k++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}
// Lower half
for (i = n - 1; i >= 1; i--) {
for (k = 1; k <= n - i; k++) printf(" ");
for (j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}
return 0;
}
GCD and LCM Calculation
This program computes the Greatest Common Divisor (GCD) using the Euclidean algorithm (recursion) and then derives the Least Common Multiple (LCM) from the product of the numbers divided by their GCD.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int num1, num2, divisor, multiple;
scanf("%d %d", &num1, &num2);
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
divisor = gcd(num1, num2);
multiple = (num1 * num2) / divisor;
printf("Max GCD: %d\nMin LCM: %d", divisor, multiple);
return 0;
}
LCM by Incremental Search
An alternative method to find the LCM involves checking multiples of the larger number until a value divisible by both numbers is found.
#include <stdio.h>
int findLCM(int a, int b) {
int max = (a > b) ? a : b;
while (1) {
if (max % a == 0 && max % b == 0) {
return max;
}
max++;
}
}
int main() {
int val1, val2;
scanf("%d %d", &val1, &val2);
printf("LCM: %d\n", findLCM(val1, val2));
return 0;
}
Day of the Year Calculation
This code determines the ordinal day of the year for a given date (YYYY-MM-DD). It accounts for the varying days in months and checks for leap years to adjust February's day count.
#include <stdio.h>
int main() {
int year, month, day, totalDays = 0;
scanf("%d-%d-%d", &year, &month, &day);
for (int m = 1; m < month; m++) {
switch (m) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
totalDays += 31;
break;
case 4: case 6: case 9: case 11:
totalDays += 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
totalDays += 29;
else
totalDays += 28;
break;
}
}
totalDays += day;
printf("%d", totalDays);
return 0;
}
Regional Shipping Cost Calculator
A billing system that calculates shipping costs based on zone code and weight. The logic handles weight rounding up to the next integer for pricing calculations and applies different base rates and multipliers per zone.
#include <stdio.h>
int main() {
int zone;
float weight, basePrice, rate, finalPrice;
scanf("%d,%f", &zone, &weight);
// Determine weight charge (rounding up partial units)
int weightCharge = (int)weight;
if (weight > weightCharge) {
weightCharge = weightCharge + 1;
} else if (weight == weightCharge && weight > 1) {
weightCharge = weightCharge; // No change for integers
} else {
weightCharge = 1; // Minimum 1 unit
}
// Adjust calculation logic for the formula: Base + (Weight - 1) * Rate
float extraWeight = (weightCharge > 1) ? (weightCharge - 1) : 0;
switch (zone) {
case 0:
basePrice = 10.0; rate = 3.0;
break;
case 1:
basePrice = 10.0; rate = 4.0;
break;
case 2:
basePrice = 15.0; rate = 5.0;
break;
case 3:
basePrice = 15.0; rate = 6.5;
break;
case 4:
basePrice = 15.0; rate = 10.0;
break;
default:
printf("Error in Area\nPrice: 0.00\n");
return 0;
}
finalPrice = basePrice + extraWeight * rate;
printf("Price: %.2f\n", finalPrice);
return 0;
}