Computer Programming Lab
Computer Programming Lab
#include <stdio.h>
int main() {
int number;
return 0;
}
OUTPUT
Computer Programming Lab
3. Displaying entered number with leading zeros and trailing zeros:
#include <stdio.h>
int main() {
int number;
// Leading zeros
printf("With leading zeros: %05d\n", number); // Assuming a 5-digit number
return 0;
}
OUTPUT
Computer Programming Lab
#include <stdio.h>
int main() {
int number;
// Right justification
printf("Right justified: %10d\n", number); // Assuming a field width of 10
// Left justification
printf("Left justified: %-10d\n", number); // Assuming a field width of 10
return 0;
}
OUTPUT
Computer Programming Lab
5.Displaying with different formatting specifiers:
#include <stdio.h>
int main() {
float number;
return 0;
}
OUTPUT
Computer Programming Lab
6.To find the greatest / smallest of three numbers:
#include <stdio.h>
int main() {
int num1, num2, num3;
return 0;
}
OUTPUT
Computer Programming Lab
7.To display pass class, second-class, distinction according to the marks entered from the keyboard:
#include <stdio.h>
int main() {
int marks;
return 0;
}
OUTPUT
Computer Programming Lab
8.To find even or odd numbers:
#include <stdio.h>
int main() {
int num;
return 0;
}
OUTPUT
Computer Programming Lab
9.To display spellings of numbers 1-10 on entry:
#include <stdio.h>
int main() {
int num;
// Displaying spellings
switch (num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
Computer Programming Lab
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
case 10:
printf("Ten\n");
break;
default:
printf("Invalid input\n");
}
return 0;
}
OUTPUT
Computer Programming Lab
10. Implementation and displaying the menu to execute 1. ADD, 2. SUBTRACT 3. MULTIPLICATION, 4.
DIVISION using switch case:
#include <stdio.h>
int main() {
int choice;
float num1, num2;
printf("Choose operation:\n");
printf("1. ADD\n");
printf("2. SUBTRACT\n");
printf("3. MULTIPLICATION\n");
printf("4. DIVISION\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Sum: %.2f\n", num1 + num2);
break;
case 2:
printf("Difference: %.2f\n", num1 - num2);
break;
case 3:
printf("Product: %.2f\n", num1 * num2);
break;
case 4:
if (num2 != 0) {
printf("Quotient: %.2f\n", num1 / num2);
Computer Programming Lab
} else {
printf("Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice\n");
}
return 0;
}
OUTPUT
Computer Programming Lab
11.To check whether there exist real (float) roots of a quadratic equation and if exist find them:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float discriminant, root1, root2;
// Calculate discriminant
discriminant = b * b - 4 * a * c;
// Display roots
printf("Root 1: %.2f\n", root1);
printf("Root 2: %.2f\n", root2);
} else {
printf("No real roots exist.\n");
}
return 0;
}
OUTPUT
Computer Programming Lab
12.To display our College name twenty times on the screen:
#include <stdio.h>
int main() {
int i;
return 0;
}
OUTPUT
Computer Programming Lab
13.To demonstrate Continue and Break statements within loop structure:
#include <stdio.h>
int main() {
int i;
// Continue statement
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
printf("\n");
// Break statement
for (i = 1; i <= 10; i++) {
if (i == 6) {
break; // Exit loop when i is 6
}
printf("%d ", i);
}
return 0;
}
OUTPUT
Computer Programming Lab
14.To add first ‘n’ natural, even, odd numbers using different loop structures:
#include <stdio.h>
int main() {
int n, i, sum_n = 0, sum_even = 0, sum_odd = 0;
printf("Enter a value for n: ");
scanf("%d", &n);
// Using a for loop
for (i = 1; i <= n; i++) {
sum_n += i;
}
// Using a while loop for even numbers
i = 2;
while (i <= n) {
sum_even += i;
i += 2;
}
// Using a do-while loop for odd numbers
i = 1;
do {
sum_odd += i;
i += 2;
} while (i <= n);
printf("Sum of first %d natural numbers: %d\n", n, sum_n);
printf("Sum of first %d even numbers: %d\n", n, sum_even);
printf("Sum of first %d odd numbers: %d\n", n, sum_odd);
return 0;
}
OUTPUT
Computer Programming Lab
15.To find GCD, LCM of two integral numbers:
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
OUTPUT
Computer Programming Lab
16.To generate a simple number triangle for n rows:
#include <stdio.h>
int main() {
int i, j, n;
return 0;
}
OUTPUT
Computer Programming Lab
17. To generate Pascal triangle for n rows:
#include <stdio.h>
long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int i, j, n;
return 0;
}
OUTPUT
Computer Programming Lab
18. To add the series 1 + (1 + 2) + (1 + 2 + 3) + …+ (1 + 2 + 3 + … + n):
#include <stdio.h>
int main() {
int i, j, n, sum = 0;
return 0;
}
OUTPUT
Computer Programming Lab
19. To generate all prime numbers within the given range:
#include <stdio.h>
int main() {
int start, end;
#include <stdio.h>
// Example usage:
int main() {
int arr[] = {4, 2, 8, 1, 6, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int min, max;
return 0;
}
OUTPUT
Computer Programming Lab
22.Sort array elements in ascending/descending order:
#include <stdio.h>
// Example usage:
int main() {
Computer Programming Lab
int arr[] = {4, 2, 8, 1, 6, 5};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
OUTPUT
Computer Programming Lab
23.To enter elements for 3X3 matrix and display them.
#include <stdio.h>
// Example usage:
int main() {
int matrix[3][3];
return 0;
}
OUTPUT
Computer Programming Lab
24.To calculate addition / subtraction of 2-dimensional matrix
#include <stdio.h>
// Example usage:
int main() {
int matrixA[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrixB[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int resultMatrixAdd[3][3], resultMatrixSubtract[3][3];
return 0;
}
OUTPUT
Computer Programming Lab
25. To calculate multiplication of 2-dimensional matrix.
#include <stdio.h>
// Example usage:
int main() {
int matrixA[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrixB[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int resultMatrix[3][3];
Computer Programming Lab
return 0;
}
OUTPUT
Computer Programming Lab
26.To find the number of vowels and consonants in a string.
#include <stdio.h>
#include <ctype.h>
while (*str) {
char currentChar = tolower(*str);
str++;
}
}
// Example usage:
int main() {
char inputString[100];
int vowelCount, consonantCount;
return 0;
}
Output
Computer Programming Lab
27. Implementation of strlen(), strcpy(), strcat() and strcmp() functions.
#include <stdio.h>
while (*destination) {
destination++;
}
// Example usage:
int main() {
const char *str1 = "Hello";
const char *str2 = "World";
char buffer[20];
// strlen() example
printf("Length of '%s': %zu\n", str1, strlen_custom(str1));
// strcpy() example
strcpy_custom(buffer, str1);
printf("Copy of '%s': %s\n", str1, buffer);
// strcat() example
strcat_custom(buffer, " ");
strcat_custom(buffer, str2);
printf("Concatenation of '%s' and '%s': %s\n", str1, str2, buffer);
// strcmp() example
Computer Programming Lab
int compareResult = strcmp_custom(str1, str2);
printf("Comparison of '%s' and '%s': %d\n", str1, str2, compareResult);
return 0;
}
OUTPUT
Computer Programming Lab
28. To check whether a string is palindrome or not.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// Function to check if a string is a palindrome
bool isPalindrome(const char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
if (str[i] != str[length - i - 1]) {
return false;
}
}
return true;
}
int main() {
char inputString[100];
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
// Remove the newline character from the input (if present)
inputString[strcspn(inputString, "\n")] = '\0';
if (isPalindrome(inputString)) {
printf("The string \"%s\" is a palindrome.\n", inputString);
} else {
printf("The string \"%s\" is not a palindrome.\n", inputString);
}
return 0;
}
OUTPUT
Computer Programming Lab
29. To replace a specific character/string by another character/string in a multiword string. C
#include <stdio.h>
#include <string.h>
if (pos != NULL) {
int oldLength = strlen(oldSubstring);
int newLength = strlen(newSubstring);
// Shift the remaining characters to make space for the new substring
memmove(pos + newLength, pos + oldLength, strlen(pos + oldLength) + 1);
// Example usage:
int main() {
char multiwordString[] = "This is a test string.";
char oldSubstr[] = "test";
char newSubstr[] = "example";
return 0;
}
OUTPUT
Computer Programming Lab
29. To make the abbreviated form of a multiword string.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to create an abbreviated form of a multiword string
void createAbbreviation(const char *inputString) {
if (inputString == NULL || *inputString == '\0') {
printf("Invalid input string.\n");
return;
}
// Print the first character of the first word in uppercase
printf("%c", toupper(inputString[0]));
// Iterate through the string to find spaces and print the next character after a space
for (int i = 0; i < strlen(inputString); i++) {
if (inputString[i] == ' ' && inputString[i + 1] != '\0') {
printf("%c", toupper(inputString[i + 1]));
}
}
printf("\n");
}
// Example usage:
int main() {
char multiwordString[] = "This is a multiword string";
printf("Original string: %s\n", multiwordString);
printf("Abbreviation: ");
createAbbreviation(multiwordString);
return 0;
}
OUTPUT
Computer Programming Lab
30. To calculate the value of nCr, n≥r using function:
#include <stdio.h>
return 0;
}
OUTPUT
Computer Programming Lab
31. To find the sum of the series 1 + 1/2 + ⋯ for n ≥ 1, x ≥ 0 using function:
#include <stdio.h>
// Function to calculate factorial
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
// Function to calculate the sum of the series
double seriesSum(int n, double x) {
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += 1.0 / (factorial(i) * x);
}
return sum;
}
// Example usage:
int main() {
int n;
double x;
printf("Enter values for n and x (n >= 1, x >= 0): ");
scanf("%d %lf", &n, &x);
printf("Sum of the series: %lf\n", seriesSum(n, x));
return 0;
}
OUTPUT
Computer Programming Lab
32.To interchange the biggest and smallest number in a one-dimensional array using function:
#include <stdio.h>
// Function to find the index of the smallest element in an array
int findMinIndex(int arr[], int size) {
int minIndex = 0;
for (int i = 1; i < size; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
// Function to find the index of the largest element in an array
int findMaxIndex(int arr[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (arr[i] > arr[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
// Function to interchange the biggest and smallest numbers
void interchangeMinMax(int arr[], int size) {
int minIndex = findMinIndex(arr, size);
int maxIndex = findMaxIndex(arr, size);
// Interchange the values
int temp = arr[minIndex];
arr[minIndex] = arr[maxIndex];
arr[maxIndex] = temp;
}
Computer Programming Lab
// Function to display the array
void displayArray(int arr[], int size) {
printf("Array elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Example usage:
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
interchangeMinMax(arr, size);
printf("After interchange:\n");
displayArray(arr, size);
return 0;
}
OUTPUT
Computer Programming Lab
33. To calculate addition, subtraction, and multiplication of 2-dimensional matrix using function:
#include <stdio.h>
// Example usage:
int main() {
int matA[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matB[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int resultMatrix[3][3];
printf("Matrix A:\n");
displayMatrix(matA);
printf("\nMatrix B:\n");
displayMatrix(matB);
// Addition
matrixAddition(matA, matB, resultMatrix);
printf("\nAddition of matrices:\n");
displayMatrix(resultMatrix);
// Subtraction
matrixSubtraction(matA, matB, resultMatrix);
Computer Programming Lab
printf("\nSubtraction of matrices:\n");
displayMatrix(resultMatrix);
// Multiplication
matrixMultiplication(matA, matB, resultMatrix);
printf("\nMultiplication of matrices:\n");
displayMatrix(resultMatrix);
return 0;
}
OUTPUT
Computer Programming Lab
34. Write a program in C to find GCD of two numbers using recursion:
#include <stdio.h>
// Example usage:
int main() {
int num1, num2;
return 0;
}
OUTPUT
Computer Programming Lab
35. To calculate factorial of any given number using recursion:
#include <stdio.h>
// Example usage:
int main() {
int num;
return 0;
}
OUTPUT
Computer Programming Lab
36. To demonstrate call by reference, call by value:
#include <stdio.h>
// Example usage:
int main() {
int a = 10, b = 15;
printf("Call by Value:\n");
printf("Before function call, a = %d\n", a);
callByValue(a);
printf("After function call, a = %d\n", a);
printf("\nCall by Reference:\n");
printf("Before function call, b = %d\n", b);
callByReference(&b);
Computer Programming Lab
printf("After function call, b = %d\n", b);
return 0;
}
OUTPUT
Computer Programming Lab
37. To read and display an integer array using pointer:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
charCount++;
if (!inWord) {
inWord = 1;
wordCount++;
} else {
// If the current character is a space, tab, or newline, set the inWord flag to 0
inWord = 0;
lineCount++;
printf("Text:\n%s\n", text);
// Display counts
int main() {
char *text;
if (text == NULL) {
return 1;
}
Computer Programming Lab
// Prompt user to enter text
free(text);
return 1;
processText(text);
free(text);
return 0;
OUTPUT
Computer Programming Lab
39. To read, display, add, and subtract two times defined using hour, minutes, and values of seconds:
#include <stdio.h>
result.seconds %= 60;
result.minutes %= 60;
return result;
}
Computer Programming Lab
if (result.seconds < 0) {
result.seconds += 60;
result.minutes--;
}
if (result.minutes < 0) {
result.minutes += 60;
result.hours--;
}
return result;
}
// Example usage:
int main() {
struct Time time1, time2, sum, difference;
// Subtraction
difference = subtractTimes(time1, time2);
printf("Difference of times: %02d:%02d:%02d\n", difference.hours, difference.minutes,
difference.seconds);
return 0;
}
OUTPUT
40. To read and display the contents of a structure variable using a pointer to a structure:
#include <stdio.h>
// Structure to represent a student
struct Student {
char name[50];
int rollNumber;
float marks;
};
// Function to read and display student information using a pointer to a structure
void readAndDisplayStudent(struct Student *s) {
printf("Enter student details:\n");
printf("Name: ");
scanf("%s", s->name);
Computer Programming Lab
printf("Roll Number: ");
scanf("%d", &(s->rollNumber));
printf("Marks: ");
scanf("%f", &(s->marks));
printf("\nStudent Details:\n");
printf("Name: %s\n", s->name);
printf("Roll Number: %d\n", s->rollNumber);
printf("Marks: %.2f\n", s->marks);
}
// Example usage:
int main() {
struct Student student;
readAndDisplayStudent(&student);
return 0;
}
OUTPUT
Computer Programming Lab
41. Write a program in C to create a singly linked list of n nodes and display it in reverse order:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *next;
};
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
scanf("%d", &(newNode->data));
newNode->next = NULL;
if (head == NULL) {
head = newNode;
temp = newNode;
} else {
temp->next = newNode;
Computer Programming Lab
temp = newNode;
}
}
return head;
}
// Function to display linked list in reverse order
void displayReverse(struct Node *head) {
if (head == NULL) {
return;
}
displayReverse(head->next);
printf("%d ", head->data);
}
// Example usage:
int main() {
int n;
struct Node *head;
printf("Enter the number of nodes: ");
scanf("%d", &n);
head = createLinkedList(n);
printf("Linked list in reverse order: ");
displayReverse(head);
printf("\n");
return 0;
}
OUTPUT
Computer Programming Lab
42. Write a program in C to insert a new node to a Singly Linked List after a desired node and display the
list:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *next;
};
newNode->data = newData;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
// Example usage:
int main() {
struct Node *head = NULL;
struct Node *first = (struct Node *)malloc(sizeof(struct Node));
struct Node *second = (struct Node *)malloc(sizeof(struct Node));
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *next;
};
// Example usage:
int main() {
struct Node *head = NULL;
struct Node *first = (struct Node *)malloc(sizeof(struct Node));
struct Node *second = (struct Node *)malloc(sizeof(struct Node));
struct Node *third = (struct Node *)malloc(sizeof(struct Node));
first->data = 1;
first->next = second;
second->data = 2;
Computer Programming Lab
second->next = third;
third->data = 3;
third->next = NULL;
head = first;
return 0;
}
OUTPUT
Computer Programming Lab
44. Implement Stack and Queue data structure using dynamic memory allocation:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *array;
int capacity;
int top;
} Stack;
push(stack, 1);
push(stack, 2);
push(stack, 3);
freeStack(stack);
return 0;
}
OUTPUT
Computer Programming Lab
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *array;
int capacity;
int front;
int rear;
int size;
} Queue;
int main() {
Computer Programming Lab
Queue *queue = createQueue(5);
enqueue(queue, 1);
enqueue(queue, 2);
enqueue(queue, 3);
freeQueue(queue);
return 0;
}
OUTPUT