0% found this document useful (0 votes)
14 views

CPUCL LAB

Uploaded by

py3048908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CPUCL LAB

Uploaded by

py3048908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

ICPC LAB ASSIGNMENT

SUBMITTED BY SUBMTTED TO

SUBHI PUNDIR Mrs. YOJNA CHANDEL

A501132524077 ASET

B.TECH AI & ML 1st Sem


INDEX

S.No. Practical Name Signature


1. WAP to calculate simple interest and amount.

2. WAP to swap two numbers using third variable.

3. WAP to demonstrate greatest of 3 nos and to print the given


no in ascending order.

4. WAP to check if the number is even or odd.


WAP to perform arithmetic operations using switch case

5. statement.
6. WAP to calculate area of circle, rectangle, square and triangle
using switch case statement.
7. WAP to find factorial of given no using do while statement.

8. WAP to print number up to ‘n’.

9.
WAP to sum of n natural number.

10. WAP to print Fibonacci series.

11. WAP to reverse a number.

12. WAP to print the following pattern using for loop

13.
WAP to print the following pattern using for loop

14. WAP to read n number of students and 5 subjects marks.

15. WAP to swap two numbers using call by value.


16. WAP to convert all lowercase to uppercase characters.

17. WAP to find the factorial of a number using recursion.

18. WAP to print the add/product of two matrices of any order.

19. WAP to perform operations on strings using strings handling


in-built functions.

20. WAP to swap two numbers using call by reference.

21. WAP to perform dynamic memory allocation and


deallocation
22. WAP to print elements of array using pointers.

23. WAP to display student information by initializing structures.

24. WAP to find the total salary of employee and employee


details using structures.
25. WAP to store and display information using union.

26. WAP to write data into file and read data from file.
PRACTICAL NO: 1

AIM: WAP to calculate simple interest and amount.

PROGRAM:

#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest, amount;

printf("Enter Principal amount: ");


scanf("%f", &principal);
printf("Enter Rate of interest: ");
scanf("%f", &rate);
printf("Enter Time (in years): ");
scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;

amount = principal + simpleInterest;


printf("Simple Interest = %.2f\n", simpleInterest);
printf("Total Amount = %.2f\n", amount);

return 0;
}
PRACTICAL NO: 2

AIM: WAP to swap two numbers using third variable..

PROGRAM:
#include <stdio.h>

int main() {
int a, b, temp;

printf("Enter the value of a: ");


scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);

temp = a;
a = b;
b = temp;

printf("After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);

return 0;
}
PRACTICAL NO: 3

AIM: WAP to demonstrate greatest of 3 nos and to print the given no in ascending order.

PROGRAM:
#include <stdio.h>

int main() {
int a, b, c, greatest;

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c) {


greatest = a;
} else if (b >= a && b >= c) {
greatest = b;
} else {
greatest = c;
}

printf("The greatest number is: %d\n", greatest);

if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}

printf("Numbers in ascending order: %d %d %d\n", a, b, c);


return 0;
}
PRACTICAL NO: 4

AIM: WAP to check if the number is even or odd.

PROGRAM:
#include <stdio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}

return 0;
}
PRACTICAL NO: 5

AIM- WAP to perform arithmetic operations using switch case statement.

PROGRAM:
#include <stdio.h>

int main() {
char operator;
float num1, num2, result;

printf("Enter first number: ");


scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);

switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f + %.2f = %.2f\n", num1, num2, result);
break;

case '-':
result = num1 - num2;
printf("Result: %.2f - %.2f = %.2f\n", num1, num2, result);
break;

case '*':
result = num1 * num2;
printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);
break;

case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;

default:
printf("Error: Invalid operator.\n");
}

return 0;
}
PRACTICAL NO: 6

AIM - WAP to calculate area of circle, rectangle, square and triangle using switch case
statement.

PROGRAM:
#include <stdio.h>

int main() {
int choice;
float area, radius, length, breadth, side, base, height;
const float PI = 3.14159;

// Display menu
printf("Choose the shape to calculate area:\n");
printf("1. Circle\n");
printf("2. Rectangle\n");
printf("3. Square\n");
printf("4. Triangle\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

switch (choice) {
case 1: // Circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
break;

case 2: // Rectangle
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);
area = length * breadth;
printf("Area of the rectangle: %.2f\n", area);
break;

case 3: // Square
printf("Enter the side of the square: ");
scanf("%f", &side);
area = side * side;
printf("Area of the square: %.2f\n", area);
break;

case 4: // Triangle
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2f\n", area);
break;

default:
printf("Invalid choice! Please enter a number between 1 and 4.\n");
}

return 0;
}
PRACTICAL NO: 7

