0% found this document useful (0 votes)
8 views43 pages

Assignment

Assigment for comupter science engineering

Uploaded by

strixbgmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views43 pages

Assignment

Assigment for comupter science engineering

Uploaded by

strixbgmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

A

Laboratory Record File


On

COMPUTER SYSTEM PROGRAMMING


(IT -214 )
Submi ed By

tt
Bikram Sarma. Roll No.22RISTCS020
M ROHAN

The Partial Fulfilment of 2nd Semester Examination


(Session: April-August 2023)

Department of Computer Science & Engineering

Regional Institute of Science & Technology


9th Mile Meghalaya

AFFILIATED TO

North Eastern Hill University


Shillong, Meghalaya
CONTENT:-

• Hello World

• ArithmeNc OperaNons

• Area of Shapes(Circle ,Square ,Rectangle ,Triangle)

• Simple Interest & Total Amount

• Roots of QuadriNc EquaNon

• Swapping

• Greatest Between Three Numbers

• To Check wether the entered le3er is vowel or consonant

• Conversion of Temperature

• To check given year is leap year or not

• Marksheet of A Student

• To Check Prime Number

• Prime Numbers from 1 to 100 and also sum


• To Check Palindrome or Not

• FACTORIAL of a number

• Switch Case

• TERNARY OPERATOR

• TRANSPOSE of a Matrix

• MATRIX AddiNon

• MulNplicaNon of MATRIX:

• Factorial of a number using recursive funcNon

• Fibonacci series using Recursive FuncNon

• To print PYRAMID STAR pa3ern

• To print Star Pa3ern:

• Swapping Using Pointers:

• Using pointers compute sum of elements of the Array

• Reverse of a String

• ConcatenaNon of a String
• Details of Employee using Structure and FuncNon.

• In structure funcNon access members using pointer

• Program to create a FILE write in it and close the FILE.

• AllocaNon of a Memory then Reallocate it and then free the Memory

• Program to use Malloc funcNon

#1 Hello World

#include <stdio.h>
void main()
{
printf("Hello World\n");
printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n
}
");

Output:

Hello World
M Rohan
Roll No.- 22RISTCS020
.

#2 Arithmetic Operations
#include <stdio.h>

void main()

int a = 10, b = 4, res;

printf("a is %d and b is %d\n", a, b);

res = a + b;

printf("a + b is %d\n", res);

res = a - b;

printf("a - b is %d\n", res);

res = a * b;

printf("a * b is %d\n", res);

res = a / b;

printf("a / b is %d\n", res);

res = a % b;

printf("a %% b is %d\n", res);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Out Put:

a is 10 and b is 4

a + b is 14

a - b is 6

a * b is 40

a / b is 2

a % b is 2

M Rohan

Roll No.- 22RISTCS020.

#3 Area of Shapes (Circle, Square, Rectangle, Triangle)

#include <stdio.h>
void main()

int l, h;

printf("Enter Length or radius of the shape: ");

scanf("%d", &l);

printf("Enter Height of the shape: ");

scanf("%d", &h);

printf("\nArea of Square of length %d cm = %d sq.cm.\n", l, l * l);

printf("\nArea of Rectangle of length %d cm and breadth %d cm = %d sq.cm.\n", l,


h, l * h);

printf("\nArea of Triangle of base %d cm and Height %d cm = %f sq.cm.\n", l, h, 0.5


* l * h);

printf("\nArea of Circle of radius %d cm = %f sq.cm.\n", l, 3.14 * l * l);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n");

Output:

Enter Length or radius of the shape: 5

Enter Height of the shape: 3

Area of Square of length 5 cm = 25 sq.cm.

Area of Rectangle of length 5 cm and breadth 3 cm = 15 sq.cm.

Area of Triangle of base 5 cm and Height 3 cm = 7.500000 sq.cm.

Area of Circle of radius 5 cm = 78.500000 sq.cm.

M Rohan

Roll No.- 22RISTCS020


.
#4 Simple Interest & Total Amount

#include <stdio.h>

void main()

float principle, rate, time, interest;

printf("Enter the principal: ");

scanf("%f", &principle);

printf("Enter the rate: ");

scanf("%f", &rate);

printf("Enter the time: ");

scanf("%f", &time);

interest = principle * rate * time / 100;

printf("The Simple interest is %f\n", interest);

printf("The Total Amount is %f\n", interest + principle);

