0% found this document useful (0 votes)
48 views66 pages

Bca Iind Sem Program

The document contains 9 code examples demonstrating various operations on arrays in C language. Example 1 counts even and odd numbers in an array. Example 2 finds the maximum and minimum elements in an array. Example 3 calculates the sum of diagonal elements in a square matrix. Example 4 inserts a new element into an array at a given position. Example 5 sorts an array in ascending order using bubble sort. Example 6 performs a binary search to find the position of an element in a sorted array. Example 7 prints the transpose of a matrix. Example 8 demonstrates addition of two matrices. Example 9 shows multiplication of two matrices.

Uploaded by

jfsdj
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)
48 views66 pages

Bca Iind Sem Program

The document contains 9 code examples demonstrating various operations on arrays in C language. Example 1 counts even and odd numbers in an array. Example 2 finds the maximum and minimum elements in an array. Example 3 calculates the sum of diagonal elements in a square matrix. Example 4 inserts a new element into an array at a given position. Example 5 sorts an array in ascending order using bubble sort. Example 6 performs a binary search to find the position of an element in a sorted array. Example 7 prints the transpose of a matrix. Example 8 demonstrates addition of two matrices. Example 9 shows multiplication of two matrices.

Uploaded by

jfsdj
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/ 66

1. WAP in c Lang input N element in array & count even & odd no.