AIM: WAP to find factorial of given no using do while statement.

PROGRAM:
#include <stdio.h>

int main() {
int number, i = 1;
unsigned long long factorial = 1;

printf("Enter a positive integer: ");


scanf("%d", &number);

if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
do {
factorial *= i;
i++;
} while (i <= number);

printf("Factorial of %d = %llu\n", number, factorial);


}

return 0;
}
PRACTICAL NO: 8

AIM: WAP to print number up to ‘n’.

PROGRAM:
#include <stdio.h>

int main() {
int n, i = 1;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
printf("Numbers from 1 to %d:\n", n);

while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n"); // Newline for better output formatting
}

return 0;
}
PRACTICAL NO: 9

AIM: WAP to sum of n natural number.

PROGRAM:
#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
for (int i = 1; i <= n; i++) {
sum += i;
}

printf("The sum of the first %d natural numbers is: %d\n", n, sum);


}

return 0;
}
PRACTICAL NO: 10

AIM: WAP to print Fibonacci series.

PROGRAM:
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next
printf("Enter the number of terms: ");
scanf("%d", &n);

if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
printf("Fibonacci Series: ");

for (int i = 1; i <= n; i++) {


printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n"); // Newline for better output formatting
}

return 0;
}
PRACTICAL NO: 11

AIM: WAP to reverse a number.

PROGRAM:
#include <stdio.h>

int main() {
int number, reverse = 0, remainder;

printf("Enter a number: ");


scanf("%d", &number);

while (number != 0) {
remainder = number % 10; // Get the last digit
reverse = reverse * 10 + remainder; // Append the digit to reverse
number = number / 10; // Remove the last digit from number
}

printf("Reversed number: %d\n", reverse);

return 0;
}
PRACTICAL NO: 12

AIM: WAP to print the following pattern using for loop:


1

22

333

4444

PROGRAM:
#include <stdio.h>

int main() {
for (int i = 1; i <= 4; i++) {
// Loop to print the number i, i times
for (int j = 1; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}

return 0;
}
PRACTICAL NO: 13

AIM: WAP to print the following pattern using for loop:


A

AB

ABC

ABCD

PROGRAM:
#include <stdio.h>

int main() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
printf("%c", 'A' + j - 1); // 'A' is the starting character
}
printf("\n");
}

return 0;
}
PRACTICAL NO: 14

AIM: WAP to read n number of students and 5 subjects marks.

PROGRAM:
#include <stdio.h>

int main() {
int n;

printf("Enter the number of students: ");


scanf("%d", &n);

int marks[n][5];

for (int i = 0; i < n; i++) {


printf("\nEnter marks for student %d:\n", i + 1);
for (int j = 0; j < 5; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &marks[i][j]);
}
}

printf("\nMarks of students in 5 subjects:\n");


for (int i = 0; i < n; i++) {
printf("\nStudent %d: ", i + 1);
for (int j = 0; j < 5; j++) {
printf("%d ", marks[i][j]);
}
printf("\n");
}

return 0;
}
PRACTICAL NO: 15

AIM: WAP to swap two numbers using call by value.

PROGRAM:
#include <stdio.h>

void swap(int a, int b) {


int temp;
temp = a;
a = b;
b = temp;

printf("Inside swap function: a = %d, b = %d\n", a, b);


}

int main() {
int x, y;

printf("Enter two numbers: ");


scanf("%d %d", &x, &y);
printf("Before swap: x = %d, y = %d\n", x, y);

swap(x, y);
printf("After swap function call: x = %d, y = %d\n", x, y);

return 0;
}
PRACTICAL NO: 16

AIM: WAP to convert all lowercase to uppercase characters.

PROGRAM:
#include <stdio.h>
#include <ctype.h> // For the toupper() function

int main() {
char str[100];

printf("Enter a string: ");


fgets(str, sizeof(str), stdin); // Read a line of input

for (int i = 0; str[i] != '\0'; i++) {


str[i] = toupper(str[i]); // Convert each character to uppercase
}

printf("Converted string: %s\n", str);

return 0;
}
PRACTICAL NO: 17

AIM: WAP to find the factorial of a number using recursion.

PROGRAM:
#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}

int main() {
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is: %d\n", num, factorial(num));
}

return 0;
}
PRACTICAL NO: 18

AIM: WAP to print the add/product of two matrices of any order.

PROGRAM:
#include <stdio.h>

void addMatrices(int a[10][10], int b[10][10], int result[10][10], int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = a[i][j] + b[i][j]; // Matrix addition
}
}
}

void multiplyMatrices(int a[10][10], int b[10][10], int result[10][10], int rowA, int colA, int
rowB, int colB) {
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colB; j++) {
result[i][j] = 0;
for (int k = 0; k < colA; k++) {
result[i][j] += a[i][k] * b[k][j]; // Matrix multiplication
}
}
}
}

void printMatrix(int matrix[10][10], int row, int col) {


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int a[10][10], b[10][10], sum[10][10], product[10][10];
int rowA, colA, rowB, colB;

printf("Enter the number of rows and columns for matrix A: ");


scanf("%d %d", &rowA, &colA);
printf("Enter elements of matrix A:\n");
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
scanf("%d", &a[i][j]);
}
}

printf("Enter the number of rows and columns for matrix B: ");


scanf("%d %d", &rowB, &colB);
if (rowA != rowB || colA != colB) {
printf("Matrix addition is not possible, as the dimensions are different.\n");
return 0;
}
printf("Enter elements of matrix B:\n");
for (int i = 0; i < rowB; i++) {
for (int j = 0; j < colB; j++) {
scanf("%d", &b[i][j]);
}
}

addMatrices(a, b, sum, rowA, colA);


printf("\nMatrix A + Matrix B:\n");
printMatrix(sum, rowA, colA);

if (colA != rowB) {
printf("Matrix multiplication is not possible, as the number of columns of A is not equal to
the number of rows of B.\n");
} else {
multiplyMatrices(a, b, product, rowA, colA, rowB, colB);
printf("\nMatrix A * Matrix B:\n");
printMatrix(product, rowA, colB);
}

return 0;
}
PRACTICAL NO: 19

AIM: WAP to perform operations on strings using strings handling in-built functions.

PROGRAM:

#include <stdio.h>
#include <string.h> // For string handling functions
#include <ctype.h> // For toupper() and tolower()

int main() {
char str1[100], str2[100];
int choice;

printf("Enter the first string: ");


fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Removing the trailing newline character

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Removing the trailing newline character

printf("\nChoose an operation to perform on the strings:\n");


printf("1. Concatenate strings\n");
printf("2. Compare strings\n");
printf("3. Copy string\n");
printf("4. Find the length of a string\n");
printf("5. Convert to uppercase\n");
printf("6. Convert to lowercase\n");
printf("Enter your choice (1-6): ");
scanf("%d", &choice);
getchar(); // To consume the newline character left by scanf

switch(choice) {
case 1:
// Concatenate strings
strcat(str1, str2);
printf("\nConcatenated string: %s\n", str1);
break;

case 2:
if (strcmp(str1, str2) == 0)
printf("\nStrings are equal.\n");
else
printf("\nStrings are not equal.\n");
break;

case 3:
strcpy(str1, str2);
printf("\nString after copying: %s\n", str1);
break;

case 4:
printf("\nLength of first string: %lu\n", strlen(str1));
printf("Length of second string: %lu\n", strlen(str2));
break;

case 5:
for(int i = 0; str1[i]; i++) {
str1[i] = toupper(str1[i]);
}
for(int i = 0; str2[i]; i++) {
str2[i] = toupper(str2[i]);
}
printf("\nUppercase strings:\n");
printf("First string: %s\n", str1);
printf("Second string: %s\n", str2);
break;

case 6:
for(int i = 0; str1[i]; i++) {
str1[i] = tolower(str1[i]);
}
for(int i = 0; str2[i]; i++) {
str2[i] = tolower(str2[i]);
}
printf("\nLowercase strings:\n");
printf("First string: %s\n", str1);
printf("Second string: %s\n", str2);
break;

default:
printf("\nInvalid choice!\n");
break;
}
return 0;
}
PRACTICAL NO: 20

AIM: WAP to swap two numbers using call by reference.

PROGRAM:
#include <stdio.h>

void swap(int *x, int *y) {


int temp;
temp = *x; // Store the value of x in temp
*x = *y; // Assign the value of y to x
*y = temp; // Assign the value of temp to y
}

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