printf ("\n\nM Rohan \nRoll No.- 22RISTCS 020.\n ");

Enter the principal: 1000

Enter the rate: 5

Enter the time: 2

The Simple interest is 100.000000

The Total Amount is 1100.000000

M Rohan

Roll No.- 22RISTCS020.

#5 Roots of Quadratic Equation


#include <stdio.h>

#include <math.h>

void main()

float a, b, c;

printf("Enter The Quadratic Equation Coefficients.\n");

printf("Coefficient of x sq. = ");

scanf("%f", &a);

printf("Coefficient of x = ");

scanf("%f", &b);

printf("Constant = ");

scanf("%f", &c);

float root1 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

float root2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);

printf("Roots of Quadratic Equation are: %f and %f.\n", root1, root2);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n");

Output:

Enter The Quadratic Equation Coefficients.

Coefficient of x sq. = 1

Coefficient of x = -3

Constant = -4

Roots of Quadratic Equation are: -1.000000 and 4.000000.


M Rohan

Roll No.- 22RISTCS020


. #6 Swapping
#include <stdio.h>

void main()

int x, y, t;

printf("Enter two integers\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

t = x;

x = y;

y = t;

printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n");

Output:

Enter two integers

5 10

Before Swapping

First integer = 5

Second integer = 10

After Swapping

First integer = 10

Second integer = 5

M Rohan

Roll No.- 22RISTCS020.

#7 Greatest Between Three Numbers

#include <stdio.h>

void main()
{

double n1, n2, n3;

printf("Enter three different numbers: ");

scanf("%lf %lf %lf", &n1, &n2, &n3);

if (n1 >= n2 && n1 >= n3)

printf("%.2f is the largest number.\n", n1);

if (n2 >= n1 && n2 >= n3)

printf("%.2f is the largest number.\n", n2);

if (n3 >= n1 && n3 >= n2)

printf("%.2f is the largest number.\n", n3);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter three different numbers: 15 30 10

30.00 is the largest number.

M Rohan

Roll No.- 22RISTCS020.

#8 To Check whether the entered letter is a vowel or consonant

#include <stdio.h>

int main()

char c;

int lowercase_vowel, uppercase_vowel;

printf("Enter an alphabet: ");

scanf(" %c", &c);


// evaluates

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (lowercase_vowel || uppercase_vowel)

printf("%c is a vowel.\n", c);

else

printf("%c is a consonant.\n", c);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter an alphabet: A
A is a vowel.

M Rohan
Roll No.- 22RISTCS020.

#9 Conversion of Temperature

#include <stdio.h>

void main()

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9 / 5) + 32;

printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

printf ("\n\nM Rohan \nRoll No.- 22RISTCS 020.\n "); }

Output:

Enter temperature in Celsius: 30


Temperature in Fahrenheit: 86.00

M Rohan

Roll No.- 22RISTCS020.

#10 To check whether the given year is a leap year or not

#include <stdio.h>

void main()

int year;

printf("Enter the year: ");

scanf("%d", &year);

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

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

else

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

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the year: 2024

2024 is a leap year.

M Rohan

Roll No.- 22RISTCS020. #

11 Marksheet of a Student

#include <stdio.h>

void main()

