Implement basic arithmetic operations (+, -, *, /).
Convert temperatures between Celsius and Fahrenheit.
#includeint main() { double temp, converted_temp; char unit; printf("Enter temperature followed by unit (C/F): "); scanf("%lf %c", &temp, &unit); if (unit == 'C' || unit == 'c') { converted_temp = (temp * 9/5) + 32; printf("%.2lf Celsius = %.2lf Fahrenheit\n", temp, converted_temp); } else if (unit == 'F' || unit == 'f') { converted_temp = (temp - 32) * 5/9; printf("%.2lf Fahrenheit = %.2lf Celsius\n", temp, converted_temp); } else { printf("Invalid unit\n"); } return 0; }
Calculate and print the average of student grades.
#includeint main() { int n, i; float grade, sum = 0.0, average; printf("Enter the number of students: "); scanf("%d", &n); for (i = 1; i <= n; ++i) { printf("Enter grade for student %d: ", i); scanf("%f", &grade); sum += grade; } average = sum / n; printf("Average grade = %.2f\n", average); return 0; }
Determine if a number is even or odd.
#includeint main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is even.\n", num); } else { printf("%d is odd.\n", num); } return 0; }
Calculate simple interest given principal, rate, and time.
#includeint main() { float principal, rate, time, interest; printf("Enter principal amount: "); scanf("%f", &principal); printf("Enter rate of interest: "); scanf("%f", &rate); printf("Enter time period in years: "); scanf("%f", &time); interest = (principal * rate * time) / 100; printf("Simple interest = %.2f\n", interest); return 0; }
User has to guess a number chosen by the program.
#include#include #include int main() { int number, guess, attempts = 0; // Initialize random number generator srand(time(0)); number = rand() % 100 + 1; // Random number between 1 and 100 printf("Guess the number between 1 and 100:\n"); do { printf("Enter your guess: "); scanf("%d", &guess); attempts++; if (guess > number) { printf("Lower number please!\n"); } else if (guess < number) { printf("Higher number please!\n"); } else { printf("Congratulations! You guessed the number in %d attempts.\n", attempts); } } while (guess != number); return 0; }
Print all prime numbers up to a given number.
#includeint isPrime(int num) { if (num <= 1) return 0; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) return 0; } return 1; } int main() { int limit; printf("Enter the limit: "); scanf("%d", &limit); printf("Prime numbers up to %d are:\n", limit); for (int i = 2; i <= limit; i++) { if (isPrime(i)) { printf("%d ", i); } } printf("\n"); return 0; }
Print the Fibonacci series up to a certain number.
#includeint main() { int limit, t1 = 0, t2 = 1, nextTerm = 0; printf("Enter the limit: "); scanf("%d", &limit); printf("Fibonacci Series: %d, %d, ", t1, t2); nextTerm = t1 + t2; while (nextTerm <= limit) { printf("%d, ", nextTerm); t1 = t2; t2 = nextTerm; nextTerm = t1 + t2; } printf("\n"); return 0; }
Calculate the factorial of a given number.
#includeint main() { int num, i; unsigned long long factorial = 1; printf("Enter an integer: "); scanf("%d", &num); if (num < 0) { printf("Factorial of a negative number doesn't exist.\n"); } else { for (i = 1; i <= num; ++i) { factorial *= i; } printf("Factorial of %d = %llu\n", num, factorial); } return 0; }
Print the multiplication table of a given number.
#includeint main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Multiplication Table of %d:\n", num); for (int i = 1; i <= 10; ++i) { printf("%d x %d = %d\n", num, i, num * i); } return 0; }
Functions to calculate the area and perimeter of different shapes.
#includefloat areaCircle(float radius) { return 3.14159 * radius * radius; } float perimeterCircle(float radius) { return 2 * 3.14159 * radius; } float areaRectangle(float length, float width) { return length * width; } float perimeterRectangle(float length, float width) { return 2 * (length + width); } int main() { float radius, length, width; printf("Enter radius of the circle: "); scanf("%f", &radius); printf("Area of Circle: %.2f\n", areaCircle(radius)); printf("Perimeter of Circle: %.2f\n", perimeterCircle(radius)); printf("Enter length and width of the rectangle: "); scanf("%f %f", &length, &width); printf("Area of Rectangle: %.2f\n", areaRectangle(length, width)); printf("Perimeter of Rectangle: %.2f\n", perimeterRectangle(length, width)); return 0; }
Check if a string or number is a palindrome.
#include#include #include 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); for (int i = 0; i < strlen(str); i++) { str[i] = tolower(str[i]); } if (isPalindrome(str)) { printf("%s is a palindrome.\n", str); } else { printf("%s is not a palindrome.\n", str); } return 0; }
Find the greatest common divisor and least common multiple.
#includeint gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm(int a, int b) { return (a * b) / gcd(a, b); } int main() { int num1, num2; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2)); printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2)); return 0; }
Functions to add, subtract, and multiply matrices.
#includevoid addMatrices(int rows, int cols, int a[rows][cols], int b[rows][cols], int result[rows][cols]) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = a[i][j] + b[i][j]; } } } void subtractMatrices(int rows, int cols, int a[rows][cols], int b[rows][cols], int result[rows][cols]) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result[i][j] = a[i][j] - b[i][j]; } } } void multiplyMatrices(int r1, int c1, int a[r1][c1], int r2, int c2, int b[r2][c2], int result[r1][c2]) { for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { result[i][j] = 0; for (int k = 0; k < c1; k++) { result[i][j] += a[i][k] * b[k][j]; } } } } void printMatrix(int rows, int cols, int matrix[rows][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 rows = 2, cols = 2; int a[2][2] = { {1, 2}, {3, 4} }; int b[2][2] = { {5, 6}, {7, 8} }; int result[2][2]; printf("Matrix A:\n"); printMatrix(rows, cols, a); printf("Matrix B:\n"); printMatrix(rows, cols, b); addMatrices(rows, cols, a, b, result); printf("Sum of matrices:\n"); printMatrix(rows, cols, result); subtractMatrices(rows, cols, a, b, result); printf("Difference of matrices:\n"); printMatrix(rows, cols, result); multiplyMatrices(rows, cols, a, rows, cols, b, result); printf("Product of matrices:\n"); printMatrix(rows, cols, result); return 0; }
mplement bubble sort, insertion sort, and selection sort.
#includevoid 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; } } } } void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } void selectionSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { int minIdx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIdx]) { minIdx = j; } } int temp = arr[minIdx]; arr[minIdx] = arr[i]; arr[i] = temp; } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr1[] = {64, 25, 12, 22, 11}; int n = sizeof(arr1) / sizeof(arr1[0]); printf("Original array:\n"); printArray(arr1, n); bubbleSort(arr1, n); printf("Sorted array with Bubble Sort:\n"); printArray(arr1, n); int arr2[] = {64, 25, 12, 22, 11}; insertionSort(arr2, n); printf("Sorted array with Insertion Sort:\n"); printArray(arr2, n); int arr3[] = {64, 25, 12, 22, 11}; selectionSort(arr3, n); printf("Sorted array with Selection Sort:\n"); printArray(arr3, n); return 0; }
Reverse the elements of an array.
#includevoid reverseArray(int arr[], int start, int end) { int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {1, 2, 3, 4, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original array: "); printArray(arr, n); reverseArray(arr, 0, n - 1); printf("Reversed array: "); printArray(arr, n); return 0; }
Find the largest element in a given array.
#includeint findLargest(int arr[], int n) { int largest = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > largest) { largest = arr[i]; } } return largest; } int main() { int arr[] = {12, 34, 56, 78, 99, 23, 45}; int n = sizeof(arr) / sizeof(arr[0]); printf("The largest element in the array is %d\n", findLargest(arr, n)); return 0; }
Count the number of vowels in a given string.
#include#include #include int countVowels(char str[]) { int count = 0; for (int i = 0; i < strlen(str); i++) { char c = tolower(str[i]); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { count++; } } return count; } int main() { char str[100]; printf("Enter a string: "); gets(str); printf("Number of vowels in the string: %d\n", countVowels(str)); return 0; }
Concatenate two given strings.
#include#include void concatenate(char result[], char str1[], char str2[]) { int i = 0, j = 0; // Copy first string while (str1[i] != '\0') { result[i] = str1[i]; i++; } // Copy second string while (str2[j] != '\0') { result[i] = str2[j]; i++; j++; } result[i] = '\0'; // Null-terminate the result } int main() { char str1[100], str2[100], result[200]; printf("Enter the first string: "); gets(str1); printf("Enter the second string: "); gets(str2); concatenate(result, str1, str2); printf("Concatenated string: %s\n", result); return 0; }
Sort an array of strings alphabetically.
#include#include 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); } } } } void printStrings(char arr[][100], int n) { for (int i = 0; i < n; i++) { printf("%s\n", arr[i]); } } int main() { int n; printf("Enter number of strings: "); scanf("%d", &n); getchar(); // To consume the newline character after entering the number char arr[n][100]; printf("Enter the strings one by one:\n"); for (int i = 0; i < n; i++) { gets(arr[i]); } sortStrings(arr, n); printf("Strings in alphabetical order:\n"); printStrings(arr, n); return 0; }
Swap the values of two integers using pointers.
#includevoid swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x, y; printf("Enter two integers: "); scanf("%d %d", &x, &y); printf("Before swapping: x = %d, y = %d\n", x, y); swap(&x, &y); printf("After swapping: x = %d, y = %d\n", x, y); return 0; }
Dynamically allocate memory for an array and find its sum.
#include#include int main() { int n, *arr, sum = 0; printf("Enter the number of elements: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); if (arr == NULL) { printf("Memory not allocated.\n"); return 1; } printf("Enter the elements:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (int i = 0; i < n; i++) { sum += arr[i]; } printf("Sum of elements: %d\n", sum); free(arr); return 0; }
Define a structure for a student and use a pointer to access and modify its members.
#includestruct Student { char name[50]; int age; float gpa; }; void printStudent(struct Student *s) { printf("Name: %s\n", s->name); printf("Age: %d\n", s->age); printf("GPA: %.2f\n", s->gpa); } int main() { struct Student s; struct Student *ptr = &s; printf("Enter name: "); scanf("%s", ptr->name); printf("Enter age: "); scanf("%d", &ptr->age); printf("Enter GPA: "); scanf("%f", &ptr->gpa); printStudent(ptr); return 0; }
Dynamically allocate memory for a 2D array and perform matrix addition.
#include#include int main() { int rows, cols; printf("Enter the number of rows and columns: "); scanf("%d %d", &rows, &cols); int **matrix1 = (int **)malloc(rows * sizeof(int *)); int **matrix2 = (int **)malloc(rows * sizeof(int *)); int **result = (int **)malloc(rows * sizeof(int *)); for (int i = 0; i < rows; i++) { matrix1[i] = (int *)malloc(cols * sizeof(int)); matrix2[i] = (int *)malloc(cols * sizeof(int)); result[i] = (int *)malloc(cols * sizeof(int)); } printf("Enter elements of the first matrix:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; 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++) { 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:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%d ", result[i][j]); } printf("\n"); } for (int i = 0; i < rows; i++) { free(matrix1[i]); free(matrix2[i]); free(result[i]); } free(matrix1); free(matrix2); free(result); return 0; }
Implement a function to find the length of a string and reverse a string using pointers.
#includeint stringLength(char *str) { int length = 0; while (*str != '\0') { length++; str++; } return length; } void reverseString(char *str) { char *end = str + stringLength(str) - 1; char temp; while (str < end) { temp = *str; *str = *end; *end = temp; str++; end--; } } int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); printf("Original string: %s\n", str); reverseString(str); printf("Reversed string: %s\n", str); return 0; }
Define a structure for a student and store multiple student records in an array.
#includestruct Student { char name[50]; int roll; float marks; }; void printStudent(struct Student s) { printf("Name: %s\n", s.name); printf("Roll Number: %d\n", s.roll); printf("Marks: %.2f\n", s.marks); } int main() { int n; printf("Enter number of students: "); scanf("%d", &n); struct Student students[n]; for (int i = 0; i < n; i++) { printf("Enter details of student %d\n", i + 1); printf("Name: "); scanf("%s", students[i].name); printf("Roll Number: "); scanf("%d", &students[i].roll); printf("Marks: "); scanf("%f", &students[i].marks); } printf("\nStudent Details:\n"); for (int i = 0; i < n; i++) { printStudent(students[i]); } return 0; }
Write student records to a file and then read them back.
#includestruct Student { char name[50]; int roll; float marks; }; int main() { FILE *file; struct Student s; // Writing to file file = fopen("students.dat", "wb"); if (file == NULL) { printf("Unable to open file for writing.\n"); return 1; } printf("Enter details of the student to write to file\n"); printf("Name: "); scanf("%s", s.name); printf("Roll Number: "); scanf("%d", &s.roll); printf("Marks: "); scanf("%f", &s.marks); fwrite(&s, sizeof(struct Student), 1, file); fclose(file); // Reading from file file = fopen("students.dat", "rb"); if (file == NULL) { printf("Unable to open file for reading.\n"); return 1; } fread(&s, sizeof(struct Student), 1, file); printf("\nStudent Details from file:\n"); printf("Name: %s\n", s.name); printf("Roll Number: %d\n", s.roll); printf("Marks: %.2f\n", s.marks); fclose(file); return 0; }
Append a student record to an existing file and display all records.
#includestruct Student { char name[50]; int roll; float marks; }; int main() { FILE *file; struct Student s; // Appending to file file = fopen("students.dat", "ab"); if (file == NULL) { printf("Unable to open file for appending.\n"); return 1; } printf("Enter details of the student to append to file\n"); printf("Name: "); scanf("%s", s.name); printf("Roll Number: "); scanf("%d", &s.roll); printf("Marks: "); scanf("%f", &s.marks); fwrite(&s, sizeof(struct Student), 1, file); fclose(file); // Reading all records from file file = fopen("students.dat", "rb"); if (file == NULL) { printf("Unable to open file for reading.\n"); return 1; } printf("\nStudent Details from file:\n"); while (fread(&s, sizeof(struct Student), 1, file)) { printf("Name: %s\n", s.name); printf("Roll Number: %d\n", s.roll); printf("Marks: %.2f\n", s.marks); printf("\n"); } fclose(file); return 0; }
Update a student's record in the file.
#include#include struct Student { char name[50]; int roll; float marks; }; int main() { FILE *file; struct Student s; int rollToUpdate; int found = 0; printf("Enter roll number of the student to update: "); scanf("%d", &rollToUpdate); file = fopen("students.dat", "rb+"); if (file == NULL) { printf("Unable to open file for updating.\n"); return 1; } while (fread(&s, sizeof(struct Student), 1, file)) { if (s.roll == rollToUpdate) { printf("Current details:\n"); printf("Name: %s\n", s.name); printf("Roll Number: %d\n", s.roll); printf("Marks: %.2f\n", s.marks); printf("Enter new details:\n"); printf("Name: "); scanf("%s", s.name); printf("Marks: "); scanf("%f", &s.marks); fseek(file, -sizeof(struct Student), SEEK_CUR); fwrite(&s, sizeof(struct Student), 1, file); found = 1; break; } } if (!found) { printf("Student with roll number %d not found.\n", rollToUpdate); } fclose(file); return 0; }
Delete a student's record from the file.
#include#include struct Student { char name[50]; int roll; float marks; }; int main() { FILE *file, *tempFile; struct Student s; int rollToDelete; int found = 0; printf("Enter roll number of the student to delete: "); scanf("%d", &rollToDelete); file = fopen("students.dat", "rb"); if (file == NULL) { printf("Unable to open file for reading.\n"); return 1; } tempFile = fopen("temp.dat", "wb"); if (tempFile == NULL) { printf("Unable to open temporary file.\n"); fclose(file); return 1; } while (fread(&s, sizeof(struct Student), 1, file)) { if (s.roll != rollToDelete) { fwrite(&s, sizeof(struct Student), 1, tempFile); } else { found = 1; } } fclose(file); fclose(tempFile); remove("students.dat"); rename("temp.dat", "students.dat"); if (found) { printf("Student with roll number %d deleted successfully.\n", rollToDelete); } else { printf("Student with roll number %d not found.\n", rollToDelete); } return 0; }