Practical 2 :-
#include <stdio.h>
#include <math.h>
int main() {
float side, area, perimeter, diagonal;
printf("Enter the side length of the square: ");
scanf("%f", &side);
area = side * side;
perimeter = 4 * side;
diagonal = sqrt(2) * side;
printf("Area of the square: %.2f\n", area);
printf("Perimeter of the square: %.2f\n", perimeter);
printf("Diagonal of the square: %.2f\n", diagonal);
return 0;
}
/*
#include <stdio.h>
#include <math.h>
int main() {
float length, width, area, perimeter, diagonal;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
perimeter = 2 * (length + width);
diagonal = sqrt(length * length + width * width);
printf("Area of the rectangle: %.2f\n", area);
printf("Perimeter of the rectangle: %.2f\n", perimeter);
printf("Diagonal of the rectangle: %.2f\n", diagonal);
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
float radius, height, surfaceArea, volume;
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
surfaceArea = 2 * M_PI * radius * (radius + height);
volume = M_PI * radius * radius * height;
printf("Total Surface Area of the cylinder: %.2f\n", surfaceArea);
printf("Volume of the cylinder: %.2f\n", volume);
return 0;
}
*/
Practical 3 :-
#include <stdio.h>
int main() {
float u, a, time_interval;
char choice;
do {
printf("Enter initial velocity (u in m/s): ");
scanf("%f", &u);
printf("Enter acceleration (a in m/s^2): ");
scanf("%f", &a);
printf("Enter time interval (in seconds): ");
scanf("%f", &time_interval);
float distance = u * time_interval + 0.5 * a * time_interval *
time_interval;
printf("Distance traveled: %.2f meters\n", distance);
printf("Do you want to calculate again? (y/n): ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;
}
/*
#include <stdio.h>
int main() {
int operand1, operand2;
char operator;
printf("Enter first operand: ");
scanf("%d", &operand1);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter second operand: ");
scanf("%d", &operand2);
switch (operator) {
case '+':
printf("Result: %d\n", operand1 + operand2);
break;
case '-':
printf("Result: %d\n", operand1 - operand2);
break;
case '*':
printf("Result: %d\n", operand1 * operand2);
break;
case '/':
if (operand2 != 0) {
printf("Result: %.2f\n", (float)operand1 / operand2);
} else {
printf("Error: Division by zero!\n");
}
break;
default:
printf("Invalid operator!\n");
}
return 0;
}
*/
Practical 4 :-
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a positive integer: ");
scanf("%d", &num);
while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of individual digits: %d\n", sum);
return 0;
}
/*
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Sequence: ");
for (i = 0; i < n; i++) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
return 0;
}
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) {
return 0; // Not a prime number
}
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int main() {
int n;
printf("Enter a positive integer (n): ");
scanf("%d", &n);
printf("Prime numbers between 1 and %d: ", n);
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
printf("%d, ", i);
}
}
printf("\n");
return 0;
}
*/
Practical 5:-
#include <stdio.h>
#include <math.h>
double calculatePower(double x, int n) {
return pow(x, n);
}
double calculateFactorial(int n) {
double factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
int main() {
double x, sum = 1.0;
int n;
printf("Enter the value of x: ");
scanf("%lf", &x);
printf("Enter the number of terms (n): ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
double term = calculatePower(-1, i) * calculatePower(x, 2 * i) /
calculateFactorial(2 * i);
sum += term;
}
printf("Sum = %.4f\n", sum);
return 0;
}
/*
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and c of the quadratic equation (ax^2 +
bx + c): ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.4f\n", root1);
printf("Root 2 = %.4f\n", root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root = %.4f\n", root1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.4f + %.4fi\n", realPart, imaginaryPart);
printf("Root 2 = %.4f - %.4fi\n", realPart, imaginaryPart);
}
return 0;
}
*/
Practical 6 :-
#include <stdio.h>
// Recursive function to calculate factorial
int factorialRecursive(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorialRecursive(n - 1);
}
}
// Non-recursive function to calculate factorial
int factorialNonRecursive(int n) {
int result = 1;
while (n > 1) {
result *= n;
n--;
}
return result;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Using Recursive Function
printf("Factorial using recursion: %d\n", factorialRecursive(num));
// Using Non-Recursive Function
printf("Factorial without recursion: %d\n", factorialNonRecursive(num));
return 0;
}
/*
#include <stdio.h>
// Recursive function to calculate GCD
int gcdRecursive(int a, int b) {
if (b == 0) {
return a;
} else {
return gcdRecursive(b, a % b);
}
}
// Non-recursive function to calculate GCD
int gcdNonRecursive(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Using Recursive Function
printf("GCD using recursion: %d\n", gcdRecursive(num1, num2));
// Using Non-Recursive Function
printf("GCD without recursion: %d\n", gcdNonRecursive(num1, num2));
return 0;
}
*/
Practical 7 :-
#include <stdio.h>
// Function to find the largest integer in a list
int findLargest(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
int size;
printf("Enter the size of the list: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d integers:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Finding the largest integer
int largest = findLargest(arr, size);
printf("The largest integer is: %d\n", largest);
return 0;
}
/*
#include <stdio.h>
#define MAX_SIZE 10
// Function to add two matrices
void addMatrices(int mat1[][MAX_SIZE], int mat2[][MAX_SIZE], int result[]
[MAX_SIZE], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
int main() {
int rows, cols;
printf("Enter the number of rows and columns for matrices: ");
scanf("%d %d", &rows, &cols);
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE]
[MAX_SIZE];
printf("Enter elements for the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements for the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat2[i][j]);
}
}
// Adding matrices
addMatrices(mat1, mat2, result, rows, cols);
// Displaying the result matrix
printf("Resultant Matrix (Sum):\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#define MAX_SIZE 10
// Function to multiply two matrices
void multiplyMatrices(int mat1[][MAX_SIZE], int mat2[][MAX_SIZE], int result[]
[MAX_SIZE], int rows1, int cols1, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int rows1, cols1, rows2, cols2;
printf("Enter the dimensions of the first matrix (rows columns): ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the dimensions of the second matrix (rows columns): ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Error: Matrix multiplication not possible due to incompatible
dimensions.\n");
return 1;
}
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE]
[MAX_SIZE];
printf("Enter elements for the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements for the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &mat2[i][j]);
}
}
// Multiplying matrices
multiplyMatrices(mat1, mat2, result, rows1, cols1, cols2);
// Displaying the result matrix
printf("Resultant Matrix (Product):\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
*/
Practical 8 :-
#include <stdio.h>
#include <string.h>
// Function to insert a sub-string into a main string from a given position
void insertSubstring(char mainStr[], char subStr[], int position) {
int mainLen = strlen(mainStr);
int subLen = strlen(subStr);
// Check if the position is valid
if (position < 0 || position > mainLen) {
printf("Invalid position for insertion.\n");
return;
}
// Shift characters to make space for the sub-string
for (int i = mainLen; i >= position; i--) {
mainStr[i + subLen] = mainStr[i];
}
// Insert the sub-string
for (int i = 0; i < subLen; i++) {
mainStr[position + i] = subStr[i];
}
}
int main() {
char mainStr[100], subStr[50];
int position;
printf("Enter the main string: ");
gets(mainStr);
printf("Enter the sub-string to insert: ");
gets(subStr);
printf("Enter the position to insert sub-string: ");
scanf("%d", &position);
// Inserting sub-string
insertSubstring(mainStr, subStr, position);
// Displaying the modified string
printf("Modified String: %s\n", mainStr);
return 0;
}
/*
#include <stdio.h>
#include <string.h>
// Function to delete n characters from a given position in a string
void deleteCharacters(char str[], int position, int n) {
int len = strlen(str);
// Check if the position and number of characters to delete are valid
if (position < 0 || position >= len || n < 0 || position + n > len) {
printf("Invalid position or number of characters to delete.\n");
return;
}
// Shift characters to delete the specified characters
for (int i = position; i < len - n; i++) {
str[i] = str[i + n];
}
str[len - n] = '\0'; // Null-terminate the string
}
int main() {
char str[100];
int position, n;
printf("Enter the string: ");
gets(str);
printf("Enter the position to delete characters from: ");
scanf("%d", &position);
printf("Enter the number of characters to delete: ");
scanf("%d", &n);
// Deleting characters
deleteCharacters(str, position, n);
// Displaying the modified string
printf("Modified String: %s\n", str);
return 0;
}
#include <stdio.h>
#include <string.h>
// Function to check if a string is a palindrome
int isPalindrome(char str[]) {
int len = strlen(str);
int i, j;
for (i = 0, j = len - 1; i < j; i++, j--) {
if (str[i] != str[j]) {
return 0; // Not a palindrome
}
}
return 1; // Palindrome
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
// Checking if the string is a palindrome
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
*/
Practical 9 :-
#include <stdio.h>
#include <string.h>
// Function to find the position/index of substring T in string S
int findSubstring(char S[], char T[]) {
int lenS = strlen(S);
int lenT = strlen(T);
for (int i = 0; i <= lenS - lenT; i++) {
int j;
for (j = 0; j < lenT; j++) {
if (S[i + j] != T[j]) {
break;
}
}
if (j == lenT) {
return i; // Found the substring at index i
}
}
return -1; // Substring not found
}
int main() {
char S[100], T[50];
printf("Enter the main string (S): ");
gets(S);
printf("Enter the substring to find (T): ");
gets(T);
// Finding and displaying the position/index of substring
int position = findSubstring(S, T);
if (position != -1) {
printf("Substring '%s' found at position/index: %d\n", T, position);
} else {
printf("Substring '%s' not found in the main string.\n", T);
}
return 0;
}
/*
#include <stdio.h>
// Function to count lines, words, and characters in a given text
void countMetrics(char text[]) {
int lines = 0, words = 0, characters = 0;
int i = 0;
while (text[i] != '\0') {
// Counting characters
characters++;
// Counting words
if (text[i] == ' ' || text[i] == '\n' || text[i] == '\t') {
words++;
}
// Counting lines
if (text[i] == '\n') {
lines++;
}
i++;
}
// Adjusting word count (last word might not be followed by space)
if (characters > 0) {
words++;
}
// Displaying the counts
printf("Lines: %d\n", lines);
printf("Words: %d\n", words);
printf("Characters: %d\n", characters);
}
int main() {
char text[1000];
printf("Enter the text:\n");
gets(text);
// Counting lines, words, and characters
countMetrics(text);
return 0;
}
*/
Practical 10 :-
#include <stdio.h>
// Function to generate Pascal's Triangle
void generatePascalsTriangle(int rows) {
int coefficients[rows][rows];
// Initialize coefficients with zeros
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
coefficients[i][j] = 0;
}
}
// Calculate coefficients using dynamic programming
for (int i = 0; i < rows; i++) {
coefficients[i][0] = 1;
for (int j = 1; j <= i; j++) {
coefficients[i][j] = coefficients[i - 1][j - 1] + coefficients[i -
1][j];
}
}
// Display Pascal's Triangle
for (int i = 0; i < rows; i++) {
// Add spaces for formatting
for (int k = 0; k < rows - i; k++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("%6d", coefficients[i][j]);
}
printf("\n");
}
}
int main() {
int rows;
printf("Enter the number of rows for Pascal's Triangle: ");
scanf("%d", &rows);
// Generating Pascal's Triangle and displaying
generatePascalsTriangle(rows);
return 0;
}
/*
#include <stdio.h>
// Function to construct a pyramid of numbers
void constructPyramid(int height) {
for (int i = 1; i <= height; i++) {
// Add spaces for formatting
for (int k = 1; k <= height - i; k++) {
printf(" ");
}
// Print increasing numbers
for (int j = 1; j <= i; j++) {
printf("%2d", j);
}
// Print decreasing numbers
for (int j = i - 1; j >= 1; j--) {
printf("%2d", j);
}
printf("\n");
}
}
int main() {
int height;
printf("Enter the height of the pyramid: ");
scanf("%d", &height);
// Constructing the pyramid and displaying
constructPyramid(height);
return 0;
}
*/
Practical 11 :-
#include <stdio.h>
#include <math.h>
int main() {
double x, sum;
int n;
while (1) {
// Read input values
printf("Enter x and n (separated by space): ");
if (scanf("%lf %d", &x, &n) != 2) {
// Handle invalid input
printf("Invalid input. Please enter valid numbers.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
if (n < 0) {
// Error: Negative exponent
printf("Error: n should not be less than 0.\n");
continue;
}
if (x == 1 && n == 0) {
// Error: Result is undefined for x=1 and n=0
printf("Error: The result is undefined for x=1 and n=0.\n");
continue;
}
// Compute the sum of the geometric progression
sum = 0;
for (int i = 0; i <= n; i++) {
sum += pow(x, i);
}
// Print the result
printf("x = %.2lf, n = %d, Sum = %.2lf\n", x, n, sum);
// Break out of the loop since the calculation is done
break;
}
return 0;
}
Practical 12 :-
#include <stdio.h>
void findTwosComplement(char binaryNum[]) {
int i;
// Finding the rightmost 1 in the binaryNum
for (i = strlen(binaryNum) - 1; i >= 0; i--) {
if (binaryNum[i] == '1') {
break;
}
}
// Complementing all bits after the rightmost 1
for (int j = i - 1; j >= 0; j--) {
binaryNum[j] = (binaryNum[j] == '0') ? '1' : '0';
}
}
int main() {
char binaryNum[100];
// Input binary number
printf("Enter a binary number: ");
scanf("%s", binaryNum);
findTwosComplement(binaryNum);
// Displaying the 2's complement
printf("2's complement: %s\n", binaryNum);
return 0;
}
/*
#include <stdio.h>
int romanToDecimal(char romanNum[]) {
int decimalNum = 0;
int i;
// Iterate through the Roman numeral from left to right
for (i = 0; romanNum[i] != '\0'; i++) {
// Check the value of the current Roman numeral character
switch (romanNum[i]) {
case 'I':
if (romanNum[i + 1] == 'V' || romanNum[i + 1] == 'X') {
decimalNum -= 1;
} else {
decimalNum += 1;
}
break;
case 'V':
decimalNum += 5;
break;
case 'X':
if (romanNum[i + 1] == 'L' || romanNum[i + 1] == 'C') {
decimalNum -= 10;
} else {
decimalNum += 10;
}
break;
case 'L':
decimalNum += 50;
break;
case 'C':
if (romanNum[i + 1] == 'D' || romanNum[i + 1] == 'M') {
decimalNum -= 100;
} else {
decimalNum += 100;
}
break;
case 'D':
decimalNum += 500;
break;
case 'M':
decimalNum += 1000;
break;
default:
printf("Invalid Roman numeral.\n");
return -1;
}
}
return decimalNum;
}
int main() {
char romanNum[100];
// Input Roman numeral
printf("Enter a Roman numeral: ");
scanf("%s", romanNum);
int result = romanToDecimal(romanNum);
if (result != -1) {
// Displaying the decimal equivalent
printf("Decimal equivalent: %d\n", result);
}
return 0;
}
*/
Practical 13 :-
#include <stdio.h>
// Function to find the 2's complement of a binary number
void findTwosComplement(char binary[]) {
int i, firstOneIndex = -1;
// Find the index of the first appearance of '1' from the right
for (i = 0; binary[i] != '\0'; i++) {
if (binary[i] == '1') {
firstOneIndex = i;
break;
}
}
// If '1' is found, complement all bits after the first '1'
if (firstOneIndex != -1) {
for (i = firstOneIndex + 1; binary[i] != '\0'; i++) {
binary[i] = (binary[i] == '0') ? '1' : '0';
}
}
}
int main() {
char binary[100];
printf("Enter a binary number: ");
scanf("%s", binary);
// Finding and displaying the 2's complement
findTwosComplement(binary);
printf("2's Complement: %s\n", binary);
return 0;
}
/*
#include <stdio.h>
// Function to convert a Roman numeral to its decimal equivalent
int romanToDecimal(char roman[]) {
int decimal = 0;
for (int i = 0; roman[i] != '\0'; i++) {
// Convert Roman numeral characters to decimal values
switch (roman[i]) {
case 'I':
if (roman[i + 1] == 'V' || roman[i + 1] == 'X')
decimal -= 1;
else
decimal += 1;
break;
case 'V':
decimal += 5;
break;
case 'X':
if (roman[i + 1] == 'L' || roman[i + 1] == 'C')
decimal -= 10;
else
decimal += 10;
break;
case 'L':
decimal += 50;
break;
case 'C':
if (roman[i + 1] == 'D' || roman[i + 1] == 'M')
decimal -= 100;
else
decimal += 100;
break;
case 'D':
decimal += 500;
break;
case 'M':
decimal += 1000;
break;
default:
printf("Invalid Roman numeral character: %c\n", roman[i]);
return -1; // Error
}
}
return decimal;
}
int main() {
char roman[20];
printf("Enter a Roman numeral: ");
scanf("%s", roman);
// Converting and displaying the decimal equivalent
int decimal = romanToDecimal(roman);
if (decimal != -1) {
printf("Decimal Equivalent: %d\n", decimal);
}
return 0;
}
*/
Practical 14 :-
#include <stdio.h>
#include <string.h>
int main() {
char input[1000];
printf("Enter multiline text (Ctrl+D to end on Unix/Linux or Ctrl+Z on
Windows):\n");
// Read multiline input until EOF (Ctrl+D/Ctrl+Z)
while (fgets(input, sizeof(input), stdin) != NULL) {
// Remove newline character if present
int len = strlen(input);
if (len > 0 && input[len - 1] == '\n') {
input[len - 1] = '\0';
}
// Print the length of each line
printf("Length of \"%s\": %d\n", input, len);
}
return 0;
}
/*
#include <stdio.h>
#include <string.h>
// Function to reverse a word in-place
void reverseWord(char word[]) {
int length = strlen(word);
for (int i = 0; i < length / 2; i++) {
char temp = word[i];
word[i] = word[length - 1 - i];
word[length - 1 - i] = temp;
}
}
int main() {
char input[1000];
printf("Enter a string: ");
gets(input);
int i = 0;
while (input[i] != '\0') {
// Find the start of a word
while (input[i] == ' ' && input[i] != '\0') {
i++;
}
// Find the end of the word
int start = i;
while (input[i] != ' ' && input[i] != '\0') {
i++;
}
// Reverse the word in-place
reverseWord(input + start);
// Print the reversed word
for (int j = start; j < i; j++) {
printf("%c", input[j]);
}
// Print space if there are more words
if (input[i] != '\0') {
printf(" ");
}
}
printf("\n");
return 0;
}
*/