You are on page 1of 24

ARRAY Declaration of Array:int arr[4] = {10,5,6,1}; string names[] = {"Peter","Alice","James"}; int numbers[] = {1,2,3,4,5,6,7,8}; char name[]=INDIA; char name[6]={I,N,D,I,A};

int arr[3][3]; 117. Write a program for read and write an one dimensional integer Array. # include<stdio.h> # include<conio.h> //#define N 10 //const N = 10 void main ( ) { int i,x[10]; //int i,x[N]; for (i=0;i<10;i++) //for(i=0;i<N;i++) { printf("Enter the Number : "); scanf("%d",&x[i]); } for (i=0;i<10;i++) //for(i=0;i<N;i++) printf("%4d",x[i]); getch ( ); }

4) Write a program to insert a given number in the array in a given position. #include <stdio.h> #include <conio.h> void main() { int a[5],i,j=5,k,val,pos; clrscr(); for(i=0;i<5;i++) { printf("\nENTER NUMBER-%d: ",i+1); scanf("%d",&a[i]); } printf("\nENTER VALUE TO INSERT: "); scanf("%d",&val); printf("\nENTER POSITION TO INSERT: "); scanf("%d",&pos); j++; for(k=j;k>=pos;k--) { a[k]=a[k-1]; } a[--pos]=val; 1

printf("\nTHE ARRAY IS: "); for(i=0;i<j;i++) { printf("\n%d",a[i]); } getch(); } 3) Write a program to create an array. Print the highest and lowest number in the array. #include <stdio.h> #include <conio.h> void main() { int val[5],h,l,i; clrscr(); for(i=0;i<5;i++) { printf("\nENTER VALUE-%d: ",i+1); scanf("%d",&val[i]); } l=val[0]; h=val[0]; for(i=0;i<5;i++) { if(val[i]>h) h=val[i]; else { if(val[i]<l) l=val[i]; } } printf("\nHIGHEST VALUE IS %d",h); printf("\nLOWEST VALUE IS %d",l); getch(); } ) Write a program to convert a decimal number to its equivalent binary number. #include <stdio.h> #include <conio.h> void main() { unsigned long dec; int a[25],c=0,i; clrscr(); printf("\nENTER A DECIMAL NUMBER: "); scanf("%lu",&dec); printf("\n%lu IN BINARY FORMAT: ",dec); 2

while(dec>0) { a[c]=dec%2; dec=dec/2; c++; } for(i=c-1;i>=0;i--) printf("%d",a[i]); getch(); } Write a program to convert a decimal number to its equivalent hexadecimal number. #include <stdio.h> #include <conio.h> void main() { unsigned long dec; int a[25],c=0,i; clrscr(); printf("\nENTER A DECIMAL NUMBER: "); scanf("%lu",&dec); printf("\n%lu IN HEXADECIMAL FORMAT: ",dec); while(dec>0) { a[c]=dec%16; dec=dec/16; c++; } for(i=c-1;i>=0;i--) { if(a[i]>=10) printf("%c",a[i]+55); else printf("%d",a[i]); } getch(); } Write a program to convert a binary number to its equivalent decimal number. #include <stdio.h> #include <conio.h> void main() { unsigned long num; int digit,i,pos=0,pow=1,dec=0; clrscr(); printf("\nENTER A BINARY NUMBER: "); scanf("%lu",&num); printf("\nDECIMAL EQUIVALANT OF %lu IS ",num); while(num!=0) 3

{ pow=1; digit=num%10; num=num/10; for(i=1;i<=pos;i++) pow=pow*2; pos++; dec=dec+(pow*digit); } printf("%d",dec); getch(); }

118

Add two Integer arrays. #include<stdio.h> #include<conio.h> void main ( ) { int i,a[5],b[5],c[5]; printf("Reading First Array.....\n"); for(i=0;i<5;i++) { printf("\nEnter the Value : "); scanf("%d",&a[i]); } printf("\n\nReading Second Array.....\n"); for(i=0;i<5;i++) { printf("\nEnter the Value : "); scanf("%d",&b[i]); } printf("\n\nSum(a[i] + b[i]) ="); for(i=0;i<5;i++) { c[i]=a[i]+b[i]; printf("%4d",c[i]); } getch( ); }

