0% found this document useful (0 votes)
25 views31 pages

Bca 4

Uploaded by

Rakesh Singh
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)
25 views31 pages

Bca 4

Uploaded by

Rakesh Singh
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

Page |1

RAJA BALWANT SINGH COLLEGE,


AGRA
Session 2017-18

Assignment
Of
'C' PROGRAMING

Submitted To:
Mr. Vivek Sir Submitted By:
Utkarsh Jindal
BCA 2nd Sem.
Page |2

Acknowledgement
I would like to express my special thanks of
gratitude to my teacher Mr. VIVEK SIR who
gave me the golden opportunity to do this
wonderful project on the topic 'C' LANGUAGE
PROGRAMS, which also helped me in doing a
lot of Research and i came to know about so many
new things I am really thankful to them.
Secondly i would also like to thank my parents
and friends who helped me a lot in finalizing this
project within the limited time frame.
Page |3

INDEX
PROGRAM PAGE NO.
Largest no. in array 4

Convert Decimal no. into Binary 6

Selection sorting 7

Bubble sort 9

Transpose of Matrix 11

Product of two Matrix 13

Delete an element in array 16

Find string length without function 18

Reverse a String 19

Concatenation of two Strings 20

Compare two Strings 21

Details of employee in structure 22

Details of student in Union 24

Dynamic Memory Allocation 26

Demonstration of Pointers Arithmetic 29


Page |4

Program to find the largest no. in an array of ten


values:-

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], max , mini , i ;
printf("enter the values of elements of an array");
for(i=0;i<10;i++)
{
scanf("%d\t",&a[i]);
}
max=a[0];
mini=a[0];
for(i=1;i<10;i++)
{
if(max<a[i])
{
max=a[i];
}
else if(mini>a[i])
{
mini=a[i];
}
else
{
break;
Page |5

}
}

printf("\n the largest number in an array is %d",max);


printf("\n the smallest number in an array is %d",mini);
getch();
}

Output:
Page |6

Program to convert decimal no. into


binary
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i=0,j;
printf("Enter a decimal number to convert into binary number");
scanf("%d",&n);
while(n>0)
{
a[i]=n%2;
n=n/2;
i++;
}
printf("Number after converting into binary = ");
for(j=i-1;j>=0;j--)
{
printf("%d",a[j]);
}
getch();
}
Page |7

Output:

Program of section sorting


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5] , i , j , t ;
printf("Enter the values of elements of an array");
for(i=0;i<5;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[ i ];
a[i]=a[j];
a[j]=t;
Page |8

}
}
}
printf("\n the sorted array is\n");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
getch();
}
Output:
Page |9

Program to show Bubble sorting


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,c;
printf("How many numbers are there");
scanf("%d",&n);
printf("\nEnter the values of elements of an array are");
for(i=0;i<n;i++)
{
scanf("%d\t",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
printf("\n the sorted array is\n");
for(i=0;i<n;i++)
P a g e | 10

{
printf("%d\n",a[i]);
}
getch();
}

Output:
P a g e | 11

Program to input a matrix and transpose of


matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],i,j;
printf("Enter the values of the elements of an array are");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d\t",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
printf("\n the original matrix is\n");
for(i=0;i<3;i++)
{
P a g e | 12

for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\nThe transpose of the matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}
Output:
P a g e | 13

Program to input of two matrix and


multiple of both matrix
#include<stdio.h>
#include<conio.h>

void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k,sum;
printf("Enter the order of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the values of the element of a first matrix are");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
P a g e | 14

}
printf("\nEnter the order of second matrix");
scanf("%d %d",&p,&q);
printf("\nEnter the values of the elements of a second matrix are");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
if(n!=p)
{
printf("\nMultiplication is not possible");
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
}
printf("\n\t\t the resultant matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
P a g e | 15

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

Output:
P a g e | 16

Program to delete an item from an


array
#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n,k;

printf("How many numbers are there");

scanf("%d",&n);

printf("\nEnter the values of the elements of an array are");

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

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

printf("\nEnter the position from which we delete the desired item");


P a g e | 17

scanf("%d",&k);

if(k<1||k>n)

printf("\nItem can't be deleted");

else

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

a[i-1]=a[i];

n=n-1;

printf("\nAfter deletion array is\n");

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

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

getch();

Output:
P a g e | 18

Program to input string and count


the string length without function
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i,l = 0;
printf("Enter a string to find the length of this string");
gets(str);
for(i=0;str[i]!='\0';i++)
{
l++;
P a g e | 19

}
printf("\nThe length of a string is %d",l);
getch();
}

