CTSD 1 Practical's
CTSD 1 Practical's
CTSD 1 Practical's
#include <stdio.h>
int main() {
printf("Your Name\n");
return 0;
}
Output:
Your Name
#include <stdio.h>
int main() {
return 0;
Output:
First Name
Last Name
#include <stdio.h>
int main() {
return 0;
}
Output:
Your Name
#include <stdio.h>
int main() {
printf("Pattern:\n\t*\n\t*\t*\n\t*\t*\t*\n");
return 0;
}
Output :
Pattern:
* *
* * *
Practical-2
Add 2 numbers.
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("Sum: %d\n", a + b);
return 0;
}
Output :
Sum: 15
Output :
Average: 20.00
#include <stdio.h>
#define PI 3.14
int main() {
float length = 5, width = 10, radius = 7;
printf("Area of Rectangle: %.2f\n", length * width);
printf("Area of Circle: %.2f\n", PI * radius * radius);
return 0;
}
Output:
Area of Rectangle: 50.00
Area of Circle: 153.86
Swap 2 numbers using 3rd variable.
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("a: %d, b: %d\n", a, b);
return 0;
}
Output:
a: 10, b: 5
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a: %d, b: %d\n", a, b);
return 0;
}
Output:
a: 10, b: 5
Practical-3
Output:
Max: 10
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 3;
if (a > b && a > c) {
printf("Max: %d\n", a);
} else if (b > c) {
printf("Max: %d\n", b);
} else {
printf("Max: %d\n", c);
}
return 0;
}
Output:
Max: 10
#include <stdio.h>
int main() {
float percentage = 75.0;
if (percentage >= 60) {
printf("Result: Passed\n");
} else {
printf("Result: Failed\n");
}
return 0;
}
Output:
Result: Passed
#include <stdio.h>
int main() {
int units = 150;
float bill;
if (units <= 100) {
bill = units * 1.0;
} else if (units <= 200) {
bill = units * 1.5;
} else {
bill = units * 2.0;
}
printf("Bill: %.2f\n", bill);
return 0;
}
Output:
Bill: 225.00
Create calculator using switch case.
#include <stdio.h>
int main() {
int a = 10, b = 5, choice = 1;
switch (choice) {
case 1:
printf("Sum: %d\n", a + b);
break;
case 2:
printf("Difference: %d\n", a - b);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output:
Sum: 15
#include <stdio.h>
#define PI 3.14
int main() {
int choice = 2;
float length = 5, radius = 7, side = 4;
switch (choice) {
case 1:
printf("Area of Rectangle: %.2f\n", length * length);
break;
case 2:
printf("Area of Circle: %.2f\n", PI * radius * radius);
break;
case 3:
printf("Area of Square: %.2f\n", side * side);
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output:
Area of Circle: 153.86
Practical-4
Print the sum of first 10 numbers.
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("Sum: %d\n", sum);
return 0;
}
Output:
Sum: 55
Print the sum of odd and even numbers between 51 and 550.
#include <stdio.h>
int main() {
int sumOdd = 0, sumEven = 0;
for (int i = 51; i <= 550; i++) {
if (i % 2 == 0) sumEven += i;
else sumOdd += i;
}
printf("Sum of Odd: %d, Sum of Even: %d\n", sumOdd, sumEven);
return 0;
}
Output:
Sum of Odd: 75000, Sum of Even: 751
int main() {
while (num != 0) {
num /= 10;
return 0;
}
Output:
Output:
Palindrome
Output:
Armstrong Number
Practical-5
Output:
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Output:
Fibonacci Series up to 100: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Generate a multiplication table for any given number.
#include <stdio.h>
int main() {
int number = 5;
printf("Multiplication Table of %d:\n", number);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
}
Output:
Multiplication Table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Output:
Result Sheet:
Student 1: 85
Student 2: 90
Student 3: 78
Student 4: 88
Student 5: 92
Practical-6
Output:
*
***
*****
*******
*********
Practical-7
#include <stdio.h>
void sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 8, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Sorted array: 1 2 3 5 8
Arrange a given numbers in descending order.
#include <stdio.h>
void sort_descending(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 8, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
sort_descending(arr, n);
printf("Sorted array in descending order: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Sorted array in descending order: 8 5 3 2 1
Reverse the order of numbers.
#include <stdio.h>
int main() {
int arr[] = {5, 2, 8, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Reversed array: ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Reversed array: 3 1 8 2 5
Add 2 Matrices.
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of Matrices:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Sum of Matrices:
68
10 12
Multiply 2 Matrices.
#include <stdio.h>
int main() {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[3][2] = {{7, 8}, {9, 10}, {11, 12}};
int product[2][2] = {0};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Product of Matrices:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Product of Matrices:
58 64
139 154
Practical-8
Toggling of string.
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World!";
for (int i = 0; str[i] != '\0'; i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
} else if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}
printf("Toggled string: %s\n", str);
return 0;
}
Output :
Toggled string: hELLO wORLD!
Output:
Palindrome
Practical-9
Output:
Addition: 15.00
Subtraction: 5.00
Multiplication: 50.00
Division: 2.00
Find area of rectangle, square and circle using UDF.
#include <stdio.h>
#define PI 3.14
float area_rectangle(float length, float width) { return length * width; }
float area_square(float side) { return side * side; }
float area_circle(float radius) { return PI * radius * radius; }
int main() {
printf("Area of Rectangle: %.2f\n", area_rectangle(5, 10));
printf("Area of Square: %.2f\n", area_square(4));
printf("Area of Circle: %.2f\n", area_circle(7));
return 0;
}
Output:
Area of Rectangle: 50.00
Area of Square: 16.00
Area of Circle: 153.86
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output :
After swapping: x = 10, y = 5