119.

Find out Maximum of an array.

#include<stdio.h> #include<conio.h> void main( ) { int a[10],n,max=0,i; printf("How many Values to Enter (1-10) :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the %d value :",i+1); scanf("%d",&a[i]); } max = a[1]; for(i=0;i<n;i++) { if(a[i]>max) max=a[i]; } printf("Maximum Value = %d",max); getch( ); } 120. Program for sorting through Bubble Sort or Selection Sort. #include<stdio.h> #include<conio.h> void main( ) { int a[10],n,i,j,t,max,imax; printf("\nHow many values to insert (1-10) :"); scanf("%d",&n); printf("\n\n"); /* INSERT AN ARRAY */

for(i=0;i<n;i++) scanf("%d",&a[i]); /* SORTING BY BUBBLE SORT */

for(i=1;i<n;i++) for(j=0;j<n-i;j++) if(a[j] > a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } /* /* SORTING BY SELECTION SORT for(i=0;i<n-1;i++) { max = a[0]; imax = 0; for(j=1;j<n-i;j++)

*/

if(max < a[j]) { max = a[j]; imax = j; } a[imax] = a[n-i-1]; a[n-i-1] = max; } */ /* PRINTING OF SORTED ARRAY */

printf("\n\n\t"); for(i=0;i<n;i++) printf(" %d ",a[i]); getch( ); } 121. Program for Binary search. #include<stdio.h> #include<conio.h> void main( ) { int a[10],n,i,j,t,s,top,bottom,mid; printf("\nenter the no. of values you want to insert (1-10) :"); scanf("%d",&n); printf("\n\n"); /* INSERT AN ARRAY */

for(i=0;i<n;i++) scanf("%d",&a[i]); /* SORTING BY BUBBLE SORT */

for(i=1;i<n;i++) for(j=0;j<n-i;j++) if(a[j] > a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } /* PRINTING OF SORTED ARRAY */

printf("\n\n\t"); for(i=0;i<n;i++) printf(" %d ",a[i]); printf("\n\n\n\nEnter the no. you want to search.:"); scanf("%d",&s); /* SEARCHING No. BY BINARY SEARCH */

top=0;

bottom=n-1; while(top <= bottom) { mid=(top+bottom)/2; if(s= =a[mid]) break; else if(a[mid] > s) bottom=mid-1; else top=mid+1; } if(top <= bottom) printf("\n\n\n\t\t\tFound at %d position.",mid+1); else printf("\n\n\n\t\t\t!Search not found"); getch( ); 122. } Program for Linear search and Replace. #include<stdio.h> #include<conio.h> void main( ) { int n,a[10],s,i,r,flag=0; printf("How many values to insert (1-10) :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter value :"); scanf("%d",&a[i]); } printf("\n\nEnter the value to search :"); scanf("%d",&s); for(i=0;i<n;i++) if(s= =a[i]) { printf("\n\n\tThe value %d found on %d position ",s,i+1); printf("\n\nReplace %d with : ",s); scanf("%d",&r); a[i] = r; flag += 1; } if(flag= =0) printf("\n\tValue not found"); printf("\n\nNow the new Array is :- "); for(i=0;i<n;i++) printf("%5d",a[i]); getch( ); }

Program to access array elements with pointer. void main() { int i,a[10],*p; p=&a[i]; clrscr(); printf("\n Enter Array Elements:-"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } printf("\n Value:-%d",*p); printf("\n You have Entered:-"); for(i=0;i<5;i++) { printf("\n %d",*(p+i)); } getch(); } Program to access 2D array elements with pointer. With a two-dimensional array, the compiler needs to know how many elements are in a column to move from one row to the next, but movement within a row is always by a single element, so the compiler doesn't need to know the number of rows. Thus the pointer declaration must include the number of columns in the array: void main() { int i,j,a[3][3],(*p)[3]; p=a; clrscr(); printf("\n Enter Array Elements:-"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } } printf("\n You have Entered:-"); for(i=0;i<3;i++) { printf("\n\n"); for(j=0;j<3;j++) { printf("\t\t %d",*(*(p+i)+j)); } } getch(); } 8

