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

55 Coding Program .Prabhatchaudhary118

go on practice and be a one of the best coder in your life .
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

55 Coding Program .Prabhatchaudhary118

go on practice and be a one of the best coder in your life .
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 91

1

PRABHAT CHAUDHARY 11232767 G1


EXPERIMENT NO.1
WRITE A PROGRAM TO SHOW TYPE CONVERSION IN C

#include<stdio.h>

int main() {

// create a double variable

double value = 4150.13;

printf("Double Value: %.2lf\n", value);

// convert double value to integer

int number = value;

printf("Integer Value: %d", number);

return 0;

Output:

Double Value: 4150.13

Integer Value
2
PRABHAT CHAUDHARY 11232767 G1
EXPERIMENT NO.2
C PROGRAM TO PERFORM ALL ARITHMETIC OPERATION

#include <stdio.h>

int main()

int num1, num2;

int sum, sub, mult, mod;

float div;

/*

* Input two numbers from user

*/

printf("Enter any two numbers: ");

scanf("%d%d", &num1, &num2);

/*

* Perform all arithmetic operations

*/

sum = num1 + num2;

sub = num1 - num2;

mult = num1 * num2;

div = (float)num1 / num2;

mod = num1 % num2;

/*

* Print result of all arithmetic operations

*/

printf("SUM = %d\n", sum);


3
PRABHAT CHAUDHARY 11232767 G1
printf("DIFFERENCE = %d\n", sub);

printf("PRODUCT = %d\n", mult);

printf("QUOTIENT = %f\n", div);

printf("MODULUS = %d", mod);

return 0;

OUTPUT:

Enter any two numbers: 10

10

SUM = 20

DIFFERENCE = 0

PRODUCT = 100

QUOTIENT = 1.000000

MODULUS = 0
4
PRABHAT CHAUDHARY 11232767 G1
EXPERIMENT NO.3
C PROGRAM TO MULTIPLY TWO FLOATING POINT NUMBERS.

#include <stdio.h>

int main() {

double a, b, product;

printf("Enter two numbers: ");

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

// Calculating product

product = a * b;

// %.2lf displays number up to 2 decimal point

printf("Product = %.2lf", product);

return 0;

OUTPUT:

Enter two numbers: 10

20

Product = 200.00

EXPERIMENT NO.4
5
PRABHAT CHAUDHARY 11232767 G1
FIND THE VALUE OF ASCII CHARACTERS

#include <stdio.h>

int main() {

char c;

printf("Enter a character: ");

scanf("%c", &c);

// %d displays the integer value of a character

// %c displays the actual character

printf("ASCII value of %c = %d", c, c);

return 0;

Output:

Enter a character: h

ASCII value of h = 104

EXPERIMENT NO.5
SWAPPING TWO NUMBERS PROGRAM

#include<stdio.h>
6
PRABHAT CHAUDHARY 11232767 G1
int main() {

double first, second, temp;

printf("Enter first number: ");

scanf("%lf", &first);

printf("Enter second number: ");

scanf("%lf", &second);

// value of first is assigned to temp

temp = first;

// value of second is assigned to first

first = second;

// value of temp (initial value of first) is assigned to second

second = temp;

// %.2lf displays number up to 2 decimal points

printf("\nAfter swapping, first number = %.2lf\n", first);

printf("After swapping, second number = %.2lf", second);

return 0;

EXPERIMENT NO.6
WRITE A PROGRAM TO CALCULATE SIMPLE INTEREST IN C PROGRAM

#include<stdio.h>

int main()
7
PRABHAT CHAUDHARY 11232767 G1
{

int P,T;

float R, SimpleInterest;

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

scanf("%d",&P);

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

scanf("%F",&R);

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

scanf("%d",&T);

SimpleInterest = (P*R*T)/100;

printf("The Simple interest will be %.2f",SimpleInterest);

OUTPUT:

Enter the value of principle:10

Enter the value of rate:20

Enter the value of years:30

The Simple interest will be 60.00

EXPERIMENT NO.7
FIND THE PERIMETER AND AREA OF A TRAINGLE

PERIMETR

#include <stdio.h>

int main() {
8
PRABHAT CHAUDHARY 11232767 G1
float x, y, z, P, A; // Declare variables for side lengths and perimeter

// Prompt user for the side lengths and store in 'x', 'y', and 'z'

printf("\nInput the first number: ");

scanf("%f", &x);

printf("\nInput the second number: ");

scanf("%f", &y);

printf("\nInput the third number: ");

scanf("%f", &z);

if(x < (y+z) && y < (x+z) && z < (y+x)) // Check if the sides can form a triangle

P = x+y+z; // Calculate perimeter

printf("\nPerimeter = %.1f\n", P); // Print perimeter

else

printf("Not possible to create a triangle..!"); // Print message if it's not possible to


form a triangle

return 0;

Output;

Input the first number: 50

Input the second number: 30

Input the third number: 70

Perimeter = 150.0
9
PRABHAT CHAUDHARY 11232767 G1
AREA

#include <stdio.h>

int main() {

float base, height, area;

printf("Enter the base of the triangle: ");

scanf("%f", &base);

printf("Enter the height of the triangle: ");

scanf("%f", &height);

area = (base * height) / 2;

printf("The area of the triangle is: %.2f\n", area);

return 0;

Output:

Enter the base of the triangle: 20

Enter the height of the triangle: 40

The area of the triangle is: 400.00

EXPERIMENT NO.8
WRITE A PROGRAM TO CHECK WHETHER A NUMBR IS NEAGATIVE,POSITIVE,ZERO

#include <stdio.h>

int main()

{
10
PRABHAT CHAUDHARY 11232767 G1
int A;

printf("Enter the number A: ");

scanf("%d", &A);

if (A > 0)

printf("%d is positive.", A);

else if (A < 0)

printf("%d is negative.", A);

else if (A == 0)

printf("%d is zero.", A);

return 0;

Output:

Enter the number A: 5

5 is positive.

Enter the number A: -7

-7 is negative.

Enter the number A: 0

0 is zero.

EXPERIMENT NO.9
WRITE A PROGRAM TO CHECK HOW WE CAN USE IF ELSE TO “OPEN A DOOR” IF THE
USER ENTER CORRECT CODE

#include <stdio.h>

int main()

{
11
PRABHAT CHAUDHARY 11232767 G1
int A;

printf("Enter the number A: ");

scanf("%d", &A);

if (A > 10)

printf("%dif is correct code.", A);

else if (A < 0)

printf("%d is incorrect code.",A );

return 0;

Output:

Enter the number A: 100

100if is correct code.

Enter the number A: -2

-2 is incorrect code.

EXPERIMENT NO.10
HOW YOU CAN USE IF..ELSE TO FIND OUT IF A NUMBER IS POSITIVE OR NEGATIVE.

#include <stdio.h>

int main() {

double num;

printf("Enter a number: ");


12
PRABHAT CHAUDHARY 11232767 G1
scanf("%lf", &num);

if (num < 0.0)

printf("You entered a negative number.");

else if (num > 0.0)

printf("You entered a positive number.");

else

printf("You entered 0.");

return 0;

OUTPUT:

Enter a number: -10

You entered a negative number.

EXPERIMENT NO.11
WAP TO PRINT EVEN/ODD NUMBERS BETWEEN 1 TO 50.

#include<stdio.h>

int main()

int i, count=0, j;

printf("odd numbers between 1 to 50 are:\n");


13
PRABHAT CHAUDHARY 11232767 G1
for(i=1; i<=50; i++)

for(j=2; j<i; j++)

if(i%j==0)

count++;

break;

if(count==0 && i!=1)

printf("%d\n", i);

count = 0;

return 0;

OUTPUT:

odd numbers between 1 to 50 are:

11

13

17
14
PRABHAT CHAUDHARY 11232767 G1
19

23

29

31

37

41

43

47

EXPERIMENT NO.12
WAP TO PRINT GIVEN NUMBER IS ARMSTRONG OR NOT?

#include <stdio.h>

int main() {

int num, originalNum, remainder, result = 0;

printf("Enter a integer: ");

scanf("%d", &num);
15
PRABHAT CHAUDHARY 11232767 G1
originalNum = num;

while (originalNum != 0) {

// remainder contains the last digit

remainder = originalNum % 10;

result += remainder;

// removing last digit from the orignal number/

originalNum /= 10;

if (result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

Output:

Enter a integer: 1

1 is an Armstrong number.

Enter a integer: 1 2 3 4 5 6 7 8 9

1 is an Armstrong number.

Enter a integer: 10

10 is not an Armstrong number.

Enter a integer: 153

153 is not an Armstrong number.


16
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.13
WAP TO PRINT THE FIBONACCI SERIES.

#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


17
PRABHAT CHAUDHARY 11232767 G1
int nextTerm = t1 + t2;

// get no. of terms from user

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

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

OUTPUT:

Enter the number of terms: 20

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,
2584, 4181,
18
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.14
C PROGRAM TO MAKE A SIMPLE CALCULATOR

#include <stdio.h>

int main() {

char op;

double first, second;

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

scanf("%c", &op);

printf("Enter two operands: ");


19
PRABHAT CHAUDHARY 11232767 G1
scanf("%lf %lf", &first, &second);

switch (op) {

case '+':

printf("%.1lf + %.1lf = %.1lf", first, second, first + second);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf", first, second, first - second);

break;

case '*':

printf("%.1lf * %.1lf = %.1lf", first, second, first * second);

break;

case '/':

printf("%.1lf / %.1lf = %.1lf", first, second, first / second);

break;

// operator doesn't match any case constant

default:

printf("Error! operator is not correct");

return 0;

OUTPUT:

Enter an operator (+, -, *, /): -

Enter two operands: 5,2

5.0 - 0.0 = 5.0


20
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.15
LCM OF TWO NUMBERS IN C

#include <stdio.h>

int main() {

int n1, n2, max;

printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

// maximum number between n1 and n2 is stored in max

max = (n1 > n2) ? n1 : n2;

while (1) {
21
PRABHAT CHAUDHARY 11232767 G1
if ((max % n1 == 0) && (max % n2 == 0)) {

printf("The LCM of %d and %d is %d.", n1, n2, max);

break;

++max;

return 0;

OUTPUT:

Enter two positive integers: 20

50

The LCM of 20 and 50 is 100.

EXPERIMENT NO.16
WRITE A PROGRAM TO CHECK A GIVEN NUMBER IS PALIMDROM OR NOT

#include <stdio.h>

int main() {

int n, reversed = 0, remainder, original;

printf("Enter an integer: ");

scanf("%d", &n);

original = n;

// reversed integer is stored in reversed variable

while (n != 0) {

remainder = n % 10;
22
PRABHAT CHAUDHARY 11232767 G1
reversed = reversed * 10 + remainder;

n /= 10;

// palindrome if orignal and reversed are equal

if (original == reversed)

printf("%d is a palindrome.", original);

else

printf("%d is not a palindrome.", original);

return 0;

Output:

Enter an integer number: 123321

123321 is a palindrome.

Enter an integer number: 12321

12321 is not a palindrome number


23
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.17
FACTORS OF POSITIVE NUMBER

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {
24
PRABHAT CHAUDHARY 11232767 G1
for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

Output:

Enter an integer: 10

Factorial of 10 = 3628800

EXPERIMENT NO.18
C PROGRAM FACTORIAL EXAMPLE.

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

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


25
PRABHAT CHAUDHARY 11232767 G1
fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

Output:

Enter an integer: 10

Factorial of 10 = 3628800

EXPERIMENT NO.19
C PROGRAM TO FIND THE SUM OF FIBONACCI NUMBERS AT EVEN INDEXES UP TO N
TERMS.

#include <stdio.h>

// Computes value of the first

// fibonacci numbers and stores

// the even-indexed sum

int calculateEvenSum(int n)

// return 0 if n is equals or less than to 0

if (n <= 0)

return 0;

int fibo[2 * n + 1];

fibo[0] = 0, fibo[1] = 1;
26
PRABHAT CHAUDHARY 11232767 G1
// Initialize the result

int sum = 0;

// Adding the remaining terms

for (int i = 2; i <= 2 * n; i++) {

fibo[i] = fibo[i - 1] + fibo[i - 2];

// For even indices

if (i % 2 == 0)

sum += fibo[i];

// Return alternating sum

return sum;

// Driver Code

int main()

// Get n

int n = 5;

// calculateEvenSum(n) function is computed and return

// the sum of even-indices Fibonacci numbers.

int sum = calculateEvenSum(n);

// display result

printf("Even indexed Fibonacci Sum upto %d terms = %d",

n, sum);

return 0;

}
27
PRABHAT CHAUDHARY 11232767 G1

OUTPUT:

indexed Fibonacci Sum upto 5 terms = 88

EXPERIMENT NO.20
C PROGRAM TO PRINT TRIANGLE

#include <stdio.h>

int main() {

int i, space, rows, k = 0;

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

scanf("%d", &rows);

for (i = 1; i <= rows; ++i, k = 0) {

for (space = 1; space <= rows - i; ++space) {

printf(" ");

while (k != 2 * i - 1) {

printf("* ");

++k;
28
PRABHAT CHAUDHARY 11232767 G1
}

printf("\n");

return 0;

Enter the number of rows: 4

***

*****

*******

EXPERIMENT NO.21
WAP TO FIND THE MAXIMUM AND MINIMUM VALUE FROM ARRAY.

#include <limits.h>

#include <stdio.h>

// Function to find the minimum and

// maximum element of the array

void findMinimumMaximum(int arr[], int N)

int i;

// variable to store the minimum

// and maximum element

int minE = INT_MAX, maxE = INT_MIN;

// Traverse the given array

for (i = 0; i < N; i++) {

// If current element is smaller


29
PRABHAT CHAUDHARY 11232767 G1
// than minE then update it

if (arr[i] < minE) {

minE = arr[i];

// If current element is greater

// than maxE then update it

if (arr[i] > maxE) {

maxE = arr[i];

// Print the minimum and maximum element

printf("The minimum element is %d", minE);

printf("\n");

printf("The maximum element is %d", maxE);

return;

// Driver Code

int main()

// Given array

int arr[] = { 1, 2, 4, -1 };

// length of the array

int N = sizeof(arr) / sizeof(arr[0]);

// Function call

findMinimumMaximum(arr, N);
30
PRABHAT CHAUDHARY 11232767 G1
return 0;

OUTPUT:

The minimum element is -1

The maximum element is 4

EXPERIMENT NO.22
WAP TO PRINT THE SUM OF ALL ARRAY ELEMENT.

#include <stdio.h>

int main() {

int arr[5];

int i;

float sum = 0;

printf("Enter 5 elements:\n");

for (i = 0; i < 5; i++) {

scanf("%d", &arr[i]);

sum = sum + arr[i];

printf("Sum of array elements: %f\n", sum);

return 0;

}
31
PRABHAT CHAUDHARY 11232767 G1

OUTPUT:

Enter 5 elements:

12345

Sum of array elements: 15.000000


#include <stdio.h>

EXPERIMENT NO.23
C PROGRAM FOR PRINTING INVERTED PYRAMID

#include <stdio.h>

int main()

int rows = 5;

// first loop for printing all rows

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

// first inner loop for printing leading white

// spaces

for (int j = 0; j < 2 * i; j++) {

printf(" ");

// second inner loop for printing numbers

for (int k = 0; k < 2 * (rows - i) - 1; k++) {


32
PRABHAT CHAUDHARY 11232767 G1
printf("%d ", k + 1);

printf("\n");

OUTPUT:

123456789

1234567

12345

123

1
33
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.24
C PROGRAM TO PRINT HOLLOW STAR PYRAMID.

#include <stdio.h>

int main()

int rows = 5;

// first loop iterating through each row

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

// first inner loop to print leading white space

for (int j = 0; j < 2 * i + 1; j++) {

printf(" ");

// second inner loop to print star* and hollow white

// space

for (int k = 0; k < 2 * (rows - i) - 1; k++) {

if (k == 0 || k == 2 * (rows - i) - 2 || i == 0)
34
PRABHAT CHAUDHARY 11232767 G1
printf("* ");

else {

printf(" ");

printf("\n");

return 0;

OUTPUT:

*********

* *

* *

* *

*
35
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.25
PASS 2D ARRAY AS A PARAMETER IN C

#include <stdio.h>

// Function prototype

void printArray(int arr[][3], int rows, int cols);

int main() {

// Example 2D array

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

// Passing the 2D array to the function

printArray(arr, 2, 3);

return 0;

// Function definition

void printArray(int arr[][3], int rows, int cols) {

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

for (int j = 0; j < cols; j++) {

printf("%d ", arr[i][j]);


36
PRABHAT CHAUDHARY 11232767 G1
}

printf("\n");

OUTPUT:

123

456

EXPERIMENT NO.26
C 2D ARRAY EXAMPLE: STORING ELEMENTS IN A MATRIX AND PRINTING IT.

#include <stdio.h>

#define ROWS 3

#define COLS 3

void printMatrix(int matrix[][COLS], int rows, int cols);

int main() {

// Define a 2D array (matrix)

int matrix[ROWS][COLS];

// Prompt the user to enter elements for the matrix

printf("Enter elements for the %dx%d matrix:\n", ROWS, COLS);

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

for (int j = 0; j < COLS; j++) {

printf("Enter element at position (%d, %d): ", i + 1, j + 1);

scanf("%d", &matrix[i][j]);

// Print the matrix


37
PRABHAT CHAUDHARY 11232767 G1
printf("\nThe matrix is:\n");

printMatrix(matrix, ROWS, COLS);

return 0;

// Function to print the matrix

void printMatrix(int matrix[][COLS], int rows, int cols) {

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

for (int j = 0; j < cols; j++) {

printf("%d\t", matrix[i][j]);

printf("\n");

OUTPUT:

Enter elements for the 3x3 matrix:

Enter element at position (1, 1): 11

Enter element at position (1, 2): 12

Enter element at position (1, 3): 13

Enter element at position (2, 1): 21

Enter element at position (2, 2): 22

Enter element at position (2, 3): 23

Enter element at position (3, 1): 31

Enter element at position (3, 2): 32

Enter element at position (3, 3): 33


38
PRABHAT CHAUDHARY 11232767 G1
The matrix is:

11 12 13

21 22 23

31 32 33

EXPERIMENT NO.27
WAP TO PRINT DIAGONAL ELEMENTS OF A MATRIX

#include <stdio.h>

#define ROWS 3

#define COLS 3

void printDiagonalElements(int matrix[][COLS], int size);

int main() {

int matrix[ROWS][COLS] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

printf("Original Matrix:\n");

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

for (int j = 0; j < COLS; j++) {

printf("%d\t", matrix[i][j]);

printf("\n");

}
39
PRABHAT CHAUDHARY 11232767 G1
printf("\nDiagonal Elements:\n");

printDiagonalElements(matrix, ROWS);

return 0;

void printDiagonalElements(int matrix[][COLS], int size) {

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

printf("%d\t", matrix[i][i]);

printf("\n");

OUTPUT:

Original Matrix:

1 2 3

4 5 6

7 8 9

Diagonal Elements:

1 5 9
40
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.28
WAP TO PRINT RECORD OF A COLLEGE STUDENT USING UNION.

#include <stdio.h>

#include <string.h>

// Define a union to hold student record

union StudentRecord {

char name[50];

int roll_number;

float cgpa;

};

int main() {

union StudentRecord student;

// Input student information

printf("Enter student's name: ");

fgets(student.name, sizeof(student.name), stdin);

student.name[strcspn(student.name, "\n")] = '\0'; // Remove newline character from


fgets input

printf("Enter student's roll number: ");

scanf("%d", &student.roll_number);

printf("Enter student's CGPA: ");


41
PRABHAT CHAUDHARY 11232767 G1
scanf("%f", &student.cgpa);

// Print student information

printf("\nStudent Record:\n");

printf("Name: %s\n", student.name);

printf("Roll Number: %d\n", student.roll_number);

printf("CGPA: %.2f\n", student.cgpa);

return 0;

OUTPUT:

Enter student's name: PRABHAT CHAUDHARY

Enter student's roll number: 11232767

Enter student's CGPA: 6.2

Student Record:

Name: ff�@HAT CHAUDHARY

Roll Number: 1086744166

CGPA: 6.20
42
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.29
CALL BY VALUE AND CALL BY REFERENCE.

WAP TO SHOW CONCEPT OF FUNCTION NO ARGUMENTS AND WITH RETURN.

WAP TO SHOW CONCEPT OF FUNCTION WITH ARGUMENTS AND NO RETURN.

// C Program to implement

// Call by value

#include <stdio.h>

// Call by value

int sum(int x, int y)

int c;

c = x + y;

// Integer value retured

return c;

// Driver Code

int main()

// Integer Declared

int a = 3, b = 2;
43
PRABHAT CHAUDHARY 11232767 G1
// Function Called

int c = sum(a, b);

printf("Sum of %d and %d : %d", a, b, c);

return 0;

OUTPUT:

Sum of 3 and 2 : 5

// C Program to implement

// Call by reference

#include <stdio.h>

// Call by reference

void swap(int* x, int* y)

int temp = *x;

*x = *y;

*y = temp;

// Driver Code

int main()

// Declaring Integer

int x = 1, y = 5;

printf("Before Swapping: x:%d , y:%d\n", x, y);

// Calling the function

swap(&x, &y);
44
PRABHAT CHAUDHARY 11232767 G1
printf("After Swapping: x:%d , y:%d\n", x, y);

return 0;

OUTPUT:

Before Swapping: x:1 , y:5

After Swapping: x:5 , y:1


45
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.30
WAP TO SWAP TWO NUMBERS USING

CALL BY VALUE CALL BY REFERENCE

CALL BY VALUE

#include <stdio.h>

void swap(int x, int y){

int temp = x;

x = y;

y = temp;

int main(){

int x = 10;

int y = 11;

printf("Values before swap: x = %d, y = %d\n", x,y);

swap(x,y);

printf("Values after swap: x = %d, y = %d", x,y);

OUTPUT:

Values before swap: x = 10, y = 11

Values after swap: x = 10, y = 11


46
PRABHAT CHAUDHARY 11232767 G1
CALL BY REFERENCE

#include <stdio.h>

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

int temp = *x;

*x = *y;

*y = temp;

int main(){

int x = 10;

int y = 11;

printf("Values before swap: x = %d, y = %d\n", x,y);

swap(&x,&y);

printf("Values after swap: x = %d, y = %d", x,y);

OUTPUT:

Values before swap: x = 10, y = 11

Values after swap: x = 11, y = 10


47
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.31
CALCULATE THE SUM OF FIRST N NATURAL NUMBERS.

#include <stdio.h>

int main() {

int n, sum = 0;

// Input the value of N

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

scanf("%d", &n);

// Calculate the sum of first N natural numbers

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

sum += i;

// Display the sum

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

return 0;

OUTPUT:

Enter the value of N: 5

The sum of first 5 natural numbers is: 15


48
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.32
POINTER PROGRAM TO SWAP TWO NUMBERS WITHOUT USING THE 3RD VARIABLE.

#include <stdio.h>

void swap(int *a, int *b) {

*a = *a ^ *b;

*b = *a ^ *b;

*a = *a ^ *b;

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

OUTPUT:

Enter two numbers: 20

30

Before swapping: num1 = 20, num2 = 30

After swapping: num1 = 30, num2 = 20


49
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.33
HOW TO RETURN A POINTER FROM A FUNCTIONS IN C.

#include <stdio.h>

// Function that returns pointer

int* fun()

// Declare a static integer

static int A = 10;

return (&A);

// Driver Code

int main()

// Declare a pointer

int* p;

// Function call

p = fun();

// Print Address

printf("%p\n", p);

// Print value at the above address

printf("%d\n", *p);

return 0;

}
50
PRABHAT CHAUDHARY 11232767 G1
OUTPUT:

0x404030

10
51
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.34
HOW TO DECLARE A TWO DIMENSIONAL ARRAY OF POINTERS IN C.

#include <stdio.h>

// Drivers code

int main()

int arr1[5][5] = { { 0, 1, 2, 3, 4 },

{ 2, 3, 4, 5, 6 },

{ 4, 5, 6, 7, 8 },

{ 5, 4, 3, 2, 6 },

{ 2, 5, 4, 3, 1 } };

int* arr2[5][5];

// Initialising each element of the

// pointer array with the address of

// element present in the other array

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

for (int j = 0; j < 5; j++) {

arr2[i][j] = &arr1[i][j];

// Printing the array using

// the array of pointers

printf("The values are\n");

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


52
PRABHAT CHAUDHARY 11232767 G1
for (int j = 0; j < 5; j++) {

printf("%d ", *arr2[i][j]);

printf("\n");

return 0;

OUTPUT:

The values are

01234

23456

45678

54326

25431
53
PRABHAT CHAUDHARY 11232767 G1
EXPERIMENT NO.35
CONCATENATING TWO STRINGS IN C.

#include <stdio.h>

#include <string.h>

int main() {

char str1[50] = "Hello";

char str2[] = " world!";

strcat(str1, str2); // Concatenating str2 to str1

printf("Concatenated string: %s\n", str1);

return 0;

OUTPUT:

Concatenated string: Hello world!


54
PRABHAT CHAUDHARY 11232767 G1
EXPERIMENT NO.36
LOOP REVERSE A STRING PROGRAM

#include <stdio.h>

#include <string.h>

void reverseString(char *str) {

int length = strlen(str);

int i, j;

char temp;

for (i = 0, j = length - 1; i < j; i++, j--) {

temp = str[i];

str[i] = str[j];

str[j] = temp;

int main() {

char str[100];

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

// Removing the newline character at the end

if (str[strlen(str) - 1] == '\n') {

str[strlen(str) - 1] = '\0';

// Reversing the string

reverseString(str);

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


55
PRABHAT CHAUDHARY 11232767 G1
return 0;

OUTPUT:

Enter a string: 89

Reversed string: 98

EXPERIMENT NO.37
56
PRABHAT CHAUDHARY 11232767 G1
C PROGRAM TO FIND THE LENGTH OF A STRING

#include <stdio.h>

int main() {

char str[100];

int length = 0;

printf("Enter a string: ");

canf("%s", str);

// Loop until the end of string ('\0' character) is encountered

while (str[length] != '\0') {

length++;

printf("Length of the string: %d\n", length);

return 0;

OUTPUT:

Enter a string: 55

Length of the string: 2

EXPERIMENT NO.38
57
PRABHAT CHAUDHARY 11232767 G1
C PROGRAM TO STORE STUDENT RECORDS AS STRUCTURES AND SORT THEM BY
NAME.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Define the structure for student records

struct Student {

char name[50];

int roll_number;

float marks;

};

// Function to compare two student records based on name

int compareNames(const void *a, const void *b) {

return strcmp(((struct Student*)a)->name, ((struct Student*)b)->na);

int main() {

int n;

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

scanf("%d", &n);

// Allocate memory for 'n' student records

struct Student *students = (struct Student *)malloc(n * sizeo(structStudent));

// Input student records

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

printf("Enter name of student %d: ", i + 1);

scanf("%s", students[i].name);
58
PRABHAT CHAUDHARY 11232767 G1
printf("Enter roll number of student %d: ", i + 1);

scanf("%d", &students[i].roll_number);

printf("Enter marks of student %d: ", i + 1);

scanf("%f", &students[i].marks);

// Sort the student records by name

qsort(students, n, sizeof(struct Student), compareNames);

// Display the sorted student records

printf("\nSorted Student Records:\n");

printf("Name\t\tRoll Number\tMarks\n");

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

printf("%s\t\t%d\t\t%.2f\n", students[i].name, students[ .roll_number,


students[i].marks);}

// Free allocated memory

free(students);

return 0;

OUTPUT:

Enter the number of students: 3

Enter name of student 1: prabhat chaudhary

Enter roll number of student 1: Enter marks of student 1: Enter name of student
2:parv Enter roll number of student 2: 11232762

Enter marks of student 2: 98

Enter name of student 3: 3:shivansh

Enter roll number of student 3: 11232789


59
PRABHAT CHAUDHARY 11232767 G1

Enter marks of student 3:

Sorted Student Records:

Name Roll Number Marks

3: 0 0.00

chaudhary 11232762 98.00

prabhat 0 0.00

EXPERIMENT NO.39
60
PRABHAT CHAUDHARY 11232767 G1
READ/ WRITE STRUCTURE FROM TO A FILE IN C

#include <stdio.h>

#include <stdlib.h>

// a struct to be read and written

struct person {

int id;

char fname[20];

char lname[20];

};

int main()

FILE* outfile;

// open file for writing

outfile = fopen("person.bin", "wb");

if (outfile == NULL) {

fprintf(stderr, "\nError opened file\n");

exit(1);

struct person input1 = { 1, "rohan", "sharma" };

// write struct to file

int flag = 0;

flag = fwrite(&input1, sizeof(struct person), 1,

outfile);

if (flag) {

printf("Contents of the structure written "


61
PRABHAT CHAUDHARY 11232767 G1
"successfully");

else

printf("Error Writing to File!");

// close file

fclose(outfile);

return 0;

OUTPUT:

Error opened file

EXPERIMENT NO.40
COPY CONTENT FROM ONE FILE TO ANOTHER FILE
62
PRABHAT CHAUDHARY 11232767 G1
#include <stdio.h>

#include <stdlib.h> // For exit()

int main()

FILE *fptr1, *fptr2;

char filename[100];

int c;

printf("Enter the filename to open for reading: ");

scanf("%s", filename);

// Open one file for reading

fptr1 = fopen(filename, "r");

if (fptr1 == NULL)

printf("Cannot open file %s\n", filename);

exit(1);

printf("Enter the filename to open for writing: ");

scanf("%s", filename);

// Open another file for writing

fptr2 = fopen(filename, "w");

if (fptr2 == NULL)

printf("Cannot open file %s\n", filename);

exit(1);

}
63
PRABHAT CHAUDHARY 11232767 G1
// Read contents from file

while ((c = fgetc(fptr1)) != EOF)

fputc(c, fptr2);

printf("Contents copied to %s\n", filename);

fclose(fptr1);

fclose(fptr2);

return 0;

EXPERIMENT NO.41
C PROGRAM TO CONVERT 24 HOUR TIME TO 12 HOUR TIME

#include <stdio.h>
64
PRABHAT CHAUDHARY 11232767 G1
int main(void)

int morning, hour, min;

scanf("%02d%02d", &hour, &min);

if (hour > 23 || min > 59)

return 1;

//check am pm

if (hour >= 12)

morning = 1;

if (hour > 12)

hour -= 12;

else

morning = 0;

//print the result

if (morning == 0)

printf("%02d:%02d a.m.", hour, min);


65
PRABHAT CHAUDHARY 11232767 G1
}

else

printf("%02d:%02d p.m.", hour, min);

return 0;

OUTPUT:

59

07:59 a.m.

EXPERIMENT NO.42
C PROGRAM TO APPEND CONTENT OF ONE TEXT FILE TO ANOTHER.

// C program to append the contents of

// source file to the destination file


66
PRABHAT CHAUDHARY 11232767 G1
// including header files

#include <stdio.h>

// Function that appends the contents

void appendFiles(char source[],

char destination[])

// declaring file pointers

FILE *fp1, *fp2;

// opening files

fp1 = fopen(source, "a+");

fp2 = fopen(destination, "a+");

// If file is not found then return.

if (!fp1 && !fp2) {

printf("Unable to open/"

"detect file(s)\n");

return;

char buf[100];

// explicitly writing "\n"

// to the destination file

// so to enhance readability.

fprintf(fp2, "\n");

// writing the contents of

// source file to destination file.

while (!feof(fp1)) {
67
PRABHAT CHAUDHARY 11232767 G1
fgets(buf, sizeof(buf), fp1);

fprintf(fp2, "%s", buf);

rewind(fp2);

// printing contents of

// destination file to stdout.

while (!feof(fp2)) {

fgets(buf, sizeof(buf), fp2);

printf("%s", buf);

// Driver Code

int main()

char source[] = "file1.txt",

destination[] = "file2.txt";

// calling Function with file names.

appendFiles(source, destination);

return 0;

OUTPUT:

Unable to open/detect file(s)


68
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.43
IDENTIFY A LEAP YEAR

#include <stdio.h>

int main() {

int year;
69
PRABHAT CHAUDHARY 11232767 G1
printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {

printf("%d is a leap year.", year);

} else {

printf("%d is not a leap year.", year);

return 0;

OUTPUT:

Enter a year: 16

16 is a leap year.

EXPERIMENT NO.44
CHANGING TEXT BACKGROUND COLOR.

#include <stdio.h>

int main() {

// ANSI escape codes for text color


70
PRABHAT CHAUDHARY 11232767 G1
printf("\x1b[41m"); // Set background color to red

printf("\x1b[37m"); // Set text color to white

printf("Hello, colored text!");

// Reset colors to default

printf("\x1b[0m");

return 0;

OUTPUT:

Hello, colored text!

EXPERIMENT NO.45
DISPLAY CURRENT DATE AND TIME

#include<stdio.h>

#include<time.h>

int main()

{
71
PRABHAT CHAUDHARY 11232767 G1
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

time_t t; // not a primitive datatype

time(&t);

printf("\nThis program has been writeen at (date and time): %s", ctime(&t));

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;

OUTPUT:

Studytonight - Best place to learn

This program has been writeen at (date and time): Fri Apr 26 04:47:12 2024

Coding is Fun !

EXPERIMENT NO.46
GREATEST COMMON DIVISOR PROGRAM.

// C program to find GCD of two numbers

#include <math.h>

#include <stdio.h>

// Function to return gcd of a and b

int gcd(int a, int b)


72
PRABHAT CHAUDHARY 11232767 G1
{

// Find Minimum of a and b

int result = ((a < b) ? a : b);

while (result > 0) {

if (a % result == 0 && b % result == 0) {

break;

result--;

// Return gcd of a and b

return result;

// Driver code

int main()

int a = 98, b = 56;

printf("GCD of %d and %d is %d ", a, b, gcd(a, b));

return 0;

// This code is contributed by prabhat chaudhary

OUTPUT:

GCD of 9

8 and 56 is 14
73
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.47
DRAW CIRCLE C GRAPHICS

// C Implementation for drawing circle

#include <graphics.h>

//driver code

int main()

// gm is Graphics mode which is


74
PRABHAT CHAUDHARY 11232767 G1
// a computer display mode that

// generates image using pixels.

// DETECT is a macro defined in

// "graphics.h" header file

int gd = DETECT, gm;

// initgraph initializes the

// graphics system by loading a

// graphics driver from disk

initgraph(&gd, &gm, "");

// circle function

circle(20, 25, 50);

getch();

// closegraph function closes the

// graphics mode and deallocates

// all memory allocated by

// graphics system .

closegraph();

return 0;

OUTPUT:

A module you have imported isn't available at the moment. It will be available soon.
75
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.48
REVERSE AN ARRAY WITHOUT USING AN ADDITIONAL ARRAY

#include <stdio.h>

void reverseArray(int arr[], int n) {

int start = 0;

int end = n - 1;

while (start < end) {

// Swap the elements at start and end

int temp = arr[start];


76
PRABHAT CHAUDHARY 11232767 G1
arr[start] = arr[end];

arr[end] = temp;

// Move the pointers towards the center

start++;

end--;

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

reverseArray(arr, n);

printf("\nReversed array: ");

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

printf("%d ", arr[i]);

return 0;

} #include <stdio.h>

void reverseArray(int arr[], int n) {

int start = 0;

int end = n - 1;

while (start < end) {


77
PRABHAT CHAUDHARY 11232767 G1
// Swap the elements at start and end

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

// Move the pointers towards the center

start++;

end--;

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

reverseArray(arr, n);

printf("\nReversed array: ");

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

printf("%d ", arr[i]);

return 0;

OUTPUT:

Original array: 1 2 3 4 5
78
PRABHAT CHAUDHARY 11232767 G1
Reversed array: 5 4 3 2 1

EXPERIMENT NO.48
IMPLEMENT LINEAR SEARCH ALGORITHM (ARRAY)

#include <stdio.h>

// Function to perform linear search

int linearSearch(int arr[], int n, int key) {

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

if (arr[i] == key) {

return i; // Return the index if the key is found

return -1; // Return -1 if the key is not found


79
PRABHAT CHAUDHARY 11232767 G1
}

int main() {

int arr[] = {12, 45, 67, 89, 34, 56, 78};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 34; // Element to search

int result = linearSearch(arr, n, key);

if (result != -1) {

printf("Element found at index %d\n", result);

} else {

printf("Element not found\n");

return 0;

OUTPUT:

Element found at index 4


80
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.50
IMPLEMENT BINARY SEARCH ALGORITHM (ARRAY)

#include <stdio.h>

// Function to reverse an array in place

void reverseArray(int arr[], int n) {

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

// Swap arr[i] with arr[n - i - 1]

int temp = arr[i];

arr[i] = arr[n - i - 1];

arr[n - i - 1] = temp;

}
81
PRABHAT CHAUDHARY 11232767 G1
int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

printf("\n");

// Reverse the array

reverseArray(arr, n);

printf("Reversed array: ");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:

Original array: 1 2 3 4 5

Reversed array: 5 4 3 2 1
82
PRABHAT CHAUDHARY 11232767 G1

EXPERIMENT NO.51
WRITE A PROGRAM TO IMPLEMENT BUBBLE SORT ALGORITHM

// C program for implementation of Bubble sort

#include <stdio.h>

// Swap function

void swap(int* arr, int i, int j)

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

// A function to implement bubble sort

void bubbleSort(int arr[], int n)


83
PRABHAT CHAUDHARY 11232767 G1
{

int i, j;

for (i = 0; i < n - 1; i++)

// Last i elements are already

// in place

for (j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

swap(arr, j, j + 1);

// Function to print an array

void printArray(int arr[], int size)

int i;

for (i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

// Driver code

int main()

int arr[] = { 5, 1, 4, 2, 8 };

int N = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, N);

printf("Sorted array: ");

printArray(arr, N);
84
PRABHAT CHAUDHARY 11232767 G1
return 0;

OUTPUT:

Sorted array: 1 2 4 5 8

EXPERIMENT NO.52
WRITE A PROGRAM TO IMPLEMENT INSERTION SORT ALGORITHM

#include <stdio.h>

void insertionSort(int arr[], int n) {

int i, key, j;

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

key = arr[i];

j = i - 1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;
85
PRABHAT CHAUDHARY 11232767 G1
}

arr[j + 1] = key;

void printArray(int arr[], int n) {

int i;

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

printf("%d ", arr[i]);

printf("\n");

int main()

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: \n");

printArray(arr, n);

insertionSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

OUTPUT:

Original array:

12 11 13 5 6

Sorted array:
86
PRABHAT CHAUDHARY 11232767 G1
5 6 11 12 13

EXPERIMENT NO.53
WRITE A PROGRAM TO IMPLEMENT SELECTION SORT ALGORITHM

// C program for implementation of selection sort

#include <stdio.h>

void swap(int* xp, int* yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

void selectionSort(int arr[], int n)

int i, j, min_idx;

// One by one move boundary of unsorted subarray

for (i = 0; i < n - 1; i++) {


87
PRABHAT CHAUDHARY 11232767 G1
// Find the minimum element in unsorted array

min_idx = i;

for (j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

// Swap the found minimum element with the first

// element

swap(&arr[min_idx], &arr[i]);

/* Function to print an array */

void printArray(int arr[], int size)

int i;

for (i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

// Driver program to test above functions

int main()

int arr[] = { 64, 25, 12, 22, 11 };

int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, n);

printf("Sorted array: \n");


88
PRABHAT CHAUDHARY 11232767 G1
printArray(arr, n);

return 0;

OUTPUT:

Sorted array:

11 12 22 25 64

EXPERIMENT NO.54
GIVEN AN ARRAY ARR[] OF SIZE N-1 WITH INTEGERS IN THE RANGE OF [1, N], THE
TASK IS TO FIND THE MISSING NUMBER FROM THE FIRST N INTEGERS.

#include <stdio.h>

void findMissing(int arr[], int N)

int temp[N + 1];

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

temp[i] = 0;}

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

temp[arr[i] - 1] = 1;}

int ans;

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

if (temp[i] == 0)

ans = i + 1;}

printf("%d", ans);
89
PRABHAT CHAUDHARY 11232767 G1
}

/* Driver code */

int main()

int arr[] = { 1, 3, 7, 5, 6, 2 };

int n = sizeof(arr) / sizeof(arr[0]);

findMissing(arr, n);}

OUTPUT:

EXPERIMENT NO.55
PROGRAM TO CYCLICALLY ROTATE AN ARRAY BY ONE

// C code for program

// to cyclically rotate

// an array by one

#include <stdio.h>

void rotate(int arr[], int n)

// store the last element in a variable

int last_el = arr[n - 1];

for (int i = n - 1; i > 0; i--)

arr[i] = arr[i - 1];

// assign the last element to first element

arr[0] = last_el;

int main()
90
PRABHAT CHAUDHARY 11232767 G1
{

int arr[] = { 1, 2, 3, 4, 5 }, i;

int n = sizeof(arr) / sizeof(arr[0]);

printf("Given array is\n");

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

printf("%d ", arr[i]);

// Function Call

rotate(arr, n);

printf("\nRotated array is\n");

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

printf("%d ", arr[i]);

return 0;

OUTPUT:

Given array is

12345

Rotated array is

51234
91
PRABHAT CHAUDHARY 11232767 G1

You might also like