CPUCL LAB
CPUCL LAB
SUBMITTED BY SUBMTTED TO
A501132524077 ASET
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.
9.
WAP to sum of n natural number.
13.
WAP to print the following pattern using for loop
26. WAP to write data into file and read data from file.
PRACTICAL NO: 1
PROGRAM:
#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest, amount;
return 0;
}
PRACTICAL NO: 2
PROGRAM:
#include <stdio.h>
int main() {
int a, b, temp;
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;
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;
}
PROGRAM:
#include <stdio.h>
int main() {
int 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
PROGRAM:
#include <stdio.h>
int main() {
char operator;
float num1, num2, result;
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
PROGRAM:
#include <stdio.h>
int main() {
int number, i = 1;
unsigned long long factorial = 1;
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
do {
factorial *= i;
i++;
} while (i <= number);
return 0;
}
PRACTICAL NO: 8
PROGRAM:
#include <stdio.h>
int main() {
int n, i = 1;
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
PROGRAM:
#include <stdio.h>
int main() {
int n, sum = 0;
if (n <= 0) {
printf("Please enter a positive integer.\n");
} else {
for (int i = 1; i <= n; i++) {
sum += i;
}
return 0;
}
PRACTICAL NO: 10
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: ");
return 0;
}
PRACTICAL NO: 11
PROGRAM:
#include <stdio.h>
int main() {
int number, reverse = 0, remainder;
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
}
return 0;
}
PRACTICAL NO: 12
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
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
PROGRAM:
#include <stdio.h>
int main() {
int n;
int marks[n][5];
return 0;
}
PRACTICAL NO: 15
PROGRAM:
#include <stdio.h>
int main() {
int x, y;
swap(x, y);
printf("After swap function call: x = %d, y = %d\n", x, y);
return 0;
}
PRACTICAL NO: 16
PROGRAM:
#include <stdio.h>
#include <ctype.h> // For the toupper() function
int main() {
char str[100];
return 0;
}
PRACTICAL NO: 17
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
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
}
}
}
}
int main() {
int a[10][10], b[10][10], sum[10][10], product[10][10];
int rowA, colA, rowB, colB;
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;
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
PROGRAM:
#include <stdio.h>
int main() {
int a, b;
return 0;
}
PRACTICAL NO: 21
PROGRAM:
#include <stdio.h>
#include <stdlib.h> // Required for malloc and free
int main() {
int n, i;
int *arr;
free(arr);
return 0;
}
PRACTICAL NO: 22
PROGRAM:
#include <stdio.h>
int main() {
int n, i;
int *ptr;
int arr[n];
ptr = arr;
return 0;
}
PRACTICAL NO: 23
PROGRAM:
#include <stdio.h>
struct Student {
int rollNumber;
char name[50];
float marks;
};
int main() {
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("\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
PROGRAM:
#include <stdio.h>
union Employee {
int empID;
char name[50];
float salary;
};
int main() {
union Employee emp;
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;
return 0;
}