11. Program to access array elements using pointer.

22. Passing entire array to a function.

33. PASSING array to a function #include <stdio.h> #include<conio.h> int maximum( int [] );

/* ANSI function prototype */

int maximum( int values[5] ) { int max_value, i;

10

max_value = values[0]; for( i = 0; i < 5; ++i ) if( values[i] > max_value ) max_value = values[i]; return max_value; } Void main() { int values[5], i, max; printf("Enter 5 numbers\n"); for( i = 0; i < 5; ++i ) scanf("%d", &values[i] ); max = maximum( values ); printf("\nMaximum value is %d\n", max ); getch(); }

123.

Program for Read and Print 2D array (Matrix) #include<stdio.h> #include<conio.h> void main( ) { int r,c,a[10][10],i,j; printf("Enter the no. of rows :"); scanf("%d",&r); printf("Enter the no. of columns :"); scanf("%d",&c); /* * * * * * READ ARRAY (Row Wise) * * * * * */ for(i=0;i<r;i++) for(j=0;j<c;j++)

11

{ printf("Row[%d] Column[%d] :",i,j); scanf("%d",&a[i][j]); } /* * * * * * * READ ARRAY (Column Wise) * * * * * for(i=0;i<c;i++) for(j=0;j<r;j++) { printf("Row[%d] Column[%d] :",j,i); scanf("%d",&a[j][i]); } */ /* * * * * * PRINT ARRAY * * * * * */ printf("\n\nNow the array is :-\n\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%4d",a[i][j]); } printf("\n"); } getch( ); } 124 Program for Addition of two Matrices. #include<stdio.h> #include<conio.h> void main( ) { int r,c,a[10][10],b[10][10],s[10][10],i,j,k,l; printf("Enter the no. of rows :"); scanf("%d",&r); printf("Enter the no. of columns :"); scanf("%d",&c); /* * * * * Read First Matrix * * * * */ printf("\n\nNow enter values for First Matrix A[%d][%d] :-\n\n",r,c); for(i=0;i<r;i++) for(j=0;j<c;j++) { printf("Row[%d] Column[%d] :",i,j); scanf("%d",&a[i][j]); } /* * * * * Read Second Matrix * * * * */

12

printf("\n\nNow enter the Second 2D array B[%d][%d] :-\n\n",r,c); for(i=0;i<r;i++) for(j=0;j<c;j++) { printf("Row[%d] Column[%d] :",i,j); scanf("%d",&b[i][j]); } /* * * * * Addition of Matrices * * * * */ for(i=0;i<r;i++) for(j=0;j<c;j++) s[i][j]=a[i][j] + b[i][j]; /* * * * * * Print all the Three Matrices * * * * * */ printf("\n\nNow the array is :-\n\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%4d",a[i][j]); printf("\t\t"); for(k=0;k<c;k++) printf("%4d",b[i][k]); printf("\t\t"); for(l=0;l<c;l++) printf("%4d",s[i][l]); printf("\n"); } getch( ); } 125 Program for Matrix Multiplication. #include<stdio.h> #include<conio.h> void main( ) { int m,n,p,a[10][10],b[10][10],c[10][10],i,j,k,l; printf("Enter the no. of rows for first Matrix :"); scanf("%d",&n); printf("Enter the no. of columns for first Matrix :"); scanf("%d",&m); /* READ FIRST MATRIX */ printf("\n\nNow enter the values for First Matrix A[%d][%d] :-\n\n",n,m); for(i=0;i<n;i++)

13

for(j=0;j<m;j++) { printf("Row[%d] Column[%d] :",i,j); scanf("%d",&a[i][j]); } /* READ SECOND MATRIX */ printf("\n\nEnter the no. of columns for second Matrix :"); scanf("%d",&p); printf("\n\nNow enter the values for Second Matrix B[%d][%d] :-\n\n",m,p); for(i=0;i<m;i++) for(j=0;j<p;j++) { printf("Row[%d] Column[%d] :",i,j); scanf("%d",&b[i][j]); } /* MULTIPLYING FIRST MATRIX WITH SECOND */ for(i=0;i<n;i++) for(j=0;j<p;j++) { c[i][j]=0; for(k=0;k<m;k++) c[i][j] = c[i][j] + a[i][k] * b[k][j]; } /* PRINT RESULTANT MATRIX */

