Cpucl Lab 1
Cpucl Lab 1
SUBMITTED BY SUBMTTED TO
A50504823017 ASET
9.
WAP to sum of n natural number.
26. WAP to write data into file and read data from file.
PRACTICAL NO: 1
PROGRAM:
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;
return 0;
}
PRACTICAL NO: 4
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>
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;
}
AIM
PRACTICAL NO: 6
- WAP to calculate area of circle, rectangle, square and triangle using switch case
statement.
PROGRAM:
#include <stdio.h>
PROGRAM:
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
#include <stdio.h>
return 0; }
AIM:
PROGRAM:
PRACTICAL NO: 8
AIM:
PROGRAM:
#include <stdio.h>
int main() {
int n, i = 1;
while (i <= n)
{ printf("%d ", i);
i++;
}
printf("\n"); // Newline for better output formatting
}
return 0;
}
AIM:
PROGRAM:
PRACTICAL NO: 9
#include <stdio.h>
int main() {
int n, sum = 0;
AIM:
PROGRAM:
}
return 0;
}
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next
printf("Enter the number of terms: ");
scanf("%d", &n);
PROGRAM:
printf("Fibonacci Series: ");
return 0; }
PRACTICAL NO: 12
AIM:
PROGRAM:
WAP to reverse a number.
#include <stdio.h>
return 0;
}
PRACTICAL NO: 13
AIM:
4444
PROGRAM:
#include <stdio.h>
return 0;
}
PRACTICAL NO: 14
AIM:
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: 15
AIM:
PROGRAM:
#include <stdio.h>
int main() {
int n;
int marks[n][5];
AIM:
return 0;
}
PRACTICAL NO: 15
AIM:
WAP to swap two numbers using call by value.
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; }
AIM:
PROGRAM:
#include <stdio.h>
#include <ctype.h> // For the toupper() function
int main() {
char str[100];
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);
return
0; }
PRACTICAL NO: 18
AIM:
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;
PRACTICAL NO: 19
AIM:
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]);
}
}
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(str
1, 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;
PROGRAM:
#include <stdio.h>
struct Student {
int rollNumber;
char name[50];
float marks;
};
int main() {
return 0;
}
AIM:
PRACTICAL NO: 24
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
int main() {
struct Employee emp1;
return 0;
}
AIM:
PRACTICAL NO: 25
PROGRAM:
#include <stdio.h>
return 0;
}
AIM:
PRACTICAL NO: 26
WAP to write data into file and read data from file.
PROGRAM:
#include <stdio.h>
return 0;
}