Solutions to Basic C Programming Exercises
Exercise 2-1: Programming in C is fun!
This task requires printing a specific string to the console.
#include <stdio.h>
int main() {
printf("Programming in C is fun!");
return 0;
}
Exercise 2-3: Output Inverted Triangle Pattern
Prints an inverted triangle pattern using asterisks and spaces with specific alignment.
#include <stdio.h>
int main() {
printf("* * * *\n");
printf(" * * *\n");
printf(" * *\n");
printf(" *");
return 0;
}
Exercise 2-4: Temperature Conversion
Converts a fixed Fahrenheit value (150) to Celsius using the standard conversion formula.
#include <stdio.h>
int main() {
int f_temp = 150;
int c_temp;
c_temp = 5 * (f_temp - 32) / 9;
printf("fahr = %d, celsius = %d", f_temp, c_temp);
return 0;
}
Exercise 2-6: Calculate Free Fall Distance
Calculates the distance an object falls in 3 seconds given gravity is 10 m/s².
#include <stdio.h>
int main() {
double time = 3.0;
double gravity = 10.0;
double distance = 0.5 * gravity * time * time;
printf("height = %.2f", distance);
return 0;
}
Exercise 2-8: Calculate Celsius Temperature
Reads an integer Fahrenheit value from input and converts it to Celsius.
#include <stdio.h>
int main() {
int f_val, c_val;
scanf("%d", &f_val);
c_val = 5 * (f_val - 32) / 9;
printf("Celsius = %d", c_val);
return 0;
}
Exercise 2-9: Integer Arithmetic Operations
Performs addition, subtraction, multiplication, and division on two integers.
#include <stdio.h>
int main() {
int num1, num2;
scanf("%d %d", &num1, &num2);
printf("%d + %d = %d\n", num1, num2, num1 + num2);
printf("%d - %d = %d\n", num1, num2, num1 - num2);
printf("%d * %d = %d\n", num1, num2, num1 * num2);
printf("%d / %d = %d\n", num1, num2, num1 / num2);
return 0;
}
Exercise 2-10: Calculate Piecewise Function 1
Computes a function where $f(x) = 1/x$ if $x \neq 0$, otherwise $f(x) = 0$.
#include <stdio.h>
int main() {
double x, y;
scanf("%lf", &x);
if (x != 0) {
y = 1.0 / x;
} else {
y = 0.0;
}
printf("f(%.1f) = %.1f", x, y);
return 0;
}
Exercise 2-11: Calculate Piecewise Function 2
Computes a function involving square root for non-negative inputs and a polynomial expression for negative inputs.
#include <stdio.h>
#include <math.h>
int main() {
double x, res;
scanf("%lf", &x);
if (x >= 0) {
res = sqrt(x);
} else {
res = pow(x + 1, 2) + 2 * x + 1.0 / x;
}
printf("f(%.2f) = %.2f", x, res);
return 0;
}
Exercise 2-12: Output Fahrenheit-Celsius Conversion Table
Generates a conversion table within a valid range, stepping by 2 degrees.
#include <stdio.h>
int main() {
int low, high;
double celsius;
scanf("%d %d", &low, &high);
if (low > high || high > 100 || low <= 0) {
printf("Invalid.");
} else {
printf("fahr celsius\n");
for (int f = low; f <= high; f += 2) {
celsius = 5.0 * (f - 32)0 / 9.0;
printf("%d%6.1f\n", f, celsius);
}
}
return 0;
}
Exercise 2-13: Sum of Nth Reciprocals
Calculates the sum of the series $1 + 1/2 + ... + 1/n$.
#include <stdio.h>
int main() {
int n, i;
double total = 0.0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
total += 1.0 / i;
}
printf("sum = %lf", total);
return 0;
}
Exercise 2-14: Sum of Odd Reciprocals
Calculates the sum of reciprocals of the first N odd numbers.
#include <stdio.h>
int main() {
int count, i;
double sum = 0.0;
scanf("%d", &count);
for (i = 1; i <= count; i++) {
sum += 1.0 / (2 * i - 1);
}
printf("sum = %lf", sum);
return 0;
}
Exercise 2-15: Sum of Simple Alternating Series
Calculates the sum of a series with alternating signs: $1 - 1/4 + 1/7 - 1/10 + ...$.
#include <stdio.h>
int main() {
int n, i;
int sign = 1;
double sum = 0.0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += 1.0 / (sign * (3 * i - 2));
sign = -sign;
}
printf("sum = %.3lf", sum);
return 0;
}
Exercise 2-17: Generate Powers of 3 Table
Prints values of 3 raised to the power of 0 to n.
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
long long val;
scanf("%d", &n);
for (i = 0; i <= n; i++) {
val = (long long)pow(3, i);
printf("pow(3,%d) = %lld\n", i, val);
}
return 0;
}
Exercise 2-18: Calculate Combination Number
Calculates the combination number $C(n, m)$ using factorials.
#include <stdio.h>
double getFactorial(int num);
int main() {
int m, n;
double result;
scanf("%d %d", &m, &n);
result = getFactorial(n) / (getFactorial(m) * getFactorial(n - m));
printf("result = %.0lf", result);
return 0;
}
double getFactorial(int num) {
double product = 1.0;
for (int i = 1; i <= num; i++) {
product *= i;
}
return product;
}
Problem 2-1: Find Integer Mean
Calculates the sum and average of four integers.
#include <stdio.h>
int main() {
int a, b, c, d;
int sum;
double avg;
scanf("%d %d %d %d", &a, &b, &c, &d);
sum = a + b + c + d;
avg = sum / 4.0;
printf("Sum = %d; Average = %.1f", sum, avg);
return 0;
}
Problem 2-2: Tiered Electricity Pricing
Calculates electricity cost based on usage with different rates for usage above and below 50 kWh.
#include <stdio.h>
int main() {
int usage;
double cost = 0.0;
scanf("%d", &usage);
if (usage >= 0 && usage <= 50) {
cost = usage * 0.53;
} else if (usage > 50) {
cost = 50 * 0.53 + (usage - 50) * 0.58;
} else {
printf("Invalid Value!");
return 0;
}
printf("cost = %.2lf", cost);
return 0;
}
Problem 2-3: Partial Sum of Squares and Reciprocals
Calculates the sum of squares plus reciprocals for a range of integers.
#include <stdio.h>
#include <math.h>
int main() {
int start, end, i;
double total = 0.0;
scanf("%d %d", &start, &end);
for (i = start; i <= end; i++) {
total += pow(i, 2) + 1.0 / i;
}
printf("sum = %lf", total);
return 0;
}
Problem 2-4: Sum of Alternating Sequence
Calculates the sum of an alternating sequence: $1 - 2/3 + 3/5 - 4/7 + ...$ up to N terms.
#include <stdio.h>
int main() {
int n, i;
int sign = 1;
double sum = 0.0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += sign * i / (2.0 * i - 1);
sign = -sign;
}
printf("%.3lf", sum);
return 0;
}
Problem 2-5: Sum of Square Root Sequence
Calculates the sum of square roots of integers from 1 to N.
#include <stdio.h>
#include <math.h>
int main() {
int limit, i;
double sum = 0.0;
scanf("%d", &limit);
for (i = 1; i <= limit; i++) {
sum += sqrt(i);
}
printf("sum = %.2lf", sum);
return 0;
}
Problem 2-6: Sum of Factorial Sequence
Calculates the sum of factorials from $1!$ to $N!$.
#include <stdio.h>
long long computeFactorial(int n);
int main() {
int n, i;
long long total = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
total += computeFactorial(i);
}
printf("%lld", total);
return 0;
}
long long computeFactorial(int n) {
long long res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
}
return res;
}