printf("\n\nNow the Resultant Matrix is :-\n\n"); for(i=0;i<n;i++) { for(j=0;j<p;j++) printf("%5d ",c[i][j]); printf("\n"); } getch( ); }

126

Program for Diagonals sum, of a Matrix. #include<stdio.h> #include<conio.h> void main( ) { int a[10][10],i,j,fwsum=0,rwsum=0,n; printf("Enter the no. of rows and columns for Matrix :"); scanf("%d",&n); /* * * * * READ MATRIX * * * * */ printf("\n\nNow enter the Matrix A[%d][%d] :-\n\n",n,n); for(i=0;i<n;i++)

14

for(j=0;j<n;j++) { printf("Row[%d] Column[%d] :",i,j); scanf("%d",&a[i][j]); } /* * * * * PRINT MATRIX * * * * */ printf("\n\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%5d",a[i][j]); printf("\n\n"); } /* * * * * CALCULATING FORWARD DIAGONAL SUM * * * * */ for(i=0;i<n;i++) fwsum += a[i][i]; printf("\n\nForward Diagonal Sum = %d",fwsum); /* * * * * CALCULATING REVERSE DIAGONAL SUM * * * * */ for(i=0;i<n;i++) rwsum += a[i][n-1-i]; printf("\n\nReverse Diagonal Sum = %d",rwsum); getch(); }

127

Program for Matrix Transpose #include<stdio.h> #include<conio.h> void main( ) { int a[10][10],b[10][10],i,j,r,c; printf("Enter the no. of rows for Matrix :"); scanf("%d",&r); printf("Enter the no. of columns for Matrix :"); scanf("%d",&c); /* * * * * READ MATRIX * * * * */ printf("\n\nNow enter the Matrix A[%d][%d] :-\n\n",r,c); for(i=0;i<r;i++) for(j=0;j<c;j++) { printf("Row[%d] Column[%d] :",i,j);

15

scanf("%d",&a[i][j]); } /* * * * * PRINT MATRIX * * * * */ printf("\n\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%5d",a[i][j]); printf("\n\n"); } /* * * * * TRANSPOSE MATRIX * * * * */ for(i=0;i<r;i++) for(j=0;j<c;j++) b[j][i] = a[i][j]; /* * * * * PRINT TRANSPOSED MATRIX * * * * */ printf("\n\n Now the Transposed Matrix is :-\n\n"); for(i=0;i<c;i++) { for(j=0;j<r;j++) printf("%5d",b[i][j]); printf("\n\n"); } getch( ); }

128

Program for Read and print Character Array. #include<stdio.h> #include<conio.h> void main ( ) { int i,n; char c[20]; /* * * * * * * FOR READING CHARACTER BY CHARACTER * * * * * * printf("how many characters to enter (1-20):"); scanf ("%d",&n); for ( i=0;i < n; i++) { fflush(stdin); scanf("%c",&c[i]); } for ( i = 0; i < n; i++) printf("%c",c[i]); * * * * * * * FOR READING A COMPLETE STRING * * * * * * * printf("Enter any String (max 20 char long) :");

16

gets(c); //scanf("%s",c); printf("\n\n%s",c); * * * * * FOR READING STRING USING getchar() * * * * * */ printf("Enter any String (max 20 char long) :"); for(i=0;(c[i] = getchar()) != '\n';i++); c[i] = '\0'; for(i=0;putchar(c[i]) ;i++); getch( ); } 129 Program for String Concatenation. #include<stdio.h> #include<conio.h> void main ( ) { int i,j=0,k=0; char a[10],b[10],c[20]; clrscr( ); printf("Enter First String (max 10 char long):"); gets(a); fflush(stdin); printf("Enter Second String (max 10 char long):"); gets(b); for ( i=0;a[i] != NULL;i++) c[i] = a[i]; for ( ;b[j] != NULL;i++,j++) c[i] = b[j];

