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

c lang

This document is an assignment on 'C Language' submitted by Devesh Chopkar for the Bachelor of Computer Applications program at Pt. Ravishankar Shukla University. It includes a certificate of approval, evaluation, and a declaration of originality, along with a comprehensive index of programming tasks and their respective code implementations. The assignment covers various programming concepts such as variable swapping, control statements, and mathematical calculations.

Uploaded by

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

c lang

This document is an assignment on 'C Language' submitted by Devesh Chopkar for the Bachelor of Computer Applications program at Pt. Ravishankar Shukla University. It includes a certificate of approval, evaluation, and a declaration of originality, along with a comprehensive index of programming tasks and their respective code implementations. The assignment covers various programming concepts such as variable swapping, control statements, and mathematical calculations.

Uploaded by

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

AN

ASSIGNMENT
ON
PC-Software and Multimedia
BACHELOR OF COMPUTER APPLICATIONS
FROM
Pt. Ravishankar Shukla University Raipur (C.G)
Year: 2023-2024

Guided by : Submitted by :
Mr. Mannu Rawani Devesh Chopkar
(HOD computer science) (Class-BCA1)
SUBMITTED TO
Pragati College Raipur (C.G)
Pt. Ravishankar Shukla University Raipur (C.G)
CERTIFICATE OF APPROVAL

This is to certify that the assignment work on “C Language ”is


carried out by Devesh Chopkar a student of BCA-I at
PRAGATI COLLEGE is hereby approved as a credible work
in the discipline of “C Language ” for the award of degree of
BCA– I Year during the year 2023-24 from Pt. Ravishankar
Shukla University, Raipur (CG).

Mr. Mannu Rawani

HOD Computer Science

Pragati College, Raipur (C.G.)

2|Page
CERTIFICATE OF EVALUATION

This is to certify that the Assignment work entitled as “C


Language ” is carried out by Devesh Chopkar a student of
BCA– I at Pragati College, after proper evaluation and
examination, is here by approved as a credible work in the
discipline of “C Language ” and is done in a satisfactory
manner for its acceptance as a requisite for the award of degree
of “BCA-I Year "during the year 2023-24 from Pt.
Ravishankar Shukla University, Raipur (CG).

Internal Examiner External Examiner

3|Page
DECLARATION

This is to certify that the assignment work entitled “C Language


”which is submitted by me in the fulfilment for the award of the
BCA– I Year, Pragati College, comprises the original work
carried out by me. I further declare that the work reported in
this process in this assignment has not been submitted and will
not be submitted, either in part of in full for the award of any
other degree or diploma in this institute or any other institute
or University.

Place – Raipur Student Name : Devesh Chopkar

Date – 15/2/2024 Roll No. :

4|Page
ACKNOWLEDEMENT

No work can be completed successfully without the help,


inspiration and encouragement by the elders and seniors. So, my
heartily thanks to all of them who helped me while preparing
this assignment. I would begin by thanking to my parents who
provided me the required support for completing this job.

I acknowledge and thank Mr. Mannu Rawani our


“Assignment In charge” and classmates who guided me to
complete this assignment.

5|Page
INDEX
S.NO. Program Page
no.
1. Write a program to swap the value of two variables with the help 8
of a third variable.
2. Write a program to swap the value of two variables without 10
using a third variable.
3. Write a program to create equivalent of a four function 12
calculator by using switch statement.
4. Write a program which illustrates the switch statement. 14
5. Write a program to find greatest among three variables. 16
6. Write a program to find greatest among three variables using 18
nested if.
7. Write a program to check whether the given number is even or 20
odd.
8. Write a program to find that the entered year is leap year or not. 21
9. Write a program for estimating simple interest. 23
10. Write a program for estimating compound interest. 25
11. Write a program to check that the given character is vowel or 27
not.
12. Write a program to generate Fibonacci series upto given terms. 28
13. Write a program to find the factorial of a given number. 29
14. Write the program to check given no. is armstrong or not. 30
15. Write a program to count the length of string without using 31
library function.
16. Write a program to find the average of arrays elements. 32
17. Write a program to find the greatest and smallest elements in 34
the one dimensional array.
18. Write a program to access the elements of two dimensional 36
array.
19. Write a program for addition of two matrices. 38
20. Write a program of multiplication of two matrices. 41
21. Write a program of linear search. 44
22. Write a program of bubble sort. 46
23. Write a program to illustrate the use of strlen() function. 48

6|Page
24. Write a program to illustrate the use of strcpy() function. 49
25. Write a program to illustrate the use of strcmp() function. 50
26. Write a program to illustrate the use of strcat() function. 51
27. Write a program to illustrate the use of strrev() function. 52
28. Write a program that calls a function by value. 53
29. Write a program that calls a function by reference 54
30. Write a program to illustrate the use of a structure pointer. 55
31. Write a program to access the element of array using pointer. 56
32. Write a program for declaration and use of pointers to pointers. 57
33. Write a program to illustrate the use of a pointer and string. 58
34. Write a program to illustrate the use of a pointer and function. 59
35. Write a program for no arguments and no return values. 60
36. Write a program for arguments and no return values. 61
37. Write a program for arguments and return values. 63
38. Write a program to demonstrate the use of union. 64
39. Write a program to illustrate the use of structure. 66
40. Write a program to passing structures variable. 68
41. Write a program to returning structures variable. 70
42. Write a program to assign the values a of a structures to another 72
structure.
43. Write a program to compare the values of structure variables. 73
44. Write a program to find the total memory occupied by structure 74
and union variables.
45. Write a program for nested structure. 75
46. Write a program to store records of five students in an array of 77
structures.
47. Write a program to read the content from a file. 79
48. Write a program to copy the content from a file to another file. 80
49. Write a program to write the content to a fille. 81
50. Write a program to use the dynamic memory allocation function. 82

7|Page
Q1. Write a program to swap the value of two variable with the help
of third variable.

Code of Program :
#include<stdio.h>

#include<conio.h>

void main ()

int a,b,c;

clrscr();

printf("\n enter the value of a : ");

scanf("%d", &a);

printf("\n enter the value of b : ");

scanf("%d", &b);

c = a;

a = b;

b = c;

printf("After swapping \n the value of a is %d", a);

printf("\n the value of b is %d", b);

getch();

8|Page
Output of program :

9|Page
Q2. Write a program to swap the value of two variable without the
help of third variable.

Code of program :
#include<stdio.h>

#include<conio.h>

void main()

int a,b;

clrscr();

printf("\n enter the value of a : ");

scanf("%d", &a);

printf("\n enter the value of b : ");

scanf("%d", &b);

printf("before swap a=%d b=%d",a,b);

a=a+b;

b=a-b;

a=a-b;

printf("\nafter swap a=%d b=%d",a,b);

getch();

10 | P a g e
Output :

11 | P a g e
Q3. Write a program to create equivalent of a four function calculator
by using switch statement.

Code of program :

#include <stdio.h>

#include <conio.h>

void main() {

char op;

double first, second;

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

scanf("%c", &op);

printf("Enter two operands: ");

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);

12 | P a g e
break;

case '*':

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

break;

case '/':

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

break;

default:

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

getch();

Output :

13 | P a g e
Q4. Write a program which illustrates the switch statement.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main()

char lang ;

printf("\n What is your preferred language English or French ? ");

scanf("%s",&lang);

