/* Program 1 */
#include <stdio.h>
int main() {
int num, sum = 0, product = 1, digit;
printf("Enter an integer: ");
scanf("%d", &num);
while (num > 0) {
digit = num % 10;
sum += digit;
product *= digit;
num /= 10;
}
printf("Sum: %d\nProduct: %d\n", sum, product);
return 0;
}
/* Program 2 */
#include <stdio.h>
int main() {
int num, reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
reverse = reverse * 10 + (num % 10);
num /= 10;
}
printf("Reversed number: %d\n", reverse);
return 0;
}
/* Program 3 */
#include <stdio.h>
int main() {
int n;
float sum = 0.0;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += 1.0 / i;
}
printf("Sum: %.2f\n", sum);
return 0;
}
/* Program 4 */
#include <stdio.h>
int main() {
int n;
int sum = 0;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if(i%2==0){
sum -= i;
}else{
sum += i;
}
}
printf("Sum: %d\n", sum);
return 0;
}
/* Program 5 */
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1])
return 0;
}
return 1;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str))
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
/* Program 6 */
#include <stdio.h>
int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (isPrime(n))
printf("Prime\n");
else
printf("Not Prime\n");
printf("Prime numbers less than 100: ");
for (int i = 2; i < 100; i++) {
if (isPrime(i))
printf("%d ", i);
}
printf("\n");
return 0;
}
/* Program 7 */
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factors: ");
for (int i = 1; i <= n; i++) {
if (n % i == 0)
printf("%d ", i);
}
printf("\n");
return 0;
}
/* Program 8 */
#include <stdio.h>
#define SWAP(a, b) \
do { \
int temp = a; \
a = b; \
b = temp; \
} while (0)
int main() {
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
SWAP(x, y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
/* Program 9 */
#include <stdio.h>
int main() {
int rows;
printf("Enter number of lines: ");
scanf("%d", &rows);
for (int i = 0; i < rows; i++) {
// inner loop 1 to print white spaces
for (int j = 0; j < 2 * (rows - i) - 1; j++) {
printf(" ");
}
// inner loop 2 to print star * character
for (int k = 0; k < 2 * i + 1; k++) {
printf("* ");
}
printf("\n");
}
return 0;
}
/* Program 10 */
#include <stdio.h>
int main() {
int n;
printf("Enter array size: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int choice;
do {
printf("Menu:\n");
printf("1. Even elements\n2. Odd elements\n3. Sum and Average\n4. Max
and Min\n5. Remove Duplicates\n6. Reverse Array\n7. Quit\n");
scanf("%d", &choice);
if (choice == 1) {
printf("Even elements: ");
for (int i = 0; i < n; i++) if (arr[i] % 2 == 0) printf("%d ",
arr[i]);
printf("\n");
}
else if (choice == 2) {
printf("Odd elements: ");
for (int i = 0; i < n; i++) if (arr[i] % 2 != 0) printf("%d ",
arr[i]);
printf("\n");
}
else if (choice == 3) {
int sum = 0;
for (int i = 0; i < n; i++) sum += arr[i];
printf("Sum: %d Average: %.2f\n", sum, sum / (float)n);
}
else if (choice == 4) {
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Max: %d Min: %d\n", max, min);
}
else if (choice == 5) {
printf("Array after removing duplicates: ");
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < i; j++) if (arr[i] == arr[j]) found = 1;
if (!found) printf("%d ", arr[i]);
}
printf("\n");
}
else if (choice == 6) {
printf("Reversed array: ");
for (int i = n - 1; i >= 0; i--) printf("%d ", arr[i]);
printf("\n");
}
} while (choice != 7);
return 0;
}
/* Program 11 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
int count[26] = {0}; // Array to store occurrences of each alphabet
char *text;
if (argc < 2) {
printf("Please enter text as a command line argument.\n");
return 1;
}
text = argv[1]; // Text entered as command line argument
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) {
count[tolower(text[i]) - 'a']++; // Increment the count for each
alphabet
}
}
printf("Alphabet occurrences:\n");
for (int i = 0; i < 26; i++) {
if (count[i] > 0) {
printf("%c: %d\n", i + 'a', count[i]);
}
}
return 0;
}
/* Program 12 */
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
/* Program 13 */
#include <stdio.h>
void modifyValues(int *x, int *y) {
*x += 10; // Add 10 to the first variable
*y *= 2; // Multiply the second variable by 2
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before modification: a = %d, b = %d\n", a, b);
modifyValues(&a, &b);
printf("After modification: a = %d, b = %d\n", a, b);
return 0;
}
/* Program 14 */
#include <stdio.h>
#define PI 3.14159
void calculateCircle(float radius, float *area, float *circumference) {
*area = PI * radius * radius;
*circumference = 2 * PI * radius;
}
int main() {
float radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
calculateCircle(radius, &area, &circumference);
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}
/* Program 15 */
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, *arr, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int)); // Allocate memory dynamically
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i]; // Add each element to the sum
}
printf("Sum of the elements: %d\n", sum);
free(arr); // Free the allocated memory
return 0;
}
/* Program 16 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void showAddresses(char *str) {
for (int i = 0; str[i] != '\0'; i++) {
printf("Character '%c' is at address: %p\n", str[i], &str[i]);
}
}
void concatenateWithoutStrcat(char *str1, char *str2, char *result) {
int i = 0, j = 0;
while (str1[i] != '\0') {
result[i] = str1[i];
i++;
}
while (str2[j] != '\0') {
result[i] = str2[j];
i++;
j++;
}
result[i] = '\0';
}
void concatenateWithStrcat(char *str1, char *str2) {
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
}
void compareStrings(char *str1, char *str2) {
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("First string is greater.\n");
} else {
printf("Second string is greater.\n");
}
}
int calculateLength(char *str) {
int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
void convertToUpper(char *str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("String in uppercase: %s\n", str);
}
void convertToLower(char *str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = tolower(str[i]);
}
printf("String in lowercase: %s\n", str);
}
void countVowels(char *str) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
printf("Number of vowels: %d\n", count);
}
void reverseString(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
printf("Reversed string: %s\n", str);
}
int main() {
char str1[100], str2[100], result[200];
int choice;
do {
printf("\nMenu:\n");
printf("1. Show address of each character in string\n");
printf("2. Concatenate two strings without using strcat\n");
printf("3. Concatenate two strings using strcat\n");
printf("4. Compare two strings\n");
printf("5. Calculate the length of the string\n");
printf("6. Convert all lowercase characters to uppercase\n");
printf("7. Convert all uppercase characters to lowercase\n");
printf("8. Calculate the number of vowels\n");
printf("9. Reverse the string\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // To consume newline left by scanf
switch (choice) {
case 1:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0'; // Remove trailing newline
showAddresses(str1);
break;
case 2:
printf("Enter first string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string: ");
fgets(str2, 100, stdin);
str2[strcspn(str2, "\n")] = '\0';
concatenateWithoutStrcat(str1, str2, result);
printf("Concatenated string: %s\n", result);
break;
case 3:
printf("Enter first string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string: ");
fgets(str2, 100, stdin);
str2[strcspn(str2, "\n")] = '\0';
concatenateWithStrcat(str1, str2);
break;
case 4:
printf("Enter first string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter second string: ");
fgets(str2, 100, stdin);
str2[strcspn(str2, "\n")] = '\0';
compareStrings(str1, str2);
break;
case 5:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Length of the string: %d\n", calculateLength(str1));
break;
case 6:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
convertToUpper(str1);
break;
case 7:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
convertToLower(str1);
break;
case 8:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
countVowels(str1);
break;
case 9:
printf("Enter a string: ");
fgets(str1, 100, stdin);
str1[strcspn(str1, "\n")] = '\0';
reverseString(str1);
break;
case 10:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 10);
return 0;
}
/* Program 17 */
#include <stdio.h>
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int result[]) {
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
result[k++] = arr1[i++];
} else {
result[k++] = arr2[j++];
}
}
while (i < n1) {
result[k++] = arr1[i++];
}
while (j < n2) {
result[k++] = arr2[j++];
}
}
int main() {
int arr1[100], arr2[100], result[200];
int n1, n2;
printf("Enter the size of first array: ");
scanf("%d", &n1);
printf("Enter elements of first sorted array:\n");
for (int i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the size of second array: ");
scanf("%d", &n2);
printf("Enter elements of second sorted array:\n");
for (int i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}
mergeArrays(arr1, n1, arr2, n2, result);
printf("Merged sorted array:\n");
for (int i = 0; i < n1 + n2; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
/* Program 18 */
#include <stdio.h>
int fibonacciRecursive(int n) {
if (n <= 1) {
return n;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series:\n");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacciRecursive(i));
}
printf("\n");
return 0;
}
// ========================================
#include <stdio.h>
void fibonacciIterative(int n) {
int a = 0, b = 1, next;
for (int i = 0; i < n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series:\n");
fibonacciIterative(n);
return 0;
}
/* Program 19 */
#include <stdio.h>
int factorialRecursive(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorialRecursive(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorialRecursive(n));
return 0;
}
// ======================================================================
#include <stdio.h>
int factorialIterative(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorialIterative(n));
return 0;
}
/* Program 20 */
#include <stdio.h>
int gcdRecursive(int a, int b) {
if (b == 0) {
return a;
}
return gcdRecursive(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, gcdRecursive(a, b));
return 0;
}
// ============================================
#include <stdio.h>
int gcdIterative(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
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, gcdIterative(a, b));
return 0;
}
/* Program 21 */
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct {
int mat[MAX][MAX];
int rows;
int cols;
} Matrix;
void inputMatrix(Matrix *m) {
printf("Enter the elements of the matrix (%d x %d):\n", m->rows, m->cols);
for (int i = 0; i < m->rows; i++) {
for (int j = 0; j < m->cols; j++) {
scanf("%d", &m->mat[i][j]);
}
}
}
void displayMatrix(Matrix *m) {
for (int i = 0; i < m->rows; i++) {
for (int j = 0; j < m->cols; j++) {
printf("%d ", m->mat[i][j]);
}
printf("\n");
}
}
Matrix addMatrices(Matrix *m1, Matrix *m2) {
Matrix result;
result.rows = m1->rows;
result.cols = m1->cols;
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
result.mat[i][j] = m1->mat[i][j] + m2->mat[i][j];
}
}
return result;
}
Matrix subtractMatrices(Matrix *m1, Matrix *m2) {
Matrix result;
result.rows = m1->rows;
result.cols = m1->cols;
for (int i = 0; i < result.rows; i++) {
for (int j = 0; j < result.cols; j++) {
result.mat[i][j] = m1->mat[i][j] - m2->mat[i][j];
}
}
return result;
}
Matrix transposeMatrix(Matrix *m) {
Matrix result;
result.rows = m->cols;
result.cols = m->rows;
for (int i = 0; i < m->rows; i++) {
for (int j = 0; j < m->cols; j++) {
result.mat[j][i] = m->mat[i][j];
}
}
return result;
}
int main() {
Matrix m1, m2, result;
int choice;
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &m1.rows, &m1.cols);
m2.rows = m1.rows;
m2.cols = m1.cols;
printf("Matrix 1:\n");
inputMatrix(&m1);
printf("Matrix 2:\n");
inputMatrix(&m2);
do {
printf("\nMenu:\n");
printf("1. Sum of Matrices\n");
printf("2. Difference of Matrices\n");
printf("3. Transpose of Matrix 1\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
result = addMatrices(&m1, &m2);
printf("Sum of Matrices:\n");
displayMatrix(&result);
break;
case 2:
result = subtractMatrices(&m1, &m2);
printf("Difference of Matrices:\n");
displayMatrix(&result);
break;
case 3:
result = transposeMatrix(&m1);
printf("Transpose of Matrix 1:\n");
displayMatrix(&result);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
/* Program 22 */
#include <stdio.h>
#include <stdlib.h>
struct Student {
int rollNo;
char name[50];
char className[20];
int year;
float totalMarks;
};
int main() {
struct Student students[10];
FILE *file;
file = fopen("students.dat", "wb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter details for 10 students:\n");
for (int i = 0; i < 10; i++) {
printf("Student %d:\n", i + 1);
printf("Enter Roll No: ");
scanf("%d", &students[i].rollNo);
getchar(); // Consume newline
printf("Enter Name: ");
fgets(students[i].name, 50, stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0'; // Remove
newline
printf("Enter Class: ");
fgets(students[i].className, 20, stdin);
students[i].className[strcspn(students[i].className, "\n")] = '\0'; //
Remove newline
printf("Enter Year: ");
scanf("%d", &students[i].year);
printf("Enter Total Marks: ");
scanf("%f", &students[i].totalMarks);
}
fwrite(students, sizeof(struct Student), 10, file);
fclose(file);
printf("Student details have been saved to 'students.dat'.\n");
return 0;
}
/* Program 23 */
#include <stdio.h>
#include <stdlib.h>
struct Student {
int rollNo;
char name[50];
char className[20];
int year;
float totalMarks;
};
int main() {
struct Student students[10];
FILE *file;
file = fopen("students.dat", "rb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fread(students, sizeof(struct Student), 10, file);
fclose(file);
printf("Roll No. Name Class Year Total Marks\n");
printf("-----------------------------------------------------------\n");
for (int i = 0; i < 10; i++) {
printf("%-9d %-20s %-8s %-6d %.2f\n",
students[i].rollNo,
students[i].name,
students[i].className,
students[i].year,
students[i].totalMarks);
}
return 0;
}
/* Program 24 */
#include <stdio.h>
#include <ctype.h>
void copyWithoutWhitespaces(const char *src, const char *dest) {
FILE *sourceFile = fopen(src, "r");
FILE *destFile = fopen(dest, "w");
if (sourceFile == NULL || destFile == NULL) {
printf("Error opening files!\n");
return;
}
char ch;
while ((ch = fgetc(sourceFile)) != EOF) {
if (!isspace(ch)) {
fputc(ch, destFile);
}
}
fclose(sourceFile);
fclose(destFile);
printf("Contents copied to '%s' without whitespaces.\n", dest);
}
int main() {
char source[100], destination[100];
printf("Enter the source file name: ");
scanf("%s", source);
printf("Enter the destination file name: ");
scanf("%s", destination);
copyWithoutWhitespaces(source, destination);
return 0;
}
/* Program 25 */
#include <stdio.h>
void reverseArray(int *arr, int size) {
int temp, i, j;
for (i = 0, j = size - 1; i < j; i++, j--) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main() {
int arr[10];
printf("Enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, 10);
printf("Reversed array:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
/* Program 26 */
#include <stdio.h>
#include <stdlib.h>
void sortArray(int *arr, int size, int ascending) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if ((ascending && arr[j] > arr[j + 1]) || (!ascending && arr[j] <
arr[j + 1])) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int *arr = (int *)malloc(10 * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
printf("Array in ascending order:\n");
sortArray(arr, 10, 1);
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in descending order:\n");
sortArray(arr, 10, 0);
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}