c[i] = NULL; printf("%s",c); getch( ); } 130 Program for Copy a String and Change it's Case. #include<stdio.h> #include<conio.h> void main ( ) { int i,j=0,k=0; char a[10],b[10],c[20]; clrscr( ); printf("\nEnter First String (max 10 char long):"); gets(a); fflush(stdin); printf("\n\nEnter Second String (max 10 char long):"); gets(b); for ( i=0;a[i] != NULL;i++)

17

c[i] = a[i]; for ( ;b[j] != NULL;i++,j++) c[i] = b[j];

c[i] = NULL; printf("\n\nNow the Resultant String is :-"); printf("\n\n%s",c); getch( ); } 131 Program for Compare Strings on basis of Length. #include<stdio.h> #include<conio.h> void main( ) { int j,i,c=0; char a[20],b[20]; printf("Enter First String:-"); scanf("%s",a); printf("Enter Second String:-"); scanf("%s",b); i=0; while(a[i]= =b[i] && a[i] != '\0' && b[i] != '\0') i = i + 1; if(a[i] = = '\0' && b[i] = = '\0') printf("\n\nStrings are equal"); else printf("\n\nStrings are not Equal"); for(i=0;a[i] != NULL;i++) c += 1; printf("\n\nLength of string 1 is %d",c); c=0; for(i=0;b[i] != NULL;i++) c += 1; printf("\n\nLength of string 2 is %d",c); getch( ); } 132 Count Vowels in a String and Pad them by " * ". #include<stdio.h> #include<conio.h> #include<ctype.h> void main ( ) { int i,c=0; char a[20],b[20]; clrscr( ); printf("Enter any String :"); gets(a); for ( i=0;a[i] != NULL;i++) { a[i] = toupper(a[i]);

18

if(a[i] = ='A' || a[i] = ='E' || a[i] = ='I' || a[i] = ='O' || a[i] = ='U') { a[i] = '*'; c++; } } printf("\n\n%s",a); printf("\n\nThere are %d Vowels in the string.",c); getch( ); } 133 Program for Reverse a String. #include<stdio.h> #include<conio.h> void main ( ) { int i,j; char a[20],b[20]; clrscr( ); printf("Enter any String :"); gets(a); for ( i=0;a[i] != NULL;i++) { } i--; for (j=0;i >= 0;j++,i--) b[i] = a[j]; b[j] = NULL; printf("%s",b); getch( ); } 134. Check a String for Palindrome. #include<stdio.h> #include<conio.h>

void main() { char a[100]; int i,len,flag=0; clrscr(); printf("\nENTER A STRING: "); gets(a); len=strlen(a); for(i=0;i<len;i++) { if(a[i]==a[len-i-1]) flag=flag+1; } if(flag==len) printf("\nTHE STRING IS PALINDROM"); else printf("\nTHE STRING IS NOT PALINDROM"); getch(); 19

}
135. Count Frequency of Alphabets in a string. #include<stdio.h> #include<conio.h> #include<ctype.h> void main ( ) { int i,j,k,c[26]; char a[20]; clrscr( ); printf("Enter any String (max 20 char) :"); gets(a); for(i=0;i<26;i++) c[i] = 0; for(i=0;a[i] != NULL;i++) { a[i] = toupper(a[i]); k = a[i] - 65; c[k] += 1; } for(i=0;i<26;i++) if(c[i] != 0) printf("\n%c = %d",i+65,c[i]); getch ( ); }

136.

Remove extra spaces from a string and change it into Proper case. #include<stdio.h> #include<conio.h> #include<ctype.h> void main ( ) { int i,j=0; char a[30],b[20]; clrscr ( ); printf("Enter any String (max 20 char) :"); gets(a); a[0] = toupper(a[0]); for(j=0,i=j+1;a[j] != '\0';j++,i++) { if(a[j] ==' ' && a[i] !=' ') a[i] = toupper(a[i]); else a[i] = tolower(a[i]); } printf("\n\n%s",a); getch(); }

20

137.