switch(lang) {

case 'e':

printf("\n Hello ");

break;

case 'f':

printf("\n Bonjour");

break;

default:

printf("Invalid input");

break;

getch();

14 | P a g e
Output :

Q5. Write a program to find Greatest among 3 variables.

15 | P a g e
Code of Program :
#include <stdio.h>

#include <conio.h>

void main()

int a, b, c,flag=1;

printf("Enter three numbers: \na: ");

scanf("%d", &a);

printf("b: ");

scanf("%d", &b);

printf("c: ");

scanf("%d", &c);

if (a > b && a > c)

printf("Biggest number is %d", a);

flag=0;

if (b > a && b > c)

printf("Biggest number is %d", b);

flag=0;

if (c > a && c > b)

printf("Biggest number is %d", c);

flag=0;

if (flag==1)

printf("Invalid input ");

16 | P a g e
getch();

Output :

Q6. Write a program to find Greatest among 3 variables using nested


if.

17 | P a g e
Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

float n1, n2, n3;

printf("Enter first number: ");

scanf("%f", &n1);

printf("Enter second number: ");

scanf("%f", &n2);

printf("Enter third number: ");

scanf("%f", &n3);

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

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

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

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

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

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

18 | P a g e
getch();

Output :

Q7. Write a program to check whether the given number is even or


odd.

19 | P a g e
Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

getch();

Output :

Q8.Write a program to find that entered year is leap year or not.

Code of Program :
20 | P a g e
#include <stdio.h>

#include <conio.h>

void main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 400 == 0) {

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

else if (year % 100 == 0) {

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

else if (year % 4 == 0) {

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

else {

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

getch();

Output :

21 | P a g e
22 | P a g e
Q9. Write a program for estimating simple interest.

Code of Program :
# include <conio.h>

# include <stdio.h>

# include <conio.h>

void main(){

float principal, rate, time, interest;

printf("Enter the principal: ");

scanf("%f", &principal);

printf("Enter the rate: ");

scanf("%f", &rate);

printf("Enter the time in year : ");

scanf("%f", &time);

interest = principal * rate * time / 100;

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

getch();

23 | P a g e
Output :

24 | P a g e
Q10. Write a program for estimating compound interest.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

float principle, rate, time, CI;

printf("Enter principle (amount): ");

scanf("%f", &principle);

printf("Enter time: ");

scanf("%f", &time);

printf("Enter rate: ");

scanf("%f", &rate);

CI = principle* (pow((1 + rate / 100), time));

printf("Compound Interest = %f", CI);

25 | P a g e
getch();

Output :

26 | P a g e
Q11. Write a program to check that given character is Vowel or not.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

char c;

int lowercase_vowel, uppercase_vowel;

printf("Enter an alphabet: ");

scanf("%c", &c);

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.", c);

else

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

getch();

Output :

27 | P a g e
Q12. Write a program to generate fibonacci series upto given terms.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main()

int a=0, b=1, n, sum, i;

printf("Enter the number of terms");

scanf("%d",&n);

printf("fibbonacci series ");

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

{sum=a+b;

printf("\t%d",sum);

a=b;

b=sum;}

getch();

Output :

28 | P a g e
Q13. Write a program to find the factorial of a given number.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

int i, fact=1,n;

printf("Enter an integer: ");

scanf("%d", &n);

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

{ fact= i*fact ;

printf("Factorial of the given number is %d",fact);

getch();

Output :

29 | P a g e
Q14.Write a Program to check the given no. is Armstrong or not.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

int num, originalNum, remainder, result = 0;

printf("Enter a three-digit integer: ");

scanf("%d", &num);

originalNum = num;

while (originalNum != 0) {

remainder = originalNum % 10;

result += remainder * remainder * remainder;

originalNum /= 10;

if (result == num)

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

else

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

getch();

30 | P a g e
Q15. Write a program to calculate to count the length of a string
without using library function.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main()

char str[100];

int i,length=0;

printf("Enter a string: \n");

scanf("%s",str);

for(i=0; str[i]!='\0'; i++)

length++;

printf("\nLength of input string: %d",length);

getch();

31 | P a g e
Q16. Write a program to find the average of arrays elements.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main() {

int n, i;

float num[100], sum = 0.0, avg;

printf("Enter the numbers of elements: ");

scanf("%d", &n);

while (n > 100 || n < 1) {

printf("Error! number should in range of (1 to 100).\n");

printf("Enter the number again: ");

scanf("%d", &n);

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

printf("%d. Enter number: ", i + 1);

scanf("%f", &num[i]);

sum += num[i];

avg = sum / n;

32 | P a g e
printf("Average = %.2f", avg);

getch();

Output :

33 | P a g e
Q17. Write a program to find the largest and smallest elements in the
one dimensional array.

Code of Program :
#include<stdio.h>

#include<conio.h>

void main()

int a[50],i,n,large,small;

printf("\nEnter the number of elements : ");

scanf("%d", &n);

printf("\nInput the array elements :" );

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

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

large=small=a[0];

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

if(a[i]>large)

large=a[i];

if(a[i]<small)

small=a[i];

34 | P a g e
printf("\nThe smallest element is %d\n",small);

printf("\nThe largest element is %d\n",large);

getch();

Output :

35 | P a g e
Q18. Write a program to access the elements of two dimensional
array.

Code of Program :
#include<stdio.h>

#include <conio.h>

void main(){

int i=0,j=0;

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

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

for(j=0;j<3;j++){

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

} }

getch();

Output :

36 | P a g e
37 | P a g e
Q19. Write a program for addition of two marices.

Code of Program :
#include <stdio.h>

#include <conio.h>

int main() {

int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter the number of rows (between 1 and 100): ");

scanf("%d", &r);

printf("Enter the number of columns (between 1 and 100): ");

scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");

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

for (j = 0; j < c; ++j) {

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

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

printf("Enter elements of 2nd matrix:\n");

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

for (j = 0; j < c; ++j) {

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

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

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

38 | P a g e
for (j = 0; j < c; ++j) {

sum[i][j] = a[i][j] + b[i][j];

printf("\nSum of two matrices: \n");

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

for (j = 0; j < c; ++j) {

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

if (j == c - 1) {

printf("\n\n");

getch();

39 | P a g e
Program :

40 | P a g e
Q20. Write a program for multiplication of two marices.

Code of Program :
#include<stdio.h>

#include <conio.h>

#include<stdlib.h>

void main(){

int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;

system("cls");

printf("enter the number of row=");

scanf("%d",&r);

printf("enter the number of column=");

scanf("%d",&c);

printf("enter the first matrix element=\n");

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

for(j=0;j<c;j++)

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

printf("enter the second matrix element=\n");

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

for(j=0;j<c;j++)

41 | P a g e
{

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

printf("multiply of the matrix=\n");

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

for(j=0;j<c;j++)

mul[i][j]=0;

for(k=0;k<c;k++)

mul[i][j]+=a[i][k]*b[k][j];

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

for(j=0;j<c;j++)

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

printf("\n");

42 | P a g e
}

getch();

Output :

43 | P a g e
Q21. Write a program of linear search.

Code of Program :
#include <stdio.h>

#include <conio.h>

void main()

int array[100], search, c, n;

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

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

scanf("%d", &array[c]);

printf("Enter a number to search\n");

scanf("%d", &search);

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

if (array[c] == search {

printf("%d is present at location %d.\n", search, c+1);

break;

44 | P a g e
}

if (c == n)

printf("%d isn't present in the array.\n", search);

getch();

Output :

45 | P a g e
Q22. Write a program of Bubble sort.

Code of Program :
#include<stdio.h>

#include <conio.h>

void print(int a[], int n)

{ int i;

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

{ printf("%d ",a[i]);

void bubble(int a[], int n)

int i, j, temp;

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

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

if(a[j] < a[i])

{ temp = a[i];

a[i] = a[j];

a[j] = temp;

46 | P a g e
}

void main ()

int i, j,temp;

int a[5] = { 10, 35, 32, 13, 26};

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

printf("Before sorting array elements are - \n");

print(a, n);

bubble(a, n);

printf("\nAfter sorting array elements are - \n");

print(a, n);

getch();

Output :

47 | P a g e
Q23. Write a code to illustrate the use of of strlen() function.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

char name[] = "superman";

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

int length=strlen(name);

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

getch();

Output :

48 | P a g e
Q24. Write a code to illustrate the use of of strcpy() function.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <string.h>

void main() {

char college[10];

char area[10]="pragati";

strcpy(college, area);

printf("\n college: %s", college);

getch();

Output :

49 | P a g e
Q25. Write a code to illustrate the use of of strcmp() function.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <string.h>

void main() {

char fruit_1[] = "mango";

char fruit_2[] = "banana";

int result = strcmp(fruit_1, fruit_2);

if (result == 0) {

printf("The strings are equal.\n");

} else if (result < 0) {

printf("String 1 is less than string 2.\n");

} else {

printf("String 1 is greater than string 2.\n");

getch();

Output :

50 | P a g e
Q26. Write a program to illustrate the use of strcat() function.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <string.h>

void main() {

char str1[10]="hello";

char str2[20]="everyone";

strcat(str1, str2);

printf("%s", str1);

getch();

Output :

51 | P a g e
Q27. Write a program to illustrate the use of strrev() function.

Code of Program :
#include <stdio.h>

#include <conio.h>

#include <string.h>

int main()

char name[10]="anurag";

printf("The given string is =%s\n", name);

printf("After reversing string is =%s", strrev(name))

getch();

Output :

52 | P a g e
Q28. Write a program that calls a function by value.

Code of Program:
#include <stdio.h>

#include <conio.h>

void square(int n);

int main(){

int n;

printf("enter the value of n:\n”);

scanf("%d",&n);

printf("the value of n is:%d\n", n);

square(n);

getch();

void square(int n){

n=n*n;

printf("square is :%d", n);

Output :

53 | P a g e
Q29. Write a program that calls a function by refrence.

Code of Program :
#include <stdio.h>

#include <conio.h>

void square(int n);

voidmain(){

int n;

printf("enter the value of n:\n”);

scanf("%d",&n);

printf("the value of n is:%d\n", n);

square(n);

getch();

void square(int n){

n=n*n;

printf("square is :%d", n);

Output :

54 | P a g e
Q30. Write a program to illustrate the use of structure pointer.

Code of Program :
#include <stdio.h>

#include <conio.h>

struct student{

char name[10];

int age;

};

void main(){

struct student s1={"devesh", 18};

struct student *p;

p=&s1;

printf ("student Name is %s\n ", (*p).name);

printf ("student age is %d", (*p).age);

getch();

Output :

55 | P a g e
Q31. Write a program to access the element of array using pointer.

Code for Program:


#include <stdio.h>

#include <conio.h>

void main ()

int p[5] = { 8, 9, 10, 11, 12 }, i;

int *ptr = p;

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

printf ("&p[%d] = %p \t p[%d] = %d\n", i, ptr + i, i, *(ptr + i));

getch();

Output:

56 | P a g e
Q32. Write a program for declaration and use of pointer to pointer.

Code of Program:
#include <stdio.h>

#include <conio.h>

void main(){

int a=20;

int *p,**pp;

p=&a; pp=&p;

printf("value of a is %d\n", a);

printf("value of a from its pointer p is :%d\n", *p);

printf("value of a from its pointer to pointer pp is :%d\n", **pp);

getch();

Output:

57 | P a g e
Q33. Write a program to illustrate the use of pointer and string.

Code of Program:

#include <stdio.h>

voidmain()

char str[20];

char *p;

printf("Enter any string: ");

fgets(str, 100, stdin);

p=str;

printf("The input string is: ");

while(*p!='\0')

printf("%c",*p++);

getch();

Output:

58 | P a g e
Q34. Write a program to illustrate the use of pointer and function.

Code of Program:
#include <stdio.h>

#include <conio.h>

void swap(int *, int *);

void main()

{ int x,y;

x=10;

y=20;

printf("before swapping x = %d and y = %d \n", x, y );

swap(&x, &y);

printf (after swapping x = %d and y = %d", x, y);

getch();

void swap(int *a, int *b)

int t;

t=*a;

*a=*b;

*b=t;}

Output:

59 | P a g e
Q35. Write a program for no arguments and no returns.

Code of Program:
#include <stdio.h>

#include <conio.h>

void about_myself();

void main()

about_myself();

getch();

void about_myself(){

printf("My name is Devesh Chopkar \n");

printf("I am from raipur \n");

printf("My hobby is Reading ");

Output :

60 | P a g e
Q36. Write a program for arguments and no return values.

Code of Program:

#include<stdio.h>

#include<conio.h>

void division(int,int);

void main()

{ int a,b;

printf("Enter The Value of a then b : ");

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

division(a,b);

getch();

void division(int x,int y)

int c;

c=x/y;

printf("\n division of a by b is : %d", c);

61 | P a g e
Output:

62 | P a g e
37. Write a program for arguments and return value.

Code of Program:

#include <stdio.h>

#include <conio.h>

int product(int x,int y);

int product(int x, int y)

{ int c;

c = x * y;

return c;

void main()

int a = 10, b = 5;

int c = product(a,b);

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

getch();

Ouput :

63 | P a g e
Q38. Write a program to demonstrate the uses of union.

Code of Program:
#include <stdio.h>

#include <conio.h>

union student {

float marks;

char name[15];

};

void main()

{ union student u1;

printf("\n Enter the name");

scanf("%s",&u1.name);

printf("\n Name = %s",u1.name);

printf("\n Enter the marks");

scanf("%f",&u1.marks);

printf("\n Marks = %f",u1.marks);

printf("\n Name = %s",u1.name);

getch();

64 | P a g e
Output :

65 | P a g e
Q39. Write a program to illustrate the use of structure.

Code of Program:
#include <stdio.h>

#include <conio.h>

#include <string.h>

struct student {

char name[20];

int age;

};

void main()

struct student s1;

printf("enter name : ") ;

scanf("%s",&s1.name);

printf("\n enter age : ");

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

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

printf("\n age : %d", age);

getch();

66 | P a g e
Output :

67 | P a g e
Q40. Write a program to passing structures variable.

Code of Program:
#include <string.h>

#include <conio.h>

#include <stdio.h>

struct student

int id;

char name[20];

float percentage;

};

void func(struct student record);

void main()

struct student record;

record.id=1;

strcpy(record.name, "Raju");

record.percentage = 86.5;

func(record);

getch();

void func(struct student record)

68 | P a g e
{

printf(" Id is: %d \n", record.id);

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

printf(" Percentage is: %f \n", record.percentage);

Output :

69 | P a g e
Q41. Write a program to return structure variables

Code of Program :
#include <stdio.h>

struct student {

char name[20];

int age;

float marks;

};

struct student get_student_data()

struct student s;

printf("Enter name: ");

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

printf("Enter age: ");

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

printf("Enter marks: ");

scanf("%f", &s.marks);

return s;

int main()

struct student s1 = get_student_data();

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

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

70 | P a g e
printf("Marks: %.1f\n", s1.marks);

return 0;

Output:

71 | P a g e
Q42. Write a program to assign the values of structure to another
structure.
Code of Program:

#include <stdio.h>

#include <string.h>

#include <conio.h>

struct Person {char name[50];

int citypin;

float bill_amt;

} person1, person2;

void main() {

strcpy(person1.name, "pyarelal");

person1.citypin = 501;

person1. bill_amt = 2500;

person2 = person1;

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

printf("Citizenship No.: %d\n", person2.citypin);

printf("Salary: %f", person2.bill_amt);

getch();}

Output :

72 | P a g e
Q43. Write a program to compare the value of structure variables.
Code of Program:
#include <stdio.h>

#include <conio.h>

struct Point {

int x;

int y;

};

voidmain() {

struct Point point1 = {5, 10};

struct Point point2 = {5, 10};

if (point1.x == point2.x && point1.y == point2.y) {

printf("point1 and point2 have the same values.\n");

} else {

printf("point1 and point2 have different values.\n");

getch();

Output:

73 | P a g e
Q44. Write a program to find the total memory occupied by structure
and union variable.

Code of Program:
#include <stdio.h>

#include <conio.h>

struct s_tag

{int a;

long int b;

} x;

union u_tag

int a;

long int b;

} y;

void main()

{printf("Memory occupied for structure = %d", sizeof(x));

printf("\nMemory occupied for union = %d", sizeof(y));

getch();

} Output:

74 | P a g e
Q45. Write a program for nested structure.

Code of Program:
#include<stdio.h>

struct address

char city[20];

int pin;

char phone[14];

};

struct employee

char name[20];

struct address add;

};

void main ()

struct employee emp;

printf("Enter employee information?\n");

scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);

printf("Printing the employee information....\n");

printf("name: %s\nCity: %s\nPincode: %d\nPhone:


%s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);

75 | P a g e
Output:

76 | P a g e
Q46. Write a program to store records of five students in an array of
structure.

Code of Program:
#include<stdio.h>

#include<conio.h>

struct student {

char name[20];

int age;

};

void main()

struct student bca[5];

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

printf("Enter details of student %d:\n", i + 1);

printf("Name: ");

scanf("%s", bca[i].name);

printf("Age: ");

scanf("%d", &bca[i].age);

printf("\n");

printf("Records of all students:\n");

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

printf("Student %d:\n", i + 1);

printf("Name: %s\n", bca[i].name);

77 | P a g e
printf("Age: %d\n", bca[i].age);

printf("\n");

getch();

Output:

78 | P a g e
Q47. Write a program to read the content from a file.

Code of Program:
#include <stdio.h>

#include <conio.h>

Void main()

FILE* fp;

char ch;

fp = fopen("abc.txt", "r");

ch=fgetc(fp);

while(ch!=EOF)

printf(“%c”,ch);

ch=fgetc(fp);

fclose(fp);

getch();

Output:

79 | P a g e
Q48. Write a program to copy the content from one file to another
file.
Code of Program:
#include <stdio.h>
#include <conio.h>

Void main()
{
FILE *fp1,*fp2;
charch;
fp1 =fopen("hello.txt","r");
fp2 =fopen("xyz.txt","w");

while((ch=getc(fp1))!=EOF)
{
putc(ch, fp2);
}

fclose(fp1);
fclose(fp2);
}

Output:

80 | P a g e
Q49. Write a program to write the content to a file.

Code of Program:

#include <stdio.h>

#include <conio.h>

void main()

FILE *fp;

char content[500];

fp = fopen("abc.txt", "w");

printf("enter the content for your file");

scanf("%s",content);

fprintf(fp,"%s",content);

fclose(fp);

getch():

Output:

81 | P a g e
Q50. Write a program to use the dynamic memory allocation function.

Code of Program:

Malloc()
#include<stdio.h>

#include<conio.h>

voidmain(){

int n,i,*ptr,sum=0;

printf("Enter number of elements: ");

scanf("%d",&n);

ptr=(int*)(n*sizeof(int));

if(ptr==NULL)

printf("Sorry! unable to allocate memory");

printf("Enter elements of array: ");

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

scanf("%d",ptr+i);

sum+=*(ptr+i);

printf("Sum=%d",sum);

free(ptr);

getch();}

82 | P a g e
Output:

Calloc()
#include<stdio.h>

#include<conio.h>

voidmain(){

int n,i,*ptr,sum=0;

printf("Enter number of elements: ");

scanf("%d",&n);

ptr=(int*)calloc(n,sizeof(int));

if(ptr==NULL)

printf("Sorry! unable to allocate memory");

exit(0);

83 | P a g e
printf("Enter elements : ");

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

scanf("%d",ptr+i);

sum+=*(ptr+i);

printf("Sum=%d",sum);

free(ptr);

getch();

Output:

Realloc
#include <stdio.h>

#include <conio.h>

voidmain() {

84 | P a g e
int *ptr = (int*) realloc(3 * sizeof(int));

ptr[0] = 10;

ptr[1] = 20;

ptr[2] = 30;

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

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

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

free(ptr);

getch();

Output:

Free()
#include <stdio.h>

#include <conio.h>

voidmain()

85 | P a g e
int *ptr, *ptr1;

int n, i;

n = 5;

printf("Enter number of elements: %d\n", n);

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

ptr1 = (int*)calloc(n, sizeof(int));

if (ptr == NULL || ptr1 == NULL) {

printf("Memory not allocated.\n");

exit(0);

else {

printf("Memory successfully allocated using malloc.\n");

free(ptr);

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

printf("\nMemory successfully allocated using calloc.\n");

free(ptr1);

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

getch();

86 | P a g e
Output :

Enter number of elements: 5


Memory successfully allocated
using malloc.
Malloc Memory successfully
freed.

Memory successfully allocated


using calloc.
Calloc Memory successfully
freed.

87 | P a g e
88 | P a g e

You might also like