This experiment involves writing a program to find the divisors of a given number or calculate its factorial. It illustrates basic loop and conditional logic.
The factorial of a non-negative integer n is the product of all positive integers less than or equal to n, denoted by n!. A divisor is a number that can divide another number without leaving a remainder.
#include <stdio.h>
// Function to calculate factorial
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
// Function to find divisors
void findDivisors(int n) {
printf("Divisors of %d are: ", n);
for (int i = 1; i <= n; i++) {
if (n % i == 0)
printf("%d ", i);
}
printf("\n");
}
int main() {
int choice, num;
printf("Enter 1 for Factorial or 2 for Divisors: ");
scanf("%d", &choice);
printf("Enter a number: ");
scanf("%d", &num);
if (choice == 1) {
printf("Factorial of %d is: %d\n", num, factorial(num));
} else {
findDivisors(num);
}
return 0;
}
This experiment focuses on writing a program to find the sum of a geometric series.
A geometric series is a series of terms where each term after the first is found by multiplying the previous term by a fixed, non-zero number called the common ratio.
#include <stdio.h>
double geometricSum(int n, double r) {
if (n == 0) return 1; // First term a = 1
return geometricSum(n - 1, r) + (1 * pow(r, n));
}
int main() {
int n;
double r;
printf("Enter the number of terms (n): ");
scanf("%d", &n);
printf("Enter the common ratio (r): ");
scanf("%lf", &r);
printf("Sum of the geometric series: %.2f\n", geometricSum(n, r));
return 0;
}
This experiment involves writing a recursive program to solve the Tower of Hanoi problem.
The Tower of Hanoi is a mathematical puzzle that involves moving disks between three rods according to certain rules. The challenge is to move the entire stack to another rod.
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", from_rod, to_rod);
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from %c to %c\n", n, from_rod, to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
This experiment involves writing a recursive program to print the first m Fibonacci numbers.
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.
#include <stdio.h>
void printFibonacci(int n1, int n2, int count) {
if (count == 0) return;
printf("%d ", n1);
printFibonacci(n2, n1 + n2, count - 1);
}
int main() {
int m;
printf("Enter number of Fibonacci numbers to print: ");
scanf("%d", &m);
printFibonacci(0, 1, m);
printf("\n");
return 0;
}
This experiment involves writing a menu-driven program to perform various operations on matrices.
This program will include operations like addition, subtraction, finding upper and lower triangular matrices, transpose, and multiplication of matrices.
#include <stdio.h>
#define MAX 10
void addMatrices(int a[MAX][MAX], int b[MAX][MAX], int res[MAX][MAX], int r, int c) {
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
res[i][j] = a[i][j] + b[i][j];
}
// Function to display the matrix
void displayMatrix(int matrix[MAX][MAX], int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", matrix[i][j]);
printf("\n");
}
}
int main() {
int r, c;
int a[MAX][MAX], b[MAX][MAX], res[MAX][MAX];
printf("Enter number of rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter elements of first matrix:\n");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &b[i][j]);
addMatrices(a, b, res, r, c);
printf("Sum of the matrices:\n");
displayMatrix(res, r, c);
return 0;
}
This experiment involves writing a program to calculate the GCD (Greatest Common Divisor) of two numbers.
The GCD of two or more integers is the largest positive integer that divides each of the integers without a remainder. The Euclidean algorithm is a common method for finding the GCD.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is: %d\n", a, b, gcd(a, b));
return 0;
}
This experiment involves writing a program to reverse a given string.
String manipulation is a fundamental concept in programming, and reversing a string demonstrates the use of arrays and loops effectively.
#include <stdio.h>
#include
void reverseString(char str[]) {
int n = strlen(str);
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0; // Remove newline character
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
This experiment involves writing a program to sort an array using the bubble sort algorithm.
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements:\n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
This experiment involves writing a program to find all prime numbers up to a given number n.
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. The prime numbers are: 2, 3, 5, 7, 11, and so on.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++)
if (num % i == 0)
return 0;
return 1;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Prime numbers up to %d are: ", n);
for (int i = 2; i <= n; i++) {
if (isPrime(i))
printf("%d ", i);
}
printf("\n");
return 0;
}
This experiment involves writing a simple calculator program that uses a switch statement to perform basic arithmetic operations.
Switch-case is a control statement that allows variable values to be tested for equality against a list of values. It is often used as a cleaner alternative to if-else statements when dealing with multiple conditions.
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
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("Division by zero error!\n");
return 1;
}
break;
default:
printf("Invalid operator!\n");
return 1;
}
printf("Result: %.2lf\n", result);
return 0;
}