#include<stdio.h>
#include<conio.h>
int main()
{
int Size, i, SUHAIL[Size];
int Even_Count = 0, Odd_Count = 0;
printf("\n Please Enter the Size of an Array : ");
scanf("%d", &Size);
printf("\nPlease Enter the Array Elements\n");
for(i = 0; i < Size; i++) {
scanf("%d", &SUHAIL[i]);
}
for(i = 0; i < Size; i ++) {
if(SUHAIL[i] % 2 == 0) {
Even_Count++;
} else {
Odd_Count++;
}
}
printf("\n Total Number of Even Numbers in this Array =%d ",
Even_Count); printf("\n Total Number of Odd Numbers in this
Array = %d ", Odd_Count); return 0;
}
Output:
Please Enter the Size of an Array : 6
Please Enter the Array Elements
2
4
1
6
8
3
7
Total Number of Even Numbers in this Array = 4
Total Number of Odd Numbers in this Array = 2
2. WAP in c Lang input N element in array & print max & min
elements.
#include <stdio.h>
#include <conio.h>
int main()
{
int SUHAIL[Size],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&Size);
printf("Enter elements in array :
");
for(i=0; i<Size; i++)
{
scanf("%d",&SUHAIL[i]);
}
min=max=SUHAIL[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
2
printf("minimum of array is : %d",min); printf("\
nmaximum of array is : %d",max);
return 0;
}

Output:
Enter size of the array : 5
Enter elements in array : 11
44
5
1 100 minimum of array is : 1
maximum of array is : 100

3. WAP in c Lang input N element in array & sum of diagonal


elements
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row,col,sum=0;
int SUHAIL[310[10];
printf("Enter the number of rows and columns for 1st matrix\
n");
scanf("%d%d",&row,&col);
printf("Enter the elements of thematrix\n");
for(i=0;i<row;i++)
{

3
for(j=0;j<col;j++)
{
scanf("%d",&SUHAIL[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",SUHAIL[i][j]);
}
printf("\n");
} //To add diagonal elements
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+SUHAIL[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\
n",sum);
}
Output:
Enter the number of rows and columns for 1st matrix
4
2
2
Enter the elements of the matrix
1
2
3
4
The matrix
1 2
3 4
The sum of diagonal elements of a square matrix = 5

4. Wap in C Lang input N elements in array & insert one elements with
given position.
Code:
#include<stdio.h>
#include<conio.h>
#define size 100
int i;
void insert(int SUHAIL[],int s);//fun
prototype void main()
{
int SUHAIL[size],s;
printf("\n how many element store in array=");
scanf("%d",&s);
printf("enter the %d elements=\n",s);
for(i=0;i<s;i++)

5
{
scanf("%d",&SUHAIL[i]);
}
insert(SUHAIL,s);//fun calling
}
void insert(int SUHAIL[],int s)//fun defination
{ int n,p;
printf("enter
the insert
elements and
its
position=");
scanf("%d
%d",&n,&p);
--p; for(i=s;i>=p;i--)
{
SUHAIL[i+1]=SUHAIL[i];

}
SUHAIL[
p]=n; s++;
printf("\n new array after insert the element=\n");
for(i=0;i<s;i++)
{
printf("%4d,",SUHAIL[i]);
}
}

6
Output:
2
4
5
6

7 8 enter the insert elements and its position=3 3


new array after insert the element=
1, 2, 3, 4, 5, 6, 7, 8

7
5. Wap in c Lang input N element in array & print ascending
order(bubble sort).
Code:
#include<stdio.h>
#include<conio.h>
#define size 100 int i;
Void sort(int SUHAIL[],int s);//fun prototype
void main()
{
int SUHAIL[size],s;
printf("\n No. of elements store in array=");
scanf("%d",&s);
printf("enter the %d elements=\n",s);
for(i=0;i<s;i++)
{
scanf("%d",&SUHAIL[i]);
}
sort(SUHAIL,s);//fun calling
}
void sort(int SUHAIL[],int s)// fun defination
{ int
j,c;

for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)

8
{
if(SUHAIL[i]>SUHAIL[j])
{
c=SUHAIL[i]; SUHAIL[i]=
[j];
SUHAIL[j]=c;
}
}
}
printf("the array in ascending order=\n");
for(i=0;i<s;i++)
{

printf("%4d,",SUHAIL[i]);
}
}
Output:
No. of elements store in array=5 enter the
5 elements=
23
56
11

99 1 the array in ascending


order=
1, 11, 23, 56, 99,
9
6. Wap in c Lang input N element in array & find the position of
given elements(binary search).
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,s, SUHAIL [s], search, first, last, middle;
printf("Enter the size of array "); scanf("%d" , &s);
printf("Enter elements (in ascending order): ");
for(i=0; i<s; i++) {

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

printf("\nEnter element to be search: ");


scanf("%d", &search); first = 0;

last = s-1; middle =


(first+last)/2;
while
(first <= last)

10
{

if(SUHAIL
[middle]<search) first = middle+1;
else if(SUHAIL [middle]==search)
{
printf("\nThe number, %d found at Position %d",
search, middle+1); break;

}
else last = middle-1;
middle = (first+last)/2;
}

if(first>last)
printf("\nThe number, %d is not found in given Array",
search);
getch();
return 0;
}
Output:
Enter the size of array 6
Enter elements (in ascending order): 55 66 77 88 99 199
Enter element to be search: 77

The number, 77 found at Position 3

7. Wap in c Lang input n*n matrix & print transpose matrix.


11
Code:
#include<stdio.h>
#include<conio.h>

#define row 10
#define column 10
void main()
{
int SUHAIL [row][column],r,c,i,j;

printf("enter the no of row and column first matrix=");


scanf("%d%d",&r,&c);
printf("enter the matrix %d elements",r*c);
for(i=0;i<r;i++)
{

for(j=0;j<c;j++)
{
printf("\n the cordinate [%d,%d]=",i,j);
scanf("%d",&SUHAIL [i][j]);
}
}
printf("\n the given matrix is:-\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d",SUHAIL [i][j]);

12
} printf("\n");
}
printf("\n the transpose of matrix :-\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%4d", SUHAIL [j][i]);
}
printf("\n");

}
Output:
enter the no of row and column first matrix=2 2 enter the
matrix 4 elements
the cordinate [0,0]=1

the cordinate [0,1]=2

the cordinate [1,0]=3

the cordinate [1,1]=4

the given matrix is:-


12
34
13
the transpose of matrix :-
1 3
2 4

8. Addition two n*n matrix.


Code:
#include<stdio.h>
#include<conio.h>

#define row 10
#define col 10
void main()
{
int SUHAIL[row][col],verma [row][col],SUHAIL
[row][col],row1, col1,row2,col2,i,j;

printf("enter the no of row and column first matrix="); scanf("%d


%d",&row1,&col1); printf("enter the no of row and column
second matrix=");
scanf("%d%d",&row2,&col2); if(row1!=row2 || col1!=col2)
{
printf("Order is not same so addition is not same"); exit(0);
}

14
printf("Enter the first matrix %d elements",row1*col1);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\n the coordinate [%d,%d]=",i,j); scanf("%d",&SUHAIL[i][j]);
}
}
printf("enter the second matrix %d elements",row2*col2);
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\n the coordinate[%d,%d]=",i,j); scanf("%d",&verma [i]
[j]);
}
}
printf("\n the first matrix elements\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%4d",SUHAIL[i][j]);

}
printf("\n");

15
printf("\n the second matrix elements\n");
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{ printf("%4d", [i][j]);
} printf("\n");
}
printf("\n the addition of two matrix elements\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
SUHAIL[i][j]=SUHAIL[i][j]+ [i][j];
printf("%4d",SUHAIL [i][j]);
} printf("\n");
}
}
Output:
enter the no of row and column first matrix=2 2 enter the no
of row and column second matrix=2 2
Enter the first matrix 4 elements