Program showing use of String.h functions. #include<stdio.h> #include<conio.h> #include<string.h> void main ( ) { int i,n,len1=0,len2=0; char a[40],b[40],c[40]; /* * * * * * * READING A STRING * * * * * * */ printf("\NEnter First String (max 20 char long) :"); gets(a); printf("\NEnter Second String (max 20 char long) :"); gets(b); /* * * COPYING STRING TO ANOTHER ARRAY * * */ strcpy (c,a); /* * * * * CALCULATING LENGTH OF STRINGS * * * * */ len1 = strlen(a); len2 = strlen(b); printf("\n\nLength of first String is %d",len1); printf("\n\nLength of Second String is %d",len2); /* * * * * * COMPAIRING BOTH STRINGS * * * * * */ i = strcmpi(a,b); if(i == 0) printf("\n\nBoth Strings are equal"); else if(i > 0) printf("\n\nSecond String is Alphabetically above String First"); else printf("\n\nFirst String is Alphabetically above Second String"); /* * * * * * CONCATENATING BOTH STRINGS * * * * * */ strcat(a,b); printf("\n\nConcatinating Second String with First :-"); printf("%s",a); /* * * * * CHANGING CASE OF RESULTANT STRING * * * * */ // strlwr(a); strupr(a); printf("\n\nString after changing case :- %s",a); printf("\n\n String C copied from A is : %s",c); getch( );

21

} 138. Program for comparing strings on basis of Alphabetical Order. #include<stdio.h> #include<conio.h> void main( ) { int c,i; char a[10],b[10]; printf("\n\nEnter First String:-"); scanf("%s",a); printf("\n\nEnter Second String:-"); scanf("%s",b); for(i=0;a[i]!=NULL || b[i]!=NULL;i++) if(a[i] != b[i]) { if(a[i] > b[i]) c = 1; else c = -1; break; } else c = 0; if(c = = 0) printf("\n\nBoth Strings are Equal"); else if(c = = -1) printf("\n\nFirst String is Albetically above Second String."); else printf("\n\nSecond String is Alphabetically above First String."); getch( ); } 139. Program for Sorting Array of Strings. #include<stdio.h> #include<conio.h> #include<string.h> void main( ) { int i,j,n; char a[10][20],t[20]; clrscr( ); /* * * * * READING ARRAY OF STRINGS * * * * */ printf("\nEnter how many Strings :-"); scanf("%d",&n); for(i=0;i < n;i++) { fflush(stdin);

22

gets(a[i]); } /* * * * SORTING OF ARRAY BY BUBBLE SORT * * * */ for(i=1;i < n;i++) for(j=0;j < n-i;j++) if(strcmp(a[j],a[j+1]) > 0) { strcpy(t,a[j]); strcpy(a[j],a[j+1]); strcpy(a[j+1],t); } /* * * * PRINTING SORTED ARRAY OF STRINGS * * * */ printf("\n\nNow the sorted array of strings is :-\n\n"); for(i=0;i < n;i++) printf("\n%s",a[i]); getch( ); 11. #include<stdio.h> #include<conio.h> void main() { char name[6]={'S', 'M', 'I', 'T', 'H', '\O'}; int i; clrscr(); for(i=0;i<5;i++) { printf("\n %c",name[i]); } getch(); } 22 #include<stdio.h> #include<conio.h> void main() { char name[6]="SMITH"; int i; clrscr(); for(i=0;i<5;i++) { printf("\n %c",name[i]); } getch(); }

143

Array of structures
23

#include<stdio.h> #include<conio.h> struct student { char name[20]; int h,e,m,tot; }; void main ( ) { student p[20]; int i,n; clrscr ( ); printf("How many Records :"); scanf("%d",&n); for(i=0;i<n;i++) { fflush(stdin); printf("\n\nEnter name :"); gets(p[i].name); printf("Marks for Hindi :"); scanf("%d",&p[i].h); printf("Marks for English :"); scanf("%d",&p[i].e); printf("Marks for Maths :"); scanf("%d",&p[i].m); p[i].tot = p[i].h + p[i].e + p[i].m; } for(i=0;i<n;i++) { printf("\n\nName is %s",p[i].name); printf("\nHindi : %d",p[i].h); printf("\nEnglish : %d",p[i].e); printf("\nMaths : %d",p[i].m); printf("\nTotal : %d",p[i].tot); } getch( ); }

24

You might also like