{
char name[30];

int roll;

float math, science, english, total, average;

printf("Enter Name: ");

scanf("%s", name);

printf("Enter Roll No.: ");

scanf("%d", &roll);

printf("Enter marks in Math: ");

scanf("%f", &math);

printf("Enter marks in Science: ");

scanf("%f", &science);

printf("Enter marks in English: ");

scanf("%f", &english);

total = math + science + english;

average = total / 3;

printf("\n----- Marksheet -----\n");

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

printf("Roll No.: %d\n", roll);

printf("Marks in Math: %.2f\n", math);

printf("Marks in Science: %.2f\n", science);

printf("Marks in English: %.2f\n", english);

printf("Total Marks: %.2f\n", total);


printf("Average Marks: %.2f\n", average);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter Name: John

Enter Roll No.: 101

Enter marks in Math: 85

Enter marks in Science: 90

Enter marks in English: 78

----- Marksheet -----

Name: John

Roll No.: 101

Marks in Math: 85.00

Marks in Science: 90.00

Marks in English: 78.00

Total Marks: 253.00

Average Marks: 84.33

M Rohan

Roll No.- 22RISTCS020.

#12 To Check Prime Number

#include <stdio.h>

int main()

int num, i, isPrime = 1;

printf("Enter a positive integer: ");

scanf("%d", &num);
if (num == 0 || num == 1)

isPrime = 0;

else

for (i = 2; i <= num / 2; ++i)

if (num % i == 0)

isPrime = 0;

break;

if (isPrime)

printf("%d is a prime number.\n", num);

else

printf("%d is not a prime number.\n", num);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Enter a positive integer: 17

17 is a prime number.

M Rohan

Roll No.- 22RISTCS020.


#13 Prime Numbers from 1 to 100 and their Sum

#include <stdio.h>

int main()

int i, j, isPrime, sum = 0;

printf("Prime Numbers between 1 to 100: ");

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

isPrime = 1;

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

if (i % j == 0)

isPrime = 0;

break;

if (isPrime)

printf("%d ", i);

sum += i;

printf("\nSum of Prime Numbers between 1 to 100: %d\n", sum);


printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Prime Numbers between 1 to 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61


67 71 73 79 83 89 97

Sum of Prime Numbers between 1 to 100: 1060


M Rohan

Roll No.- 22RISTCS020.

#14 To Check Palindrome or Not

#include <stdio.h>

#include <string.h>

int main()

char str[100];

int i, j, len, isPalindrome = 1;

printf("Enter a string: ");

scanf("%s", str);

len = strlen(str);

for (i = 0, j = len - 1; i < len / 2; i++, j--)

if (str[i] != str[j])

isPalindrome = 0;

break;

}
}

if (isPalindrome)

printf("%s is a palindrome.\n", str);

else

printf("%s is not a palindrome.\n", str);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter a string: level

level is a palindrome.

M Rohan

Roll No.- 22RISTCS020. #

15 Factorial of a Number

#include <stdio.h>

long factorial(int num)

if (num == 0)

return 1;

else

return num * factorial(num - 1);

void main()

int num;
printf("Enter a positive integer: ");

scanf("%d", &num);

printf("Factorial of %d is %ld.\n", num, factorial(num));

printf ("\n\nM Rohan \nRoll No.- 22RISTCS 020.\n "); }

Output:

Enter a positive integer: 5

Factorial of 5 is 120.

M Rohan

Roll No.- 22RISTCS020

. #16 Switch Case

#include <stdio.h>

void main()

int choice;

printf("Enter a number (1-5): ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("You chose number 1.\n");

break;

case 2:

printf("You chose number 2.\n");

break;

case 3:

printf("You chose number 3.\n");


break;

case 4:

printf("You chose number 4.\n");

break;

case 5:

printf("You chose number 5.\n");

break;

default:

printf("Invalid choice.\n");

break;

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter a number (1-5): 3

You chose number 3.

M Rohan

Roll No.- 22RISTCS020

. #17 Ternary Operator

#include <stdio.h>

void main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

(num % 2 == 0) ? printf("%d is even.\n", num) : printf("%d is odd.\n", num);


printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter a number: 7

7 is odd.

M Rohan

Roll No.- 22RISTCS020. #

18 Transpose of a Matrix

#include <stdio.h>

#define ROWS 3

#define COLS 3

void transpose(int mat[ROWS][COLS])

int temp;

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

for (int j = i + 1; j < COLS; j++)

temp = mat[i][j];

mat[i][j] = mat[j][i];

mat[j][i] = temp;

}
void printMatrix(int mat[ROWS][COLS])

printf("Transpose of the matrix:\n");

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

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

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

printf("\n");

void main()

int matrix[ROWS][COLS];

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

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

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

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

transpose(matrix);

printMatrix(matrix);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }


Output:

Enter the elements of the matrix (3 x 3):

123

456

789

Transpose of the matrix:

147

258

369

M Rohan

Roll No.- 22RISTCS020

. #19 Matrix Addition

#include <stdio.h>

#define ROWS 3

#define COLS 3

void matrixAddition(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int


result[ROWS][COLS])

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

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

result[i][j] = mat1[i][j] + mat2[i][j];

}
void printMatrix(int mat[ROWS][COLS])

printf("Resultant matrix:\n");

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

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

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

printf("\n");

void main()

int matrix1[ROWS][COLS], matrix2[ROWS][COLS], result[ROWS][COLS];

printf("Enter the elements of matrix1 (%d x %d):\n", ROWS, COLS);

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

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

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