the coordinate [0,0]=4


the coordinate [0,1]=8
the coordinate [1,0]=1
the coordinate [1,1]=3
enter the second matrix 4 elements
the coordinate[0,0]=6
the coordinate[0,1]=9
16
the coordinate[1,0]=7

the coordinate[1,1]=4

the first matrix elements


4 8
1 3

the second matrix elements


6 4
4 9

the addition of two matrix elements

10 17
8 7

9. Multiplication two n*n matrix.


Code:
# include<stdio.h>
#include<conio.h>

#define row 10
#define col 10
void main()
{

17
int SUHAIL[row][col], varma [row][col],SUHAIL
[row][col],row1, col1,row2,col2,i,j,k;

printf("enter the no of row and column first matrix="); scanf("%d


%d",&row1,&col1); printf("enter the no of row and column
second matrix=");
scanf("%d%d",&row2,&col2); if(row1!=row2 || col1!=col2)
{
printf("Order is not same so addition is not same"); exit(0);
}
printf("Enter the first matrix %d elements",row1*col1);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\n the coordinate [%d,%d]=",i,j); scanf("%d",&SUHAIL[i][j]);
}
}
printf("enter the second matrix %d elements",row2*col2);
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\n the coordinate[%d,%d]=",i,j);
scanf("%d",&verma [i][j]);
}
}
printf("\n the first matrix elements\n");

18
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("%4d",SUHAIL[i][j]);
} printf("\n");
}
printf("\n the second matrix elements\n"); for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{ printf("%4d", varma [i][j]);
} printf("\n");
}
printf("\n the multiplication of two matrix elements\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
SUHAIL
[i][j]=0;
for(k=0;k<col1;k++)
{
SUHAIL [i][j]=SUHAIL [i][j]+SUHAIL[i][k]* varma
[k][j]; }
printf("%4d",SUHAIL [i][j]);

}
printf("\n");

19
}

}
Output:
enter the no of row and column first matrix=2 2
enter the no of row and column second matrix=2 2
Enter the first matrix 4 elements the
coordinate [0,0]=2
the coordinate [0,1]=4
the coordinate [1,0]=6

the coordinate [1,1]=8


enter the second matrix 4 elements
the coordinate[0,0]=1
the coordinate[0,1]=3

the coordinate[1,0]=5

the coordinate[1,1]=7

the first matrix elements


24
68

the second matrix elements


1
5
the multiplication of two matrix elements 17
20
22 Error! Bookmark not defined.
46 Error! Bookmark not defined.

10. Wap in c Lang print 5*5 matrix as follows upper diagonal


elements with ‘+1”,lower diagonal with ‘-1’ and diagonal elements
with ‘o’.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int SUHAIL [5][5],i,j;
for(i=0;i<5;i++)
{

for(j=0;j<5;j++)
{ if(i>j)
{
printf("%4d",-1);
} if(i<j)
{
printf("%4d",+1);
} if(i==j)
{
printf("%4d",0);
}
} printf("\n");

21
}}
Output:

0111 1
-1 0 1 1 1
-1 -1 0 1 1
-1 -1 -1 0 1
-1 -1 -1 -1 0

11. Wap in c Lang to read a Matrix and find out the max element and
its position in the matrix.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int SUHAIL[10][10],i,j,k,max,row,col;
printf("enter the no of row and colume for the matrix ");
scanf("%d%d",&row,&col);
printf("enter the matrix elements=%d",row*col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&SUHAIL[i][j]);
}
22
}
max=SUHAIL[0][0];

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{

if( SUHAIL[i][j]>max)
{
max=SUHAIL[i][j];
}}}
printf("the max elements in given matrix=%d",max);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(max==SUHAIL[i][j])
printf("the elements found at position=[%d,%d]",i+1,j+1);
break;
}
}
}
Output:
enter the no of row and colume for the matrix 2 2 enter the
matrix elements=4
23

23
56
78
99
the max elements in given matrix=99

12 WAP in Lang swap two no with call by reference’s.


