NAME – KRISH KEVADIYA
Subject- joy of programming
Enrolment – 24SE02ME008
Subject Code – SECE1120
Q.1 Write a program for checking leap year.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Q.2 Write a program for factorial of a number.
➔
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Q.3 Write a program for checking number is palindrome or not.
➔
#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
}
return 0;
}
Q.4 Write a program for Fibonacci series.
➔
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Q.5 Write a program for printing multiplication table of 5 using for loop.
➔
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
printf("5 x %d = %d\n", i, 5 * i);
}
return 0;
}
Q.6 Write a program for printing prime numbers between range of 1 to 100
➔
#include <stdio.h>
#include <stdbool.h>
int main() {
for (int num = 2; num <= 100; num++) {
bool isPrime = true;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) printf("%d ", num);
}
return 0;
}
Q.7 Write a program to print an array in reverse order.
➔
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array in reverse order:\n");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Q.8 Write a program to calculate the sum of an array.
➔
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n]; // Declare the array of size n
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
sum += arr[i];
}
printf("The sum of the array elements is: %d\n", sum);
return 0;
}
Q.9 Write a program to calculate average of an array
➔
#include <stdio.h>
int main() {
int n;
float sum = 0.0, average;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
average = sum / n;
printf("The average of the array elements is: %.2f\n",
average);
return 0;
}
Q.10 Write a program to find the largest element of an array.
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The largest element in the array is: %d\n", max);
return 0;
}
Q.11 Write a program to find the second largest element of an array.
➔
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array (must be at least 2): ");
scanf("%d", &n);
if (n < 2) {
printf("Array size must be at least 2.\n");
return 1;
}
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int first = arr[0], second = -1;
for (int i = 1; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] < first) {
second = arr[i];
}
}
if (second == -1) {
printf("There is no distinct second largest
element.\n");
} else {
printf("The second largest element in the array is:
%d\n", second);
}
return 0;
}
Q.12 Write a program to concatenate two arrays.
➔
#include <stdio.h>
int main() {
int n1, n2;
printf("Enter the size of the first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter the elements of the first array:\n");
for (int i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
printf("Enter the size of the second array: ");
scanf("%d", &n2);
int arr2[n2];
printf("Enter the elements of the second array:\n");
for (int i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
int result[n1 + n2];
for (int i = 0; i < n1; i++) {
result[i] = arr1[i];
}
for (int i = 0; i < n2; i++) {
result[n1 + i] = arr2[i];
}
printf("Concatenated array:\n");
for (int i = 0; i < n1 + n2; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
Q.13 Write a program to perform matrix addition and store the result
in third array and display it.
➔
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns of the
matrices: ");
scanf("%d %d", &rows, &cols);
int matrix1[rows][cols], matrix2[rows][cols],
result[rows][cols];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Resultant matrix after addition:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Q.14 Write a program to perform matrix multiplication and store the
result in third array and display it.
➔
#include <stdio.h>
#define MAX 10 // Maximum size for matrices
void multiplyMatrices(int matrix1[MAX][MAX], int
matrix2[MAX][MAX], int result[MAX][MAX], 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 i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void displayMatrix(int matrix[MAX][MAX], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrix1[MAX][MAX], matrix2[MAX][MAX],
result[MAX][MAX];
int rows1, cols1, rows2, cols2;
printf("Enter rows and columns for the first matrix:
");
scanf("%d %d", &rows1, &cols1);
printf("Enter rows and columns for the second
matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrix multiplication is not possible with the
given dimensions.\n");
return 1;
}
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
multiplyMatrices(matrix1, matrix2, result, rows1,
cols1, cols2);
printf("Result of matrix multiplication:\n");
displayMatrix(result, rows1, cols2);
return 0;
}
Q.15 Write a program to find the length of the string using library
functions.
➔
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int length = strlen(str);
if (str[length - 1] == '\n') {
length--; // Decrease length to ignore the newline
character
}
printf("The length of the string is: %d\n", length);
return 0;
}
Q.16 Write a program to find the length of the sting without using
library functions.
➔
#include <stdio.h>
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int length = stringLength(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Q.17 Write a program in C to count the total number of alphabets
digits and special characters in C.
➔
#include <stdio.h>
void countCharacters(char str[], int *alphabets, int *digits, int
*specialChars) {
int i = 0;
*alphabets = *digits = *specialChars = 0;
while (str[i] != '\0') {
if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i]
<= 'z')) {
(*alphabets)++; } else if (str[i] >= '0' && str[i] <=
'9') {
(*digits)++; } else if (str[i] != '\n') {
(*specialChars)++; }
i++;
}
}
int main() {
char str[100];
int alphabets, digits, specialChars;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
countCharacters(str, &alphabets, &digits, &specialChars);
printf("Total Alphabets: %d\n", alphabets);
printf("Total Digits: %d\n", digits);
printf("Total Special Characters: %d\n", specialChars);
return 0;
}
Q.18 Write a program to concatenate two strings without using string
library functions.
➔
#include <stdio.h>
void concatenateStrings(char str1[], char str2[]) {
int i = 0, j = 0;
while (str1[i] != '\0') {
i++;
}
while (str2[j] != '\0') {
str1[i] = str2[j];
I++;
j++;
}
str1[i] = '\0';
}
int main() {
char str1[100], str2[50];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
int len1 = 0;
while (str1[len1] != '\0') len1++;
if (str1[len1 - 1] == '\n') {
str1[len1 - 1] = '\0'; }
int len2 = 0;
while (str2[len2] != '\0') len2++;
if (str2[len2 - 1] == '\n') {
str2[len2 - 1] = '\0';
}
concatenateStrings(str1, str2);
printf("The concatenated string is: %s\n", str1);
return 0;
}
Q.19 Write a program in C to sort a string array in ascending order.
➔
#include <stdio.h>
#include <string.h>
void sortStrings(char arr[][100], int n) {
char temp[100];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}
int main() {
int n;
printf("Enter number of strings: ");
scanf("%d", &n);
char arr[n][100];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
scanf("%s", arr[i]);
}
sortStrings(arr, n);
printf("\nStrings in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Q.20 Write a program to explain the concept of function in C with the
help of an example.
➔
#include <stdio.h>
int add(int a, int b);
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2);
printf("The sum of %d and %d is %d\n", num1, num2,
sum);
return 0;
}
int add(int a, int b) {
return a + b;
}