printf("Enter the elements of matrix2 (%d x %d):\n", ROWS, COLS);

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


{

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

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

matrixAddition(matrix1, matrix2, result);

printMatrix(result);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the elements of matrix1 (3 x 3):

123

456

789

Enter the elements of matrix2 (3 x 3):

987

654

321

Resultant matrix:

10 10 10

10 10 10

10 10 10 M
Rohan

Roll No.- 22RISTCS020. #

20 Multiplication of Matrix
#include <stdio.h>

#define ROWS 3

#define COLS 3

void matrixMultiplication(int mat1[ROWS][COLS], int mat2[ROWS][COLS], int


result[ROWS][COLS])

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

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

result[i][j] = 0;

for (int k = 0; k < COLS; k++)

result[i][j] += mat1[i][k] * mat2[k][j];

void printMatrix(int mat[ROWS][COLS])

printf("Resultant matrix:\n");

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

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

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

}
printf("\n");

void main()

int matrix1[ROWS][COLS], matrix2[ROWS][COLS], result[ROWS][COLS];

printf("Enter the elements of matrix1 (%d x %d):\n", ROWS, COLS);

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

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

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

printf("Enter the elements of matrix2 (%d x %d):\n", ROWS, COLS);

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

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

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

matrixMultiplication(matrix1, matrix2, result);

printMatrix(result);
printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the elements of matrix1 (3 x 3):

123

456

789

Enter the elements of matrix2 (3 x 3):

987

654

321

Resultant matrix:

30 24 18

84 69 54

138 114 90

M Rohan

Roll No.- 22RISTCS020.

#21 Factorial of a Number using Recursive Function

#include <stdio.h>

long factorial(int num)

if (num == 0)

return 1;

else

return num * factorial(num - 1);

}
void main()

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

printf("Factorial of %d is %ld.\n", num, factorial(num));

printf ("\n\nM Rohan \nRoll No.- 22RISTCS 020.\n "); }

Output:

Enter a positive integer: 5

Factorial of 5 is 120.

M Rohan

Roll No.- 22RISTCS020.

#22 Fibonacci Series using Recursive Function

#include <stdio.h>

int fibonacci(int n)

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

return fibonacci(n - 1) + fibonacci(n - 2);

void main()

int num;
printf("Enter the number of terms in the Fibonacci series: ");

scanf("%d", &num);

printf("Fibonacci series: ");

for (int i = 0; i < num; i++)

printf("%d ", fibonacci(i));

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the number of terms in the Fibonacci series: 8

Fibonacci series: 0 1 1 2 3 5 8 13

M Rohan

Roll No.- 22RISTCS020.

#23 To Print Pyramid Star Pattern

#include <stdio.h>

void printPyramid(int rows)

int space, star;

for (int i = 1; i <= rows; i++)

// Print spaces

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

printf(" ");

// Print stars

for (star = 1; star <= (2 * i - 1); star++)

printf("*");
printf("\n");

void main()

int rows;

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

scanf("%d", &rows);

printPyramid(rows);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the number of rows for the pyramid: 5

***

*****

*******

********* M
Rohan

Roll No .- 22RISTCS020.

# 24 To Print Star

Pattern #include <stdio .

void printStarPattern(int rows)


h>

for (int i = 1; i <= rows; i++)

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


{

printf("*");

printf("\n");

void main()

int rows;

printf("Enter the number of rows for the star pattern: ");

scanf("%d", &rows);

printStarPattern(rows);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter the number of rows for the star pattern: 5

**

***

****

*****

M Rohan

Roll No.- 22RISTCS020.

#25 Swapping Using Pointers

#include <stdio.h>

void swap(int *a, int *b)

{
int temp = *a;

*a = *b;

*b = temp;

void main()

int x, y;

printf("Enter two integers\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n");

Output:

Enter two integers

5 10

Before Swapping

First integer = 5

Second integer = 10

After Swapping

First integer = 10

Second integer = 5

M Rohan

Roll No.- 22RISTCS020.

#26 Using Pointers Compute Sum of Elements of the Array


#include <stdio.h>

void main()

int arr[5] = {10, 20, 30, 40, 50};

int *ptr = arr;

int sum = 0;

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

sum += *ptr;

ptr++;

printf("Sum of elements of the array = %d\n", sum);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Sum of elements of the array = 150


M Rohan

Roll No.- 22RISTCS020

. # 27 Reverse of a

String

#include <stdio.h>

#include <string.h>

void main()

char str[100];

printf("Enter a string: ");

scanf("%s", str);
printf("Reverse of the string: ");

for (int i = strlen(str) - 1; i >= 0; i--)

printf("%c", str[i]);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter a string: Hello

Reverse of the string: olleH

M Rohan

Roll No.- 22RISTCS020.

#28 Concatenation of a String

#include <stdio.h>

#include <string.h>

void main()

char str1[100], str2[100];

printf("Enter the first string: ");

scanf("%s", str1);

printf("Enter the second string: ");

scanf("%s", str2);

strcat(str1, str2);

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

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }


Output:

Enter the first string: Hello

Enter the second string: World

Concatenated string: HelloWorld

M Rohan

Roll No.- 22RISTCS020.

#29 Details of Employee using Structure and Function

#include <stdio.h>

#include <string.h>

struct Employee

char name[50];

int age;

float salary;

};