Output:

Program to print a string in reverse


order
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i,k;
printf("Enter the string");
gets(str);
for(i=0;str[i]!='\0';i++);
for(k=i-1;k>=0;k--)
{
P a g e | 20

printf("\nThe reverse of the string is %c",str[k]);


}
getch();
}

Output:

Program to concatenate two strings


#include<stdio.h>
#include<conio.h>
void main()
{
char str1[15],str2[15];
int i,j;
printf("Enter two strings");
gets(str1);
gets(str2);
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++)
{
P a g e | 21

str1[i]=str2[j];
i++;
}
str1[i]='\0';
printf("\nThe merge string is %s",str1);
getch();
}

Output:

Program to compare two strings


#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf("Enter 1st string\n");


gets(a);

printf("Enter 2nd string\n");


gets(b);

if (strcmp(a,b) == 0)
printf("The strings are equal.\n");
else
P a g e | 22

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

return 0;
}
Output:

Program to input the details of an


employee using structure and print
same
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
int id;
char name[30];
float salary;
};
P a g e | 23

void main()
{
struct employee E;
printf("Enter the employee details");
printf("\n\tEnter the name of employee");
gets([Link]);
printf("\nEnter the employee id");
scanf("%d",&[Link]);
printf("\n\tEnter the salary of the employee");
scanf("%f",&[Link]);
printf("\n\nDisplay the details of the employee");
printf("\nName = %s",[Link]);
printf("\nId = %d",[Link]);
printf("\nSalary = %f",[Link]);
getch();
}

Output:
P a g e | 24

Program to input the details of a


student using Union and print same
#include<stdio.h>
#include<conio.h>
#include<string.h>
union student
P a g e | 25

{
int rollno;
char name[20];
float marks;
};
void main()
{
union student S;
[Link] = 21;
strcpy([Link],"UTKARSH");
[Link] = 99;
printf("the roll number of student is %d",[Link]);
printf("\nthe name of the student is %s",[Link]);
printf("\nthe marks of a student is %f",[Link]);
getch();
}

Output:
P a g e | 26
P a g e | 27

Program to input the details of ten


students using Dynamic memory
Allocation
#include <stdio.h>
#include <stdlib.h>

struct student
{
char name[30];
int roll;
float perc;
};

int main()
{
struct student *pstd;
int n,i;

printf("Enter total number of elements: ");


scanf("%d",&n);

pstd=(struct student*)malloc(n*sizeof(struct student));

if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}

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


{
printf("\nEnter detail of student [%3d]:\n",i+1);
printf("Enter name: ");
scanf(" ");
gets((pstd+i)->name);
printf("Enter roll number: ");
scanf("%d",&(pstd+i)->roll);
P a g e | 28

printf("Enter percentage: ");


scanf("%f",&(pstd+i)->perc);
}
printf("\nEntered details are:\n");
for(i=0; i<n; i++)
{
printf("%30s \t %5d \t %.2f\n",(pstd+i)->name,(pstd+i)->roll,(pstd+i)-
>perc);
}
return 0;
}
Output:
P a g e | 29
P a g e | 30

Program to Demonstrate Pointers


Arithmetic
#include<stdio.h>

#include<conio.h>

void main() {

int int_var = 10, *int_ptr;

char char_var = 'A', *char_ptr;

float float_val = 4.65, *float_ptr;

int_ptr = &int_var;

char_ptr = &char_var;

float_ptr = &float_val;

printf("Address of int_var = %u\n", int_ptr);

printf("Address of char_var = %u\n", char_ptr);

printf("Address of float_var = %u\n\n", float_ptr);

int_ptr++;

char_ptr++;

float_ptr++;

printf("After increment address in int_ptr = %u\n", int_ptr);

printf("After increment address in char_ptr = %u\n", char_ptr);

printf("After increment address in float_ptr = %u\n\n", float_ptr);

int_ptr = int_ptr + 2;

char_ptr = char_ptr + 2;

float_ptr = float_ptr + 2;
P a g e | 31

printf("After addition address in int_ptr = %u\n", int_ptr);

printf("After addition address in char_ptr = %u\n", char_ptr);

printf("After addition address in float_ptr = %u\n\n", float_ptr);

getch();

return 0;

Output:

You might also like