ASSIGNMENT
Programming in C
By - DINESH (L.E.)
Enrollment No. - 61016412824
Q1. Write a Program to print the Fibonacci series using recursion.
Here is a simple C program to print the Fibonacci series using recursion:
#include <stdio.h>
// Function to find the nth Fibonacci number
int fibonacci(int n) {
// Base case: 0th and 1st Fibonacci numbers are 0 and 1
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
// Recursive case: nth Fibonacci number is the sum of the (n-1)th and (n-2)th Fibonacci
numbers
return fibonacci(n - 1) + fibonacci(n - 2);
int main() {
int n;
// Ask user for the number of terms in the Fibonacci series
printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
return 0;
OUTPUT
Q2. Write a Program to Reverse an Array.
Here is a C program that reverses an array:
#include <stdio.h>
// Function to reverse the array
void reverseArray(int arr[], int size) {
int start = 0, end = size - 1, temp;
while (start < end) {
// Swap the elements at start and end
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move the indices towards the center
start++;
end--;
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int n;
// Ask user for the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Ask user to input the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Print the original array
printf("Original Array: ");
printArray(arr, n);
// Reverse the array
reverseArray(arr, n);
// Print the reversed array
printf("Reversed Array: ");
printArray(arr, n);
return 0;
OUTPUT
Q3. Write a Program to find the factorial of a given number.
Here is a C program to find the factorial of a given number:
#include <stdio.h>
// Function to calculate factorial
long long int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive case
int main() {
int num;
// Ask user for the number
printf("Enter a number to find its factorial: ");
scanf("%d", &num);
// Ensure the number is non-negative
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and display the factorial
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
OUTPUT
Q4. Write a Program to search elements in an array (using Binary Search).
Here is a C program that implements binary search to search for an element in a sorted array:
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if the target is at the middle
if (arr[mid] == target) {
return mid; // Target found at index mid
// If the target is smaller, ignore the right half
if (arr[mid] > target) {
right = mid - 1;
// If the target is larger, ignore the left half
else {
left = mid + 1;
// If the target is not found
return -1;
}
int main() {
int n, target;
// Ask user for the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Ask user to input the elements of the array (sorted order)
printf("Enter the elements of the sorted array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Ask user for the element to search
printf("Enter the element to search for: ");
scanf("%d", &target);
// Perform binary search
int result = binarySearch(arr, n, target);
// Output the result
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
return 0;
}
OUTPUT
Q5. Write a Program to concatenate two strings.
Here is a C program to concatenate two strings:
#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Hello ";
char s2[] = "Dinesh";
// Concatenate str2 to str1
strcat(s1, s2);
printf("%s\n", s1);
return 0;
}
OUTPUT
Q6. Write a Program to check if the given string is a palindrome string or not.
Here is a C program to check if a given string is a palindrome or not:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to check if the string is a palindrome
int isPalindrome(char str[]) {
int start = 0;
int end = strlen(str) - 1;
// Convert the string to lowercase for case-insensitive comparison
while (start < end) {
// Skip non-alphanumeric characters
if (!isalnum(str[start])) {
start++;
} else if (!isalnum(str[end])) {
end--;
} else {
// Compare characters, ignoring case
if (tolower(str[start]) != tolower(str[end])) {
return 0; // Not a palindrome
start++;
end--;
return 1; // Is a palindrome
int main() {
char str[100];
// Ask user for input string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove the newline character if present
str[strcspn(str, "\n")] = '\0';
// Check 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;
OUTPUT
Q7. Write a program to sort an array using Insertion Sort.
Here is a C program that implements Insertion Sort to sort an array of integers:
#include <stdio.h>
// Function to perform Insertion Sort
void insertionSort(int arr[], int n) {
int i, key, j;
// Traverse through 1 to n
for (i = 1; i < n; i++) {
key = arr[i]; // The element to be inserted
j = i - 1;
// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
// Place the key at after the last element greater than it
arr[j + 1] = key;
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int n;
// Ask user for the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Ask user to input the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Sort the array using Insertion Sort
insertionSort(arr, n);
// Print the sorted array
printf("Sorted array: ");
printArray(arr, n);
return 0;
OUTPUT
Q8. Write a program to sort an array using Quick Sort.
Here is a C program that implements the Quick Sort algorithm to sort an array of integers:
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
// Partition function for Quick Sort
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Taking the last element as the pivot
int i = (low - 1); // Index of smaller element
// Rearrange the array by placing elements smaller than pivot on the left and larger on the
right
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++; // Increment the index of smaller element
swap(&arr[i], &arr[j]);
// Swap the pivot element with the element at i + 1, so that the pivot is in its correct position
swap(&arr[i + 1], &arr[high]);
return (i + 1); // Return the partition index
}
// Quick Sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
// Find the pivot element that divides the array into two parts
int pi = partition(arr, low, high);
// Recursively sort the sub-arrays
quickSort(arr, low, pi - 1); // Before partition
quickSort(arr, pi + 1, high); // After partition
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int n;
// Ask user for the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Ask user to input the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
// Sort the array using Quick Sort
quickSort(arr, 0, n - 1);
// Print the sorted array
printf("Sorted array: ");
printArray(arr, n);
return 0;
OUTPUT
Q9. Write a program to store information on students using structure.
Here’s a C program that uses structures to store information about students, such as their
name, roll number, and marks:
#include <stdio.h>
#include <string.h>
// Define a structure to store student information
struct Student {
char name[50];
int rollNumber;
float marks;
};
// Function to input student information
void inputStudentInfo(struct Student* student) {
printf("Enter student's name: ");
fgets(student->name, sizeof(student->name), stdin);
student->name[strcspn(student->name, "\n")] = '\0'; // Remove newline character
printf("Enter roll number: ");
scanf("%d", &student->rollNumber);
printf("Enter marks: ");
scanf("%f", &student->marks);
// To clear the input buffer after reading marks
getchar();
// Function to display student information
void displayStudentInfo(struct Student student) {
printf("\nStudent Name: %s\n", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Marks: %.2f\n", student.marks);
int main() {
struct Student student;
// Input student information
inputStudentInfo(&student);
// Display student information
printf("\nStudent Information:\n");
displayStudentInfo(student);
return 0;
OUTPUT
Q10. Write a Program to calculate Compound Interest.
Here is a C program to calculate Compound Interest:
#include <stdio.h>
#include <math.h>
// Function to calculate compound interest
double calculateCompoundInterest(double principal, double rate, int time, int n) {
// Compound Interest formula: A = P(1 + r/n)^(nt)
// where A is the amount, P is the principal, r is the annual interest rate,
// n is the number of times interest is compounded per year, and t is the time in years
double amount = principal * pow((1 + rate / n), n * time);
return amount - principal; // Compound Interest = Amount - Principal
int main() {
double principal, rate, compoundInterest;
int time, n;
// Input the principal amount
printf("Enter the principal amount: ");
scanf("%lf", &principal);
// Input the annual interest rate
printf("Enter the annual interest rate (in percentage): ");
scanf("%lf", &rate);
// Input the time period in years
printf("Enter the time period (in years): ");
scanf("%d", &time);
// Input the number of times interest is compounded per year
printf("Enter the number of times interest is compounded per year: ");
scanf("%d", &n);
// Convert rate from percentage to decimal
rate = rate / 100;
// Calculate compound interest
compoundInterest = calculateCompoundInterest(principal, rate, time, n);
// Output the compound interest
printf("The compound interest is: %.2lf\n", compoundInterest);
return 0;
OUTPUT