You are on page 1of 78

1

AN ASSIGNMENT ON :

PROGRAMING IN C LANGUAGE
FROM : B.C.A – 1st YEAR
YEAR: (2022-23)
REGULAR

Guided by : Submitted by :
Mr. Mannu Rawani Anshu Kumar
Singh
(Head of CS Department) (Student B.C.A-1st)
2

ACKNOWLDGEMENT

All the work or task a student does becomes truly


successful with the help of an Inspiration &
Encouragement by seniors, teachers or professors.
Therefore my heartily thanks to all of them who
helped me in preparing this assignment . And thanks
to my parents providing me with the required
support for completing this work.
I acknowledge and thanks
for the help I got from Mr. Mannu Rawani sir, our
“Assignment in charge”.
3

INDEX
S.no. Objective/Question: Page no. Remarks

1. Write a program to swap the value of 7


two variables with the help of third
variable.
2. Write a program to swap the value of 8
two variables without using third
variable.
3. Write a program to create equivalent a 64-65
four function calculator by using switch
statement.
4. Write a program which illustrates the 17-18
switch statement.
5. Write a program to find greatest among 10-11
three variables.
6. Write a program to find greatest among 54-55
three variables using nested if.
7. Write a program to check whether the 27
given number is even or add.
8. Write a program to find that entered 45-46
year is leap year or not.
9. Write a program for estimating simple 9
interest.
10. Write a program for estimating 28
compound interest.
11. Write a program to check that give 29
character is vowel or not.
12. Write a program to generate fibonocci 48-47
series upto given terms
4

13. Write a program to find the factorial of 12


given number.
14. Write the program to check given name 66-67
is Armstrong or not.
15. Write a program to count the length of 30
string without using library function.
16. Write a program to find the average of 61
array's elements.
17. Write a program to find the largest and 51-52
smallest elements in the one
dimensional array.
18. Write a program to access the elements 43-44
of two dimensional array.
19. Write a program for addition of two 13-14
matrices.
20. Write a program of multiplication of 20-21
two matrices.
21. Write a program of linear search. 38-39
22. Write a program of bubble sort. 49-50
23. Write a program to illustrate the use of 15
strlen( ) function.
24. Write a program to illustrate the use of 16
strcpy( ) function.
25. Write a program to illustrate the use of 22-23
strcmp( ) function.
26. Write a program to illustrate the use of 19
strcat( ) function.
27. Write a program to illustrate the use of 24
strrev( ) function.
28. Write a program that calls a function by 25
value.
29. Write a program that calls a function by 26
reference.
30. Write a program to illustrate the use of 42
a structure pointer.
5

31. Write a program to access the element 40


of array using pointer.
32. Write a program for declaration and 41
use of pointers to pointers.
33. Write a program to illustrate the use of 31
pointer and string.
34. Write a program to illustrate the use of 32
pointer and function.
35. Write a program for no arguments and 33
no return values.
36. Write a program for arguments and no 36
return values.
37. Write a program for arguments and 34
return values.
38. Write a program to demonstrate the 35
uses of union.
39. Write a program to illustrate the use of 37
structure.
40. Write a program to passing structures 53
variable.
41. Write a program to returning structures 62-63
variable.
42. Write a program to assign the values of 56
a structure to another structure.
43. Write a program to compare the values 68-69
of structure variables.
44. Write a program to find the total 70-71
memory occupied by structure and
union variables.
45. Write a program for nested structure. 76-77
46. Write a program to store records of s 72-73
students in array of structure.
47. Write a program to read content from a 57-58
file.
6

48. Write a program to copy the content 59-60


from a file to another file.
49. Write a program to Write the content 74-75
to a file.
50. Write a program to use the dynamic 78
memory allocation functions.
7

PROGRAM

Q. (1) write a program to swap of two variables with the help


of third variable .
Code of program :-
#include<stdio.h>
int main(){
int x = 10;
int y = 20;
int temp;
printf("before swapping:\n ");
printf("x=%d,y=%d\n",x,y);
temp =x;
x =y;
y =temp;
printf("after swapping:\n ");
printf("x=%d,y=%d\n",x,y);
return 0;
}

