C Practicals 1 To 3 by Nafis Parwez
C Practicals 1 To 3 by Nafis Parwez
C Practicals 1 To 3 by Nafis Parwez
Enrollment No - A710145023050
Subject - C Practicals
1 Module 1
2 Module 2
3 Module 3
#include <stdio.h>
int main() {
int num1, num2, num3, largest;
return 0;
}
int main() {
int number;
return 0;
}
int main() {
int num1, num2, temp;
return 0;
}
int main() {
int n, sum_of_squares = 0;
return 0;
}
int main() {
int n, a = 0, b = 1, nextTerm;
printf("\n");
return 0;
}
Module-2
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sample array
int n = sizeof(arr) / sizeof(arr[0]); // Size of the array
int numToSearch, i, found = 0;
// Linear search
for (i = 0; i < n; i++) {
if (arr[i] == numToSearch) {
found = 1;
break;
}
}
if (found) {
printf("%d found at index %d\n", numToSearch, i);
} else {
printf("%d not found in the array\n", numToSearch);
}
return 0;
}
2.2 Write a C program to find the maximum and minimum number in a given
array.
#include <stdio.h>
#include <limits.h> // For INT_MAX and INT_MIN
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sample array
int n = sizeof(arr) / sizeof(arr[0]); // Size of the array
int max, min;
// Initialize max and min with first element (assuming non-empty array)
max = min = arr[0];
return 0;
}
2.3 Write a C program to perform basic string operations using string functions.
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100]; // Arrays to store strings
// **Important note:** strcat modifies the first string (str1) in-place. Be cautious
with string sizes to avoid buffer overflows.
return 0;
}
Module-3
3.1 Write a C program to determine whether the entered character string is palindrome or not.
#include <stdio.h>
#include <ctype.h> // For tolower()
#include <string.h> // For strcspn and strlen
int main() {
char str[100];
int len, i, isPalindrome = 1;
return 0;
}
3.2 Write a C program to determine whether the entered character string is palindrome or not.
#include <stdio.h>
#include <string.h> // Included for strcspn
struct Employee {
char name[50];
int id;
float basic_salary, da, hra, gross_salary, net_salary;
};
int main() {
struct Employee emp;
// Get employee ID
printf("ID: ");
scanf("%d", &emp.id);
// Calculate HRA (House Rent Allowance) - You can adjust the calculation formula as
needed
emp.hra = 0.2 * emp.basic_salary;
return 0;
}
3.3 a) Write a C program to calculate the factorial of a number using recursion.
#include <stdio.h>
int factorial(int n) {
if (n == 0) { // Base case: factorial of 0 is 1
return 1;
} else {
return n * factorial(n - 1); // Recursive call: factorial(n) = n * factorial(n-
1)
}
}
int main() {
int number;
if (number < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
} else {
int fact = factorial(number);
printf("The factorial of %d is %d\n", number, fact);
}
return 0;
}
3.3 b) Write a C program to generate a Fibonacci series using recursion.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) { // Base case: 0th and 1st terms are 0 and 1
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive call: nth term is sum
of (n-1)th and (n-2)th terms
}
}
int main() {
int n, i;
return 0;
}