Code:
#include<stdio.h> #include<conio.h>
int swap(int *, int *); void
main()
{ int a,b; printf("Enter the value of a and b="); scanf("%d
%d",&a,&b); printf("The value of a and b before swapping a=
%dand
b=%d",a,b);
printf("\n");
swap(&a,&b); printf("The value of a and b after swapping a=
%dand
b=%d",a,b);
printf("\n");
}
int swap(int *a,int *b)
{ int c;
c=*a;
*a=*b;
*b=c;

24
printf("The value of a and b Inner fun calling a=%dand
b=%d",*a,*b);
printf("\n");
}
Output:
Enter the value of a and b=45 100
The value of a and b before swapping a=45and b=100 The value
of a and b Inner fun calling a=100and b=45
The value of a and b after swapping a=100and b=45

13. Wap in c Lang input n elements in array with calloc function.


Code:
#include<stdlib.h>
int main() # include<stdio.h>

{
int *p, i, n;

printf("Enter the size of the array: ");


scanf("%d", &n);

p = (int*)calloc(n, sizeof(int));
if(p==NULL)
{
printf("Memory allocation failed"); exit(1);
}

25
for(i = 0; i < n; i++)
{
printf("Enter %d element: ", i);
scanf("%d", p+i);
}

printf("\n THE ARRAY WILL BE::\n\n", n);


for(i = 0; i < n; i++)
{ printf("%d ", *(p+i));
}
return 0;
}
Output:
Enter the size of the array: 4
Enter 0 element: 1
Enter 1 element: 3
Enter 2 element: 5
Enter 3 element: 7

THE ARRAY WILL BE::

1357

14. Wap in c Lang input any string with malloc & change size with
realloc function.
Code:
#include<stdio.h>
#include<conio.h>
26
#include<string.h>
void main()
{
char *str; str=(char *)malloc(30*sizeof(char));
strcpy(str,"SUHAIL"); printf("the string %s store at
address=%u",str,str); str=(char *) realloc(str,90); printf("\
n");
printf("Enter the string after reallocating its size=");
scanf("%s",str); printf("\n the string %s store at address=
%u",str,str); free(str);
}
Output:
the string SUHAIL store at address=12260368
Enter the string after reallocating its size=SUHAIL
the string SUHAIL store at address=12260368
15. Wap in C Lang count vowel char given string.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int c=0,i,l; char str[20];
printf("Enter the string=");
gets(str); l=strlen(str);
For(i=0;i<l;i++)
{
If(srt==[i]’á’|str==[i]’e’|

27
for(i=0;i<l;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'|| str[i]=='u')

c++;
}
printf("The vowel character given string=%d",c); } Output:
Enter the string=egg
The vowel character given string=1

16. Wap to count the no of words and length of given string.


Code:
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main()
{

char s[200]; int


count = 0, i,l;
printf("Enter the
string:\n");
scanf("%[^\n]s",
s); for (i = 0;s[i] !=
'\0';i++)

28
{

if (s[i] == ' ' && s[i+1] != ' ') count++;


}
printf("Number of words in given string are: %d\n",
count + 1);

l=strlen(s); printf("\n length of given string =%d",l-


count);
}
Output:
Enter the string:
SUHAIL
Number of words in given string are: 2

length of given string =10

17. Wap in C Lang input any string and print it length with user
define function & passing argument.
Code:
#include<stdio.h>
#include<conio.h>
#define size 100
Void main ()
int i; int strlen(char *str); //fun prototype
{

29
char str[size];

printf("Enter any string for calculating its length =");


gets(str);
printf("The length of string=%d",strlen(str));
} int strlen(char *str)
{ int l=0; for(i=0;str[i]!='\
0';i++)
{ l++;
} return(l);

30
}
Output:
Enter any string for calculating its length =SUHAIL The length
of string=11

18. Wap in C Lang copy one string two another string with user
define function & passing argument.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 100
Void main()
int i;
void strcopy(char *SUHAIL); //fun prototype
{
char SUHAIL[size];
printf("Enter any string =\n");
gets(SUHAIL);

strcopy(SUHAIL); //fun calling


}
void strcopy(char *SUHAIL) //fun defination

{
31
char
SUHAILcopy[size];
int l=0,p=0;
printf("the first
string=%s",SUHAIL);
for(i=0;
SUHAIL[i]!='\0';i++)
{ l++; }
for(i=0;i<=l;i++)
{
SUHAILcopy[p]=SUHAIL[i]; p++;
}
printf("\nthe second new string=%s",SUHAILcopy);
}
Output:
Enter any string = SUHAIL the first
string=SUHAIL the second new
string=SUHAIL

32
19. Wap in C Lang compare two string with user define function &
passing argument.
Code:
#include<stdio.h>
#include<conio.h>
#define size 100
int i;
void main ()
void strcompare(char
*SUHAIL);
{
char SUHAIL[size];
printf("enter the first string =");
gets(SUHAIL);
strcompare(SUHAIL);
}
void strcompare(char *SUHAIL)
{
char newSUHAIL[size];
int j,f=0; printf("The first
string=%s",SUHAIL);
printf("\n");
printf("Enter the second string=");
gets(newSUHAIL);
for(i=0,j=0;SUHAIL[i]!='\0',newSUHAIL[j]!='\
0';i++,j+)
{
if (SUHAIL[i]!=newSUHAIL[j])

33
{ f=1;
break;
}
} if(f==0)
printf("\n Both string are same\n");
else
printf("\n Both string are not same");
}
Output:
enter the first string =SUHAIL
The first string=SUHAIL
Enter the second
string=SUHAIL Both string
are same

34
20. Wap in C Lang concatenation two string with user define
function & passing argument.

Code:
#include<stdio.h>
#include<conio.h>
#define size 100
int i;
void strconcatenat(char *SUHAIL1); //fun prototype void
main()
{
char SUHAIL1[size];
printf("enter the first string =");
gets(SUHAIL1);
strconcatenat(SUHAIL1); //fun calling
}
void strconcatenat(char *SUHAIL1)//fun defination
{
char
SUHAIL2[size],newstr[size]; int j;
printf("the first
string=%s",SUHAIL1);
printf("\nEnter the second string=");
gets(SUHAIL2);
for(i=0;SUHAIL1[i]!='\0';i++)

35
{
newstr[i]=SUHAIL1[i];
}
for(j=0;SUHAIL2[j]!='\0';j++)
{
newstr[i+j] =SUHAIL2[j];
} newstr[i+j]='\0';
printf("the new concatenat two string=%s",newstr);
}
Output:
enter the first string =sd the
first string=sd
Enter the second string=management the new
concatenat two string=sdmanagement

36
21. Wap in c Lang to read a line of text and print the no of
occurrences of a given substring.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h> void
main()
{
char a[]="SD management SD college of pharmacy SD group
of college SD college of law",c[10]="SD"; int l,cu=0;
l=strlen(c); while(1)
{
if(strstr(a,c)==NULL)
break;
strcpy(a,strstr(a,c));
strcpy(a,a+l); cu++;
}
printf("the sub string found occurance=%d",cu);
}
Output: the sub string found
occurance=4

37
22. wap in c lang input any string reverse all the word.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char
SUHAILstr[100],rev[100]; int i,j=0;
printf("Enter any string=");
gets(SUHAILstr);
for(i=0;SUHAILstr[i]!='\0'; i++)
{
if(SUHAILstr[i]!=' ')
{
rev[j]=SUHAIL
str[i]; j++;
}
else
{
SUHAILstr[j]='\0';
printf("%s",strrev(rev));
printf(" "); j=0;
} } rev[j]='\0';
printf("%s",strrev(rev));
}
Output:

38
Enter any string=myself flesym

23. Wap in c lang Input any string sorting char by char.


Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char SUHAIL[128],
temp; int n, i, j;
printf("\nEnter string for sorting: ");

gets(SUHAIL); n =
strlen(SUHAIL); for
(i=0; i<=n; i++)
{ for (j=i+1; j<n; j++)
{
if (SUHAIL[i] > SUHAIL[j])
{
temp = SUHAIL[i];
SUHAIL[i] =
SUHAIL[j]; SUHAIL[j]
= temp;
39
}
}}
printf("\n%s", SUHAIL);
printf("\n");

}
Output:
Enter string for sorting: edcba

abcde

24. Wap in c lang input multiple string and print it ascending


order. Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int
i,j,n;
char SUHAIL[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(SUHAIL[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
40
if(strcmp(SUHAIL[i],SUHAIL[j])>0)
{
strcpy(temp,SUHAIL[
i]);
strcpy(SUHAIL[i],SUHAIL[j]);
strcpy(SUHAIL[j],tem
p);
}
}

41
printf("The sorted string\n");
for(i=0;i<=n;i++) puts(SUHAIL[i]);
}
Output:
Enter the no. of string to be sorted
3 zebra
mango
apple
The sorted string

apple mango
zebra

25. Wap in c lang Bank structure perform operation


1. add customer 2.display all record 3.deposite amount 4. Withdraw
amount 5. Search record and 6. Exit . with in which following items
are taken in the structure a/c no, Name, father name, city, a/c
type ,and amount where maximum costumer are 10.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct bank
{
int acno,amount; char
name[12],fname[20],city[12],type[10];

42
}o[10]; void
main()
{
int ch,i,c,n,sacno,damount,wamount; do
{

printf("\n====================**************************===
=================\n");
printf("\n Press 1 to ADD CUSTOMER");
printf("\n Press 2 to DISPLAY ALL THE RECORDS");
printf("\n Press 3 to DEPOSITE AMOUNT");
printf("\n Press 4 to WITHDRAW AMOUNT");
printf("\n Press 5 to SEARCH RECORDS");
printf("\n Press 6 to EXIT");
printf("\n");
printf("\n");
printf("\n====================**************************===
=================\n");
printf("\n ENTER YOUR CHOICE=");
scanf("%d",&ch); switch(ch)
{
case 1:
printf("Enter the number of customer to be added=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Account Number=");

43
scanf("%d",&o[i].acno);
printf("Enter the Account Holder Name=");

fflush(stdin);
scanf("%s",o[i].name);
printf("Enter the Account's Holder Father's name=");
fflush(stdin);
scanf("%s",o[i].fname);
printf("Enter the City Name=");
fflush(stdin);
scanf("%s",o[i].city);
printf("Enter the Account Type(Savings/Current/Fixed
Deposite)=");
fflush(stdin);
scanf("%s",o[i].type);
printf("Enter the Starting Amount=");
fflush(stdin);
scanf("%d",&o[i].amount);
}
break; case 2:
printf("\n THE CUSTOMER INFORMATION : \n");
for(i=0;i<n;i++)
{
printf("\n The Account no. is =%d",o[i].acno);
printf("\n The Account Holder name =%s",o[i].name);
printf("\n The Account Holder's father name
=%s",o[i].fname);

44
printf("\n The City name =%s",o[i].city);
printf("\n The Account Type =%s",o[i].type);
printf("\n The Amount =%d",o[i].amount);
}
break;

case 3:
printf("enter the accno no for deposited amount ");
scanf( "%d",&sacno);
for(i=0;i<n;i++)
{
if(o[i].acno==sacno)
{
printf("enter the amount");
scanf("%d",&damount);
o[i].amount=o[i].amount+damount;
}
}
printf("the successfully deposited amount");
break; case 4:
printf("enter the accno no for withdrow amount");
scanf( "%d",&sacno);
for(i=0;i<n;i++)
{
if(o[i].acno==sacno)

45
{
printf("enter the withdrow amount");
scanf("%d",&wamount); if(wamount>o[i].amount)
{

printf("the balance is insufficient");


getch();
}
else
{
o[i].amount=o[i].amount-wamount;
printf("the successfully withdraw amount");
}
}
}
break; case 5:
printf("enter the search ac no");
scanf("%d",&sacno);
printf("\n THE CUSTOMER INFORMATION : \n");
for(i=0;i<n;i++)
{
if(o[i].acno==sacno)

{
printf("\n The Account no. is =%d",o[i].acno);
printf("\n The Account Holder name =%s",o[i].name);
printf("\n The Account Holder's father name

46
=%s",o[i].fname);
printf("\n The City name =%s",o[i].city);
printf("\n The Account Type =%s",o[i].type);

printf("\n The Amount =%d",o[i].amount);


}
}
break; case
6:exit(0);
}
printf("\n If you want to continue then press 1 otherwise
press any number :-)");
scanf("%d",&c);
}
while(c==1); getch();
}
Output:
====================**************************==========
==========

Press 1 to ADD CUSTOMER


Press 2 to DISPLAY ALL THE RECORDS
Press 3 to DEPOSITE AMOUNT
Press 4 to WITHDRAW AMOUNT
Press 5 to SEARCH RECORDS
Press 6 to EXIT

47
====================**************************==========
==========

ENTER YOUR CHOICE=1


Enter the number of customer to be added=1
Enter the Account Number=1234567
Enter the Account Holder Name=SUHAIL
Enter the Account's Holder Father's name=sanjay
Enter the City Name=muzaffarnagar
Enter the Account Type(Savings/Current/Fixed
Deposite)=savings
Enter the Starting Amount=10000

If you want to continue then press 1 otherwise press any


number :-)1

====================**************************==========
==========

Press 1 to ADD CUSTOMER


Press 2 to DISPLAY ALL THE RECORDS
Press 3 to DEPOSITE AMOUNT
Press 4 to WITHDRAW AMOUNT

48
Press 5 to SEARCH RECORDS
Press 6 to EXIT

====================**************************==========
==========

ENTER YOUR CHOICE=3


enter the accno no for deposited amount 5000 the
successfully deposited amount
If you want to continue then press 1 otherwise press any
number :-)4

26. Wap of student(structure) directory with nested structure & print


student info.& percentage.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{ int rollno; char
studentname[20],course[20];

49
struct position
{ float
m,s,c,h,e,t,p; }o2;//
inner object
}o1;//outer object
void main()
{
printf(" Enter the Roll no. of student=");
scanf("%d",&o1.rollno);
printf(" Enter the name of student=");
fflush(stdin);
gets(o1.studentname);
printf(" Enter the course of student=");
fflush(stdin); gets(o1.course);
printf(" Enter the marks of all 5 subject marks=");

scanf("%f%f%f%f
%f",&o1.o2.m,&o1.o2.s,&o1.o2.c,&o1.o2.h ,&o1.o2.e);

o1.o2.t=o1.o2.m+o1.o2.s+o1.o2.s+o1.o2.c+o1.o2.h+o1.o2.e;
o1.o2.p=(o1.o2.t*100)/500; printf("\n\
n************************************************");
printf("\n The Roll no. of student =%d",o1.rollno);
printf("\n The name of student =%s",o1.studentname);
printf("\n The course name of student =%s",o1.course);
printf("\n The total marks of student =%f",o1.o2.t);
printf("\n The total percentage of student =%f",o1.o2.p);
printf("\n\n************************************************"); }

50
Output:
Enter the Roll no. of student=185 Enter
the name of student=SUHAIL Enter the
course of student=B.C.A
Enter the marks of all 5 subject marks=80 70 85 90 85

************************************************
The Roll no. of student =185
The name of student =SUHAIL
The course name of student =B.C.A
The total marks of student =480.000000
The total percentage of student =96.000000

************************************************

51
27. Wap in c Lang to used the bitwise operator.
Code:
#include<stdio.h> void
main()
{ int a,b,choice;
do
{ printf("\n\n********** MENU **********"); printf("\n Press
1 to calculate Bitwise AND operator:");

printf("\n Press 2 to calculate Bitwise OR operator:");


printf("\n Press 3 to calculate Bitwise exclusive
OR operator:");
printf("\n Press 4 to calculate Bitwise complement operator:");
printf("\n Press 5 to calculate Bitwise shift
operators:"); printf("\n Press 6 for exit"); printf("\n\n
Enter your choice:");

scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the value of a and b=");
scanf("%d%d",&a,&b);
printf("The output of the Bitwise AND operator
a&b is %d",a&b); break;
case 2:
printf("\n Enter the value of a and b=");
scanf("%d%d",&a,&b);

52
printf("The output of the Bitwise OR operator
a&b is %d",a|b); break;
case 3:

printf("\n Enter the value of a and b =");


scanf("%d%d",&a,&b);
printf("The output of the Bitwise exclusive OR
operator a^b is %d",a^b);
break;
case 4:
printf("\n Enter the value of a =");
scanf("%d",&a);
printf("The output of the Bitwise complement
operator ~a is %d",~a);
break;
case 5:
printf("\n Enter the value of a =");
scanf("%d",&a);

printf("\nThe value of a<<2 (left shift) is : %d ",


a<<2);

printf("\n Enter the value of b =");


scanf("%d",&b);
53
printf("\nThe value of a>>2 (right shift) is : %d ", break;

b>>2);

case 6:
printf("Goodbye\n");
break;
default:
printf("Wrong Choice. Enter again\n");
break;
} }while(choice!=6);
}
Output:

********** MENU **********


Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:
Press 3 to calculate Bitwise exclusive OR operator:
Press 4 to calculate Bitwise complement operator:

Press 5 to calculate Bitwise shift operators: Press 6 for


exit

54
Enter your choice:1

Enter the value of a and b=6 4


The output of the Bitwise AND operator a&b is 4

********** MENU **********


Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:
Press 3 to calculate Bitwise exclusive OR operator:
Press 4 to calculate Bitwise complement operator:
Press 5 to calculate Bitwise shift operators:
Press 6 for exit

Enter your choice:2

Enter the value of a and b=23 10


The output of the Bitwise OR operator a&b is 31

********** MENU **********


Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:
Press 3 to calculate Bitwise exclusive OR operator:
55
Press 4 to calculate Bitwise complement operator:
Press 5 to calculate Bitwise shift operators:
Press 6 for exit

Enter your choice:3

Enter the value of a and b =12 10


The output of the Bitwise exclusive OR operator a^b is 6

********** MENU **********


Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:
Press 3 to calculate Bitwise exclusive OR operator:
Press 4 to calculate Bitwise complement operator:
Press 5 to calculate Bitwise shift operators:
Press 6 for exit

Enter your choice:4

Enter the value of a =8


56
The output of the Bitwise complement operator ~a is -9

********** MENU **********


Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:

Press 3 to calculate Bitwise exclusive OR operator:


Press 4 to calculate Bitwise complement operator:
Press 5 to calculate Bitwise shift operators:
Press 6 for exit

Enter your choice:5

Enter the value of a =5

The value of a<<2 (left shift) is : 20


Enter the value of b =7

The value of a>>2 (right shift) is : 1

57
********** MENU **********
Press 1 to calculate Bitwise AND operator:
Press 2 to calculate Bitwise OR operator:
Press 3 to calculate Bitwise exclusive OR operator:
Press 4 to calculate Bitwise complement operator:
Press 5 to calculate Bitwise shift operators:
Press 6 for exit

Enter your choice:6 Goodbye

28. Wap to read data from keyboard & write to a file “input” again
read the same data from the input file and display on the string.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp; char c;
fp=fopen("input.txt","w");
printf("\n data input=");
While
((c=getchar())!= EOF)
{
putc(c,fp);
}
fclose(fp);

58
fp=fopen("input.txt","r");
printf("\nData stored in the file input.txt is as follows\n");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}

fclose(fp);
}
Output:

data input=SUHAIL
^Z

Data stored in the file input.txt is as follows


SUHAIL

29. Wap in C Lang count char, space, tabs and new lines in a file.
Code:
#include<stdio.h>
#include<conio.h> void
main()
{

59
FILE *fp; char ch , fname[40]; int
nol=0,not=0,nos=0,noc=0;
printf("Enter file name : ");
gets(fname);
fp = fopen(fname, "r");

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break; noc++;
if(ch==' ') nos++;
if(ch=='\n') nol++;
if(ch=='\t') not++;

}
fclose(fp); printf("\n Number of characters =
%d",noc); printf("\n Number of blanks =
%d",nos); printf("\n Number of tabs = %d",not);
printf("\n Number of lines = %d",nol);

}
Output:
Enter file name : file29.txt

60
Number of characters = 90
Number of blanks = 12
Number of tabs = 0
Number of lines = 7

30. Wap in c lang compare two text file.


Code:
#include<stdio.h>
#include<conio.h> void
main()
{
FILE *fp1, *fp2; int c1, c2; char
fname1[40], fname2[40];
printf("Enter name of first file :");
gets(fname1);
printf("Enter name of second file:");
gets(fname2); fp1 = fopen(fname1, "r");
fp2 = fopen(fname2, "r");

if(fp1==NULL)
{
printf("first file does not exist in the system");
getch(); exit(0);
}
if(fp2==NULL)
{
printf("second file does not exist in the system");

61
getch();
exit(0);
}
c1 = getc(fp1);
c2 = getc(fp2);
while ((c1 != EOF) && (c2 != EOF) && (c1 == c2))
{

c1 = getc(fp1); c2 =
getc(fp2);
} if (c1 == c2)
printf("Yesss !!!! Files are identical");
else
printf("Noooo !!!! Files are Not identical");
fclose(fp1);
fclose(fp2);
getch();
}
Output:
First text file is SUHAIL.txt which contain text is Okay
Second text file is varma .txt which contain text is Okay

Enter name of first file


:SUHAIL.txt Enter name of second
file:verma.txt Yesss !!!! Files are identical

62
31. Wap in c lang count no of word in text file.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{ char ch;

FILE *file; int count = 0; file =


fopen("file29.txt","r"); while((ch =
fgetc(file)) != EOF){
//Counts each word
if(ch ==' ' || ch == '\n') count++;
}

printf("Number of words present in given file: %d",


count); fclose(file);

return 0;
}

Output:

63
Number of words present in given file: 6

32. Wap in c Lang sum n no with command Line Argument.


Code:
#include<stdio.h>
#include<stdlib.h>
int main()
int argc,char *argv[])
{
int sum=0,i; if(argc<=1)
{
printf("\n Enter number of arguments to calculate sum
\n \n");
exit(0);
}
else
{
for(i=1;i<argc;i++)
{

sum+=atoi(argv[i]);
}
}
printf("\n Sum of all command line arguments is %d \n
\n",sum);
}
Output:

64
Enter number of arguments to calculate sum
10 20 30
Sum of all command line arguments is 60

33. Wap in c Lang input any string and update with Fseek function.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp; fp=fopen("SUHAIL.c",
"w"); if(fp==NULL)
{
printf("this file does not exit");

exit(0);
}
fputs(" Heyy!!! myself SUHAIL",fp);
fseek(fp,11,0);
fputs("sam",fp);
printf("Text updated successfully");
fclose(fp);
getch();
}
Output:
Text updated successfully

65
34. Wap in C Lang swap two no with used Macro.
Code:
#include<stdio.h>
#include<conio.h>
#define SWAP c=a
Void main ()
a=b, b=c;
{ int a,b,c;
printf("Enter the value of a and b before swapping=");
scanf("%d%d",&a,&b);
SWAP
printf("The value a and b after swapping a=%d & b=%d",a,b);
}
Output:
Enter the value of a and b before swapping=100 400 The value a
and b after swapping a=400 & b=100

END OF PROGRAM

66

You might also like