Output of program :-
8

Q. (2) write a program to swap the value of two variable


without using third variable.
code of program:-
#include<stdio.h>
int main(){
int x=30;
int y=40;
printf("before swapping:\n ");
printf("x=%d,y=%d\n",x,y);
x = x+y;
y = x-y;
x = x-y;
printf("after swapping:\n ");
printf("x=%d,y=%d\n”,x,y );
return 0;
}

Output of program:-
9

Q. (9) write a program for estimating simple interest.


Code of program:-
#include <stdio.h>
int main(){
float p,r,t,si;
printf("\n enter the principal amount: ");
scanf("%f",&p);
printf("\n enetr the value of rate: ");
scanf("%f",&r);
printf("\n enter the time: ");
scanf("%f",&t);
si=(p*r*t)/100;
printf("si=%f",si);
return 0;
}

Output of program:-
10

Q. (5) write a program to find greatest among three


variables.
Code of program:-
#include <stdio.h>

int main() {
int a, b, c;
printf("enter the first number: ");
scanf("%d",&a);
printf("enter the second number: ");
scanf("%d",&b);
printf("enter the third number: ");
scanf("%d",&c);
if (a >= b && a >= c) {
printf("%d is the greatest number\n", a);
} else if (b >= a && b >= c) {
printf("%d is the greatest number\n", b);
} else {
printf("%d is the greatest number\n", c);
}
return 0;
}
11

Output of program:-
12

Q. (13) write a program to find the factorial of given number.


Code of program:-
#include<stdio.h>

int main()
{
int n,i,f=1;
printf("\n enter the value: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n factorial=%d",f);
return 0;
}
Output of program:-
13

Q. (19) write a program for addition of two matrices.


Code of program:-
#include<stdio.h>
int main(){
int a[3][3]={{2,3,4},{3,3,9},{5,7,3}};
int b[3][3]={{8,3,7},{2,7,9},{5,9,1}};
int c[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("the sum of a,b is: \n");
for(i=0;i<3;i++)

for(j=0;j<3;j++)
{
printf("\t%d",c[i][j]);
}
printf("\n");
}
return 0;
}
14

Output of program:-
15

Q. (23) write a program illustrate the use of strlen( ) function.


code of program:-
#include<stdio.h>
#include<string.h>
int main(){
char name[10]="Anshu";
int len;
len= strlen(name);
printf("\n Length of String=%d",len);
return 0;
}

Output of program:-
16

Q. (24) write a program illustrate the use of strcpy( ) function.


code of program:-
#include<stdio.h>
#include<string.h>
int main()
{
char name1[10]="Anshu";
char name2[10];
strcpy(name2,name1);
printf("\n Copied String=%s",name2);
return 0;
}
output of program:-
17

Q. (4) write a program which illustrates the switch statement.


Code of program:-
#include<stdio.h>
int main(){
int dayno;
printf(" enter the day number \n");
scanf("%d",&dayno);
switch(dayno)
{
case 1:printf("Monday");
break;
case 2:printf("Tuesday");
break;
case 3:printf("Wednesday");
break;
case 4:printf("Thrusday");
break;
case 5:printf("Friday");
break;
case 6:printf("Saturday");
break;
case 7:printf("Sunday");
break;
}
return 0;
}
18

Output of program:-
19

Q. (26) write a program which illustrate the use of strcat( )


function.
Code of program:-
#include<stdio.h>
#include<string.h>

int main(){
char name[10]="Anshu";
char surname[10]="Singh";

strcat(name,surname);
printf("\n Concated String=%s",name);
return 0;
}

Output of program:-
20

Q. (20) write a program of multiplication of two matrices.


Code of program:-
#include<stdio.h>
int main()
{
int a[3][3]={{6,3,2},{2,3,7},{4,7,9}};
int b[3][3]={{9,7,6},{2,3,1},{6,5,2}};
int c[3][3],i,j,k;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n Multiplication of Two Matrix are \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
21

{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}

Output of program:-
22

Q. (25) write a program to illustrate the use of strcmp


( ) function.
Code of program:-
#include <stdio.h>
#include <string.h>

int main(){
char name1[20] = "Anshu";
char name2[20] = "Singh";

int result = strcmp(name1,name2);

if (result < 0){


printf("name1 is less than name2");
} else if (result > 0){
printf("name1 is greater than name2");
} else{
printf("name1 is equal to name2");
}
return 0;
}
23

Output of program:-
24

Q. (27) write a program to illustrate the use of strrev( )


function.
Code of program:-
#include <stdio.h>
#include <string.h>

int main() {
char name[20] = "Anshu";
printf("real string: %s\n",name);
strrev(name);
printf("Reversed string: %s",name);
return 0;
}
Output of program:-
25

Q. (28) write a program that calls a function by value.


Code of program:-
#include <stdio.h>

int function(int x){


return x * 2;
}

int main(){
int value =5;
int result = function(value);
printf("%d",result);
return 0;
}
Output of program:-
26

Q. (29) write a program that calls a function by reference.


Code of program:-
#include <stdio.h>

int function(int *x){


*x= *x * 2;
}
int main(){
int value = 5;
function(&value);
printf("%d",value);
return 0;
}
Output of program:-
27

Q. (7) write a program check whether the given number is


oven or odd.
Code of program:-
#include <stdio.h>

int main(){
int num;
printf("enter a number: ");
scanf("%d",&num);
if (num % 2==0){
printf("%d is even",num);
} else{
printf("%d is odd",num);
}
return 0;
}
Output of program:-
28

Q. (10) write a program for estimating compound interest.


Code of program:-
#include <stdio.h>
#include <math.h>

int main(){
float p,r,t;
printf("enter the principal amount: ");
scanf("%f",&p);
printf("enter the annual interest rate: ");
scanf("%f",&r);
printf("enter the number of years: ");
scanf("%f",&t);

float a= p* (pow((1 + r/100),t));


printf("The compound interest is: %f\n",a-p);
return 0;
}
Output of program:-
29

Q. (11) write a program to check that give character is vowel


or not.
Code of program:-
#include <stdio.h>

int main(){
char c;
printf("enter a character: ");
scanf("%c",&c);

if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')


printf("%c is a vowel\n",c);
else
printf("%c is not a vowel\n",c);
return 0;
}
Output of program:-
30

Q. (15) write a program to count the length of string without


using library function.
Code of program:-
#include <stdio.h>

int main(){
char str[100];
int i,length =0;
printf("Enter a string: ");
scanf("%s",str);
for (i =0; str[i] !='\0'; i++){
length++;
}
printf("The length of the string is: %d",length);
return 0;
}

Output of program:-
31

Q. (33) write a program to illustrate the use of pointer and


string.
Code of program:-
#include <stdio.h>

int main(){
char str[100];
char *ptr;
printf("enter a string: ");
scanf("%s",str);
ptr =str;
printf("the string is: %s\n",ptr);
printf("the characters of the string are:\n");
while(*ptr !='\0'){
printf("%c ",*ptr);
ptr++;
}
return 0;
}

Output of program:-
32

Q. (34) write a program to illustrate the use of pointer and


function.
Code of program:-
#include <stdio.h>

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


int temp= *a;
*a =*b;
*b =temp;
}

int main() {
int x=5, y=10;
printf("before swapping: x =%d, y =%d\n",x,y);
swap(&x, &y);
printf("after swapping: x =%d,y =%d\n",x,y);
return 0;
}
Output of program:-
33

Q. (35) write a program for no arguments and no return


values.
Code of program:-

#include <stdio.h>

void print_anshu(){
printf("Anshu!\n");
}
int main(){
print_anshu();
return 0;
}

Output of program:-
34

Q. (37) write a program for arguments and no return values.


code of program:-
#include <stdio.h>

int add(int a,int b){


return a+b;
}
Int main(){
int x =5, y =10,result;
result = add(x,y);
printf("the sum of %d and %d is: %d\n",x,y,result);
return 0;
}
Output of program:-
35

Q (38) write a program demonstrate the uses of union.


Code of program:-
#include <stdio.h>

union data{
int i;
float f;
char str[20];
} data;

int main(){
data.i =10;
printf("integer value: %d\n",data.i);
data.f =220;
printf("float value: %f\n",data.f);
strcpy(data.str, "Anshu");
printf("string value: %s\n",data.str);
return 0;
}

Output of program:-
36

Q. (36) write a program for arguments and no return values.


Code of program:-
#include <stdio.h>

void print_numbers(int a, int b, int c){


printf("the numbers are: %d,%d,%d\n",a,b,c);
}

int main(){
int x =10, y =20, z =30;
print_numbers(x,y,z);
return 0;
}
Output of program:-
37

Q. (39) write a program to illustrate the use of structure.


Code of program:-
#include<stdio.h>
struct laptop{
char name[20];
float price;
};
int main()
{
struct laptop l1;
printf("\n enter the laptop name: ");
scanf("%s",&l1.name);
printf("\n enter the price");
scanf("%f",&l1.price);
printf("\n laptop name =%s",l1.name);
printf("\n laptop price =%f",l1.price);
return 0;

}
Output of program:-
38

Q. (21) write a program of linear search.


Code of program:-
#include<stdio.h>
int main()
{
int a[6]={9,3,6,8,2,1};
int num,i,count=0;
printf("\n enter the element you want to search: ");
scanf("%d",&num);
for(i=0;i<6;i++)
{
if(num==a[i])
{
printf("\n element found: ");
count=1;
}
}
if(count==0)
printf("\n element not found: ");
return 0;
}
39

Output of program:-
40

Q. (31) write a program to access the element of array using


pointer.
Code of peogram:-
#include<stdio.h>
int main()
{
int arr[] ={1,2,3,4,5};
int *ptr;
ptr=arr;
printf("elements of the array: ");
for (int i=0; i < sizeof(arr)/sizeof(arr[0]);
i++){
printf("%d",*(ptr+i));
}
return 0;
}
Output of program:-
41

Q. (32) write a program for declaration and use of pointers to


pointers.
Code of program:-
#include<stdio.h>
int main(){
int a=10;
int *ptr1;
int **ptr2;

ptr1 = &a;
ptr2 = &ptr1;
printf("value of a: %d\n",a);
printf("value of a using ptr1: %d\n",*ptr1);
printf("value of a using ptr2: %d\n",**ptr2);
return 0;
}
Output of program:-
42

Q. (30) write a program to illustrate the use of a structure


pointer.
Code of program:-
#include<stdio.h>
int main(){
struct person{
char name[20];
int age;
};
struct person p1={"Anshu",18};
struct person *ptr;
ptr=&p1;
printf("\n Name=%s",ptr->name);
printf("\n Age=%d",ptr->age);
return 0;
}
output of program:-
43

Q. (18) write a program to access the elements of two


dimensional array.
Code of program:-
#include<stdio.h>
int main(){
int a[3][3];
int i,j;
printf("\n enter the elements a[3][3]");
for(i=0;i<3;i++){

for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
printf("\n enter the array of elements are: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;

}
44

Output of program:-
45

Q. (8) write a program find the entered year is leap year or


not.
Code of program:-
#include <stdio.h>

int main(){
int year;
printf("enter a year: ");
scanf("%d",&year);
if(year % 4== 0){
if(year % 100== 0){
if(year % 400== 0){
printf("%d is a leap year",year);
} else{
printf("%d is not a leap year",year);
}
} else{
printf("%d is a leap year",year);
}
} else{
printf("%d is not a leap year",year);
}
return 0;
}
46

Output of programm :-
47

Q. (12) write a program to generate fibonocci series upto


given terms.
Code of program:-
#include<stdio.h>
int main(){
int n,i;
int a=0,b=1,c;
printf("enter the number of terms: ");
scanf("%d",&n);
printf("fibonacci series: ");
for(i=0;i<n;i++){
if(i<=1){
c=i;
} else{
c=a+b;
a=b;
b=c;
}
printf("%d,",c);

}
return 0;
}
48

Output of program:-
49

Q. (22) write a program of bubble sort.


Code of program:-
#include<stdio.h>
int main(){
int a[5]={6,2,8,3,5};
int i,j;
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if (a[i]>a[j])
{
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nthe sorted array is: \n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
50

printf("\n");
return 0;
}

Output of program:-
51

Q. (17) write a program to find the largest and smallest


elements in one dimensional array.
Code of program:-
#include<stdio.h>
int main(){
int a[5]={6,9,5,3,4};
int i;
for (i=0;i<5;i++){
if(a[0]<a[i+1]){
int temp;
temp=a[0];
a[0]=a[i+1];
a[i+1]=temp;
}
}
printf("\ngreatest element =%d",a[0]);
return 0;
}
52

output of program:-
53

Q. (40) write a program to passing structure variable.


Code of program:-
#include<stdio.h>
int main(){
struct point{
int a;
int b;
};
int printpoint(struct point p){
printf("(%d,%d)\n",p.a,p.b);
}
struct point p1={1,2};
printpoint(p1);
return 0;
}
Output of program:-
54

Q. (6) write a program find greatest among three variables


using nested if.
Code of program:-
#include<stdio.h>
int main(){
int a,b,c;
printf("enter the first variable: ");
scanf("%d",&a);
printf("enter the second variable: ");
scanf("%d",&b);
printf("enter the third variable: ");
scanf("%d",&c);
if(a>b){
if(a>c){
printf("%d is the greatest\n",a);
} else{
printf("%d is the greatest\n",c);
}
} else{
if(b>c){
printf("%d is the greatest\n",b);
} else{
printf("%d is the greatest\n",c);
}
55

}
return 0;
}
Output of program:-
56

Q. (42) write a program to assign the values of a structures to


another structure.
Code of program:-
#include<stdio.h>
int main(){
struct example{
char name[30];
int age;
float salery;
};
struct example example1= {"Anshu kumar singh",18,3000};
struct example example2;
example2 = example1;
printf("Name:%s\n",example2.name);
printf("Age:%d\n",example2.age);
printf("Salery:%.2f\n",example2.salery);
return 0;
}
Output of program:-
57

Q. (47) write a program to read content from file.


Code of program:-
#include<stdio.h>
int main()
{
FILE *fp;
char ch;

fp=fopen("file1.txt","r");
while(ch!=EOF)
{
ch=fgetc(fp);

printf("%c",ch);
}
return 0;
}
58

Read file:-

output of program:-
59

Q. (48) write a program to copy the content from a file to


another file.
Code of program:-
#include<stdio.h>
int main(){
FILE *fr,*fw;
char ch;
fr= fopen("file1.txt","r");
fw= fopen("file2.txt","w");
while(ch!=EOF){
ch= fgetc(fr);
fputc(ch,fw);
}
printf("\n file succesfully copied");
return 0;
}
output of program:-
60

Copied file:-
61

Q. (16) write a program to find the average of array’s


elements.
Code of program:-
#include<stdio.h>
int main(){
int n,i;
float num[50],sum=0,average;
printf("enter the number of elements: ");
scanf("%d",&n);
for(i=0;i<=n;i++){
printf("enter the number %d:",i+1);
scanf("%f",&num[i]);
sum= sum+num[i];
}
average= sum/n;
printf("average =%.2f",average);
return 0;
}
Output of program:-
62

Q. (41) write a program to returning structures variable.


Code of program:-
#include <stdio.h>

struct person{
int age;
int weight;
};

struct person createperson(int age,int weight) {


struct person p;
p.age = age;
p.weight = weight;
return p;
}

int main(){
struct person p =createperson(18,53);
printf("Age=%d,weight=%d\n",p.age,p.weight);
return 0;
}
63

Output of program:-
64

Q. (3) write a program to create equivalent of a function


calculator by using switch statement.
Code of program:-
#include<stdio.h>
int main(){
int a,b,example,option;
printf("enter the value of a/n");
scanf("%d",&a);
printf("enyer the value of b/n");
scanf("%d",&b);
printf("tap 1 using for addition\n");
printf("tap 2 using for substraction\n");
printf("tap 3 using for multipliaction\n");
printf("tap 4 using for divison\n");
scanf("%d",&option);
switch(option)
{
case 1: example=(a+b);
printf("addition of a and b %d\n",example);
break;
case 2: example=(a-b);
printf("substraction of a and b %d\n",example);
break;
case 3: example=(a*b);
65

printf("multiplication of a and b %d\n",example);


break;
case 4: example=(a/b);
printf("divison of a and b %d\n",example);
break;
}
return 0;
}

Output of program:-
66

Q. (14) write the program to check give number is Armstrong


or not.
Code of program:-
#include<stdio.h>
int main(){

int n,r,c,arm=0;
printf("enter any numbers: ");
scanf("%d",&n);
c=n;
while(n>0)
{
r =n%10;
arm= (r*r*r)+arm;
n=n/10;
}
if(c==arm)
printf("armstrong number");
else
printf("not armstrong number");
return 0;

}
67

Output of program:-
68

Q. (43) write a program to compare the values structure


variables.
Code of program:-
#include<stdio.h>
struct example{
int a;
int b;
};
int comparestructs(struct example s1,struct example s2)
{
if (s1.a==s2.a && s1.b==s2.b){
return 0;
}else{
return 1;
}
}
int main(){
struct example s1={1,2};
struct example s2={1,2};
int result= comparestructs(s1,s2);
if (result==0){
printf("the structs are equal\n");
}else{
printf("the structs are not equal\n");
69

}
return 0;
}

Output of program:-
70

Q. (44) write a program to find the total memory occupied by


structure and union variables.
Code of program:-
#include<stdio.h>
struct mystruct{
int a;
char b;
float c;
};
union myunion{
int a;
char b;
float c;
};
int main(){
struct mystruct s;
union myunion u;

int size_s=sizeof(s);
int size_u=sizeof(u);
printf("total memory occuppied by structure variables:
%dbytes\n",size_s);
printf("total memory occupied by union variables:
&%dbytes\n",size_u);
71

return 0;
}

Output of program:-
72

Q. (46) write a program to store records of 5 students in


array of structure.
Code of program:-
#include<stdio.h>

struct student{
char name[30];
int rollno;
float per;
};
int main(){
struct student s1,s2,s3,s4,s5;
int i,j;
for(i=0;i<5;i++)
{
printf("\n enter the name of student: ");
scanf("%s",&s1.name);
printf("\n enter the rollno of student: ");
scanf("%d",&s1.rollno);
printf("\n enter the prcentage of student: ");
scanf("%f",&s1.per);
}
for(i=0;i<5;i++)
{
73

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


printf("\n rollno of students: %d",s1.rollno);
printf("\n percentage of students: %f",s1.per);
}
return 0;
}
Output of program:-
74

Q (49) write a program to write the content to a file.


Code of program:-
#include<stdio.h>
#include<string.h>
int main(){
FILE *fw;
char str[50];
fw=fopen("anshu.txt","w");
printf("\n enter few lines\n");
while(strlen(gets(str))>0)
{
fputs(str,fw);
}
return 0;
}
Output of program:-
75

Content file:- anshu.txt


76

Q. (45) write a program for nested structure.


Code of program:-
#include<stdio.h>
struct student
{
int rollno;
float per;
};
struct department
{
char dname[10];
struct student s1;
};
int main(){
struct department d1;
printf("\n enter the department name: ");
scanf("%s",&d1.dname);
printf("\n enter the rollno: ");
scanf("%d",&d1.s1.rollno);
printf("\n enter the percentage: ");
scanf("%d",&d1.s1.per);
return 0;
}
77

Output of program:-
78

Q. (50) write a program to use the dynamic memory


allocation functions.
Code of program:-
#include<stdio.h>
int main(){
int *ptr,i;
ptr = (int*) malloc(10);
for(i=0;i<5;i++)
{
ptr[i]=i*i;
printf("\n%d",ptr[i]);

}
return 0;
}
Output of program:-

You might also like