swap(&a, &b); // Call the swap function with addresses of a and b

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}
PRACTICAL NO: 21

AIM: WAP to perform dynamic memory allocation and deallocation.

PROGRAM:
#include <stdio.h>
#include <stdlib.h> // Required for malloc and free

int main() {
int n, i;
int *arr;

printf("Enter the number of elements: ");


scanf("%d", &n);

arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit if memory allocation fails
}

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Elements in the array are: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);

return 0;
}
PRACTICAL NO: 22

AIM: WAP to print elements of array using pointers.

PROGRAM:
#include <stdio.h>

int main() {
int n, i;
int *ptr;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

ptr = arr;

printf("Elements of the array are: ");


for (i = 0; i < n; i++) {
printf("%d ", *(ptr + i)); // Accessing elements using pointer arithmetic
}
printf("\n");

return 0;
}
PRACTICAL NO: 23

AIM: WAP to display student information by initializing structures.

PROGRAM:
#include <stdio.h>

struct Student {
int rollNumber;
char name[50];
float marks;
};

int main() {

struct Student student1 = {1, "Rahul", 89.5};

printf("Student Information:\n");
printf("Roll Number: %d\n", student1.rollNumber);
printf("Name: %s\n", student1.name);
printf("Marks: %.2f\n", student1.marks);

return 0;
}
PRACTICAL NO: 24

AIM: WAP to find the total salary of employee and employee details using structures.

PROGRAM:
#include <stdio.h>
#include <string.h> // Include string.h for strcspn

struct Employee {
int empID;
char name[50];
float basicSalary;
float hra; // House Rent Allowance
float da; // Dearness Allowance
};

int main() {
struct Employee emp1;

printf("Enter Employee ID: ");


scanf("%d", &emp1.empID);

printf("Enter Employee Name: ");


scanf(" %[^\n]%*c", emp1.name); // This reads input with spaces

emp1.name[strcspn(emp1.name, "\n")] = '\0'; // Remove the newline character at the end

printf("Enter Basic Salary: ");


scanf("%f", &emp1.basicSalary);

printf("Enter HRA (House Rent Allowance): ");


scanf("%f", &emp1.hra);

printf("Enter DA (Dearness Allowance): ");


scanf("%f", &emp1.da);

float totalSalary = emp1.basicSalary + emp1.hra + emp1.da;

printf("\nEmployee Details:\n");
printf("Employee ID: %d\n", emp1.empID);
printf("Name: %s\n", emp1.name);
printf("Basic Salary: %.2f\n", emp1.basicSalary);
printf("HRA: %.2f\n", emp1.hra);
printf("DA: %.2f\n", emp1.da);
printf("Total Salary: %.2f\n", totalSalary);

return 0;
}
PRACTICAL NO: 25

AIM: WAP to store and display information using union.

PROGRAM:

#include <stdio.h>

union Employee {
int empID;
char name[50];
float salary;
};

int main() {
union Employee emp;

printf("Enter Employee ID: ");


scanf("%d", &emp.empID);
printf("Employee ID: %d\n", emp.empID);

printf("Enter Employee Name: ");


scanf(" %[^\n]%*c", emp.name);
printf("Employee Name: %s\n", emp.name);

printf("Enter Employee Salary: ");


scanf("%f", &emp.salary);
printf("Employee Salary: %.2f\n", emp.salary);

return 0;
}
PRACTICAL NO: 26

AIM: WAP to write data into file and read data from file.

PROGRAM:
#include <stdio.h>

int main() {
FILE *file;
char name[50];
int age;
float salary;

file = fopen("employee.txt", "w"); // Open file in write mode


if (file == NULL) {
printf("Error opening file for writing!\n");
return 1;
}

printf("Enter name: ");


scanf(" %[^\n]%*c", name); // To read a string with spaces
printf("Enter age: ");
scanf("%d", &age);
printf("Enter salary: ");
scanf("%f", &salary);

fprintf(file, "Name: %s\n", name);


fprintf(file, "Age: %d\n", age);
fprintf(file, "Salary: %.2f\n", salary);

fclose(file); // Close the file after writing


printf("Data written to file successfully.\n");
file = fopen("employee.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("Error opening file for reading!\n");
return 1;
}

printf("\nReading data from file:\n");


while (fgets(name, sizeof(name), file) != NULL) { // Read line by line
printf("%s", name); // Print each line
}

fclose(file); // Close the file after reading

return 0;
}

You might also like