void displayEmployee(struct Employee emp)

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

printf("Employee Age: %d\n", emp.age);

printf("Employee Salary: %.2f\n", emp.salary);

void main()

struct Employee emp1;


printf("Enter Employee Name: ");

scanf("%s", emp1.name);

printf("Enter Employee Age: ");

scanf("%d", &emp1.age);

printf("Enter Employee Salary: ");

scanf("%f", &emp1.salary);

displayEmployee(emp1);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter Employee Name: John

Enter Employee Age: 30

Enter Employee Salary: 45000

Employee Name: John

Employee Age: 30

Employee Salary: 45000.00

M Rohan

Roll No.- 22RISTCS020.

#30 In Structure Function, Access Members Using Pointers

#include <stdio.h>

#include <string.h>

struct Employee

char name[50];
int age;

float salary;

};

void displayEmployee(struct Employee *emp)

printf("Employee Name: %s\n", emp->name);

printf("Employee Age: %d\n", emp->age);

printf("Employee Salary: %.2f\n", emp->salary);

void main()

struct Employee emp1;

printf("Enter Employee Name: ");

scanf("%s", emp1.name);

printf("Enter Employee Age: ");

scanf("%d", &emp1.age);

printf("Enter Employee Salary: ");

scanf("%f", &emp1.salary);

displayEmployee(&emp1);

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter Employee Name: John

Enter Employee Age: 30


Enter Employee Salary: 45000

Employee Name: John

Employee Age: 30

Employee Salary: 45000.00

M Rohan

Roll No.- 22RISTCS020.

#31 Program to Create a File, Write in it, and Close the File

#include <stdio.h>

void main()

FILE *filePointer;

char data[100];

filePointer = fopen("sample.txt", "w");

if (filePointer == NULL)

printf("File creation failed.\n");

return;

printf("Enter data to write into the file: ");

scanf("%s", data);

fprintf(filePointer, "%s", data);

fclose(filePointer);
printf("Data written to the file successfully.\n");

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter data to write into the file: Hello, this is a sample text.

Data written to the file successfully.

M Rohan

Roll No.- 22RISTCS020.

#32 Allocation of a Memory then Reallocate it and then Free the Memory

#include <stdio.h>

#include <stdlib.h>

void main()

int *ptr;

// Allocate memory for 5 integers

ptr = (int *)malloc(5 * sizeof(int));

if (ptr == NULL)

printf("Memory allocation failed.\n");

return;

printf("Enter 5 integers:\n");
for (int i = 0; i < 5; i++)

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

printf("Entered integers: ");

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

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

printf("\n");

// Reallocate memory for 10 integers

ptr = (int *)realloc(ptr, 10 * sizeof(int));

if (ptr == NULL)

printf("Memory reallocation failed.\n");

return;

printf("Enter 5 more integers:\n");

for (int i = 5; i < 10; i++)

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

printf("All 10 integers: ");

for (int i = 0; i < 10; i++)


{

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

printf("\n");

// Free the allocated memory

free(ptr);

printf("Memory freed successfully.\n");

printf("\n\nM Rohan\nRoll No.- 22RISTCS020.\n "); }

Output:

Enter 5 integers:

12345

Entered integers: 1 2 3 4 5

Enter 5 more integers:

6 7 8 9 10

All 10 integers: 1 2 3 4 5 6 7 8 9 10

Memory freed successfully.

M Rohan

Roll No.- 22RISTCS020.


THANK YOU

You might also like