C Code Examples: Calculator, Leap Year, and More
Posted on Feb 21, 2025 in Communication
C Code Examples
1. Calculator
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
// Input
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
// Switch case to perform calculation based on the operator
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero is not allowed.\n");
return 1; // Exit the program with an error code
}
break;
default:
printf("Error: Invalid operator.\n");
return 1; // Exit the program with an error code
}
// Output the result
printf("Result: %.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);
return 0; // Exit the program successfully
}
2. Leap Year Checker
#include <stdio.h>
int main() {
int year;
// Input
printf("Enter a year: ");
scanf("%d", &year);
// Using ternary operator to check for leap year
(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
? printf("%d is a leap year.\n", year)
: printf("%d is not a leap year.\n", year);
return 0; // Exit the program successfully
}
3. Second Largest Element in Array
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find the second largest element
int largest, secondLargest;
if (n < 2) {
printf("Array should have at least two elements.\n");
return 1; // Exit the program with an error code
}
// Initialize the largest and secondLargest with the first two elements
if (arr[0] > arr[1]) {
largest = arr[0];
secondLargest = arr[1];
} else {
largest = arr[1];
secondLargest = arr[0];
}
for (int i = 2; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
printf("The second largest element is: %d\n", secondLargest);
return 0; // Exit the program successfully
}
4. Matrix Operations
#include <stdio.h>
int main() {
int rows, cols;
// Input: Size of the matrices
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Input: Matrix A
int matrixA[rows][cols];
printf("Enter elements for Matrix A:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
// Input: Matrix B
int matrixB[rows][cols];
printf("Enter elements for Matrix B:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrixB[i][j]);
}
}
// Subtract matrices and calculate transpose of the resultant matrix
int resultMatrix[rows][cols];
int transposeMatrix[cols][rows];
// Subtract matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
resultMatrix[i][j] = matrixA[i][j] - matrixB[i][j];
}
}
// Calculate transpose of the resultant matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposeMatrix[j][i] = resultMatrix[i][j];
}
}
// Display the resultant matrix
printf("\nResultant Matrix (A - B):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", resultMatrix[i][j]);
}
printf("\n");
}
// Display the transpose of the resultant matrix
printf("\nTranspose of the Resultant Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d\t", transposeMatrix[i][j]);
}
printf("\n");
}
return 0; // Exit the program successfully
}
5. Bubble Sort
#include <stdio.h>
// Function to perform bubble sort on an array
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n;
// Input: Size of the array
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
// Input: Array elements
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform bubble sort
bubbleSort(arr, n);
// Output: Display the sorted array
printf("Sorted array using Bubble Sort:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0; // Exit the program successfully
}