CTSD 1 Practical's

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Practical-1

Print your name.

#include <stdio.h>

int main() {

printf("Your Name\n");

return 0;

}
Output:

Your Name

Print full name in 2 lines.

#include <stdio.h>

int main() {

printf("First Name\nLast Name\n");

return 0;

Output:

First Name

Last Name

Print your name in the center of the screen.

#include <stdio.h>

int main() {

printf("\n\n\n Your Name\n\n");

return 0;

}
Output:

Your Name

Print some patterns using ‘\n’ & ‘\t’.

#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

Find average of 3 numbers.


#include <stdio.h>
int main() {
float a = 10, b = 20, c = 30;
printf("Average: %.2f\n", (a + b + c) / 3);
return 0;
}

Output :
Average: 20.00

Find area of rectangle and circle.

#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

Swap 2 numbers without 3rd variable.

#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

Find the maximum of 2 numbers.


#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("Max: %d\n", (a > b) ? a : b);
return 0;
}

Output:
Max: 10

Find the maximum of 3 numbers using nested if.


#include <stdio.h>
int main() {
int a = 5, b = 10, c = 3;
int max;
if (a > b) {
if (a > c) max = a;
else max = c;
} else {
if (b > c) max = b;
else max = c;
}
printf("Max: %d\n", max);
return 0;
}
Output :
Max: 10

Find the maximum of 3 numbers using else if else ladder.

#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

Generate student’s result based on percentage.

#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

Generate electricity bill based on usage of units.

#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

Find area of rectangle, circle and square using switch case.

#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

Reverse a given number.


#include <stdio.h>

int main() {

int num = 12345, reversed = 0;

while (num != 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

printf("Reversed Number: %d\n", reversed);

return 0;
}

Output:

Reversed Number: 54321

Check whether given number is palindrome.


#include <stdio.h>
int main() {
int num = 121, original = num, reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (original == reversed) {
printf("Palindrome\n");
} else {
printf("Not Palindrome\n");
}
return 0;
}

Output:
Palindrome

Check whether given number is Armstrong.


#include <stdio.h>
#include <math.h>
int main() {
int num = 153, sum = 0, original = num, digits = 0;
while (original != 0) {
original /= 10;
digits++;
}
original = num;
while (original != 0) {
int lastDigit = original % 10;
sum += pow(lastDigit, digits);
original /= 10;
}
if (sum == num) {
printf("Armstrong Number\n");
} else {
printf("Not Armstrong Number\n");
}
return 0;
}

Output:
Armstrong Number

Practical-5

Generate a Fibonacci series of N Numbers.


#include <stdio.h>
int main() {
int n = 10, t1 = 0, t2 = 1, nextTerm;
printf("Fibonacci Series: ");
for (int i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Output:
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Generate a Fibonacci series up to N Numbers.


#include <stdio.h>
int main() {
int n = 100, t1 = 0, t2 = 1, nextTerm;
printf("Fibonacci Series up to %d: ", n);
while (t1 <= n) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

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

Generate result sheet for 5 students using for loop.


#include <stdio.h>
int main() {
int marks[5] = {85, 90, 78, 88, 92};
printf("Result Sheet:\n");
for (int i = 0; i < 5; i++) {
printf("Student %d: %d\n", i + 1, marks[i]);
}
return 0;
}

Output:
Result Sheet:
Student 1: 85
Student 2: 90
Student 3: 78
Student 4: 88
Student 5: 92

Practical-6

Nested for loop pattern programs (Pyramids).


#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
printf("\n");
}
return 0;
}

Output:
*
***
*****
*******
*********

Practical-7

Multiply first 10 numbers using 1-D Array.


#include <stdio.h>
int main() {
int arr[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
}
int product = 1;
for (int i = 0; i < 10; i++) {
product *= arr[i];
}
printf("Product of first 10 numbers: %d\n", product);
return 0;
}

Output: Product of first 10 numbers: 3628800


Arrange a given numbers in ascending order.

#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

Count total number of words in a string.


#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Count total number of words";
int count = 0;
char *token = strtok(str, " ");
while (token != NULL) {
count++;
token = strtok(NULL, " ");
}
printf("Total words: %d\n", count);
return 0;
}
Output :
Total words: 6

Find the length of a string.


#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of the string: %lu\n", strlen(str));
return 0;
}
Output:
Length of the string: 13

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!

Check whether given string is palindrome.


#include <stdio.h>
#include <string.h>
int main() {
char str[] = "madam";
int len = strlen(str);
int flag = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}
if (flag) {
printf("Palindrome\n");
} else {
printf("Not Palindrome\n");
}
return 0;
}

Output:
Palindrome
Practical-9

Create a calculator using UDF.


#include <stdio.h>
float add(float a, float b) { return a + b; }
float subtract(float a, float b) { return a - b; }
float multiply(float a, float b) { return a * b; }
float divide(float a, float b) { return a / b; }
int main() {
float a = 10, b = 5;
printf("Addition: %.2f\n", add(a, b));
printf("Subtraction: %.2f\n", subtract(a, b));
printf("Multiplication: %.2f\n", multiply(a, b));
printf("Division: %.2f\n", divide(a, b));
return 0;
}

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

Find Factorial of a number using Recursion.


#include <stdio.h>
int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d: %d\n", num, factorial(num));
return 0;
}
Output:
Factorial of 5: 120

Swap 2 numbers using Pass by Reference.

#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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy