Você está na página 1de 14

1. Write a C program to perform matrix addition. #include<stdio.

h> int main() { int m1[10][10],m2[10][10],i,j,res[10][10]; printf("Matrix 1\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&m1[i][j]); printf("Matrix2\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&m2[i][j]); printf("Resultant Matrix\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { res[i][j]=m1[i][j]+m2[i][j]; } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d\t",res[i][j]); } printf("\n"); } return 0; } 2. Write a C program to perform matrix multiplication. #include<stdio.h> int main() { int m1[10][10],m2[10][10],i,j,k,mult[10][10]; printf("Matrix 1\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&m1[i][j]); printf("Matrix2\n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&m2[i][j]); printf("Resultant Matrix\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { mult[i][j]=0; for(k=0;k<2;k++) mult[i][j]+=m1[i][k]*m2[k][j];

} printf("\n"); } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d\t",mult[i][j]); } printf("\n"); } return 0; } 3. Write a C program to compute the transpose of a matrix #include<stdio.h> int main() { int m[10][10],i,j; printf("Matrix \n"); for(i=0;i<2;i++) for(j=0;j<2;j++) scanf("%d",&m[i][j]); printf("Resultant Matrix\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d\t",m[j][i]); } printf("\n"); } return 0; } 4. Write a C program to find the greatest of N numbers using arrays #include<stdio.h> intmain () { inti,j,t,n,number[30]; printf ("Enter the value of N\n"); scanf ("%d", &n); printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]); for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j) { if (number[i] < number[j]) { t=number[i];

number[i] = number[j]; number[j] = t; } } } printf ("The greatest number is %d",number[0]); return 0; } 5. Write a C program to sort N numbers. Use pointers. #include<stdio.h> #include<stdlib.h> intmain() { intn,i,j,t; int *a; printf("How many number do u want to sort\n"); scanf("%d",&n); a = (int*) malloc (n*sizeof(int)); printf("Enter the Number\n"); for(i=0;i<n;i++) scanf("%d",a+i); for (i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { t = *(a+i); *(a+i) = *(a+j); *(a+j) = t; } printf("Number's in Ascending order:\n"); for(i=0;i<n;i++) printf("%d\n",*(a+i)); return 0; } 6. Write a C program using pointers to read in an array of integers and print its elements in reverse order #include<stdio.h> #include<stdlib.h> int main() { int *a,n,i; a = (int*)malloc(sizeof(int)); printf("Enter the number of elements :\n"); scanf("%d",&n); printf("Enter the array elements:\n"); for(i=0;i<n;i++) scanf("%d",a+i); printf("Array elements in reverse order:\n"); for(i=n-1;i>=0;i--)

printf("%d\n",*(a+i)); return 0; } 7. Develop a C function that will scan a character string passed as an argument and convert all lower-case characters to their upper-case equivalents #include <stdio.h> #include <ctype.h> int main(void) { charstr[80]; voidupr(char[]); printf("Enter a string: "); gets(str); upr(str); return 0; } voidupr(char str1[80]) { int i; for( i = 0; str1[ i ]; i++) str1[ i ] = toupper( str1[ i ] ); printf("%s\n", str); } 8. Develop a C program to print the number of vowels in a given paragraph #include<ctype.h> #include<stdio.h> int main() { char s[100]; inti,nv; printf("Enter some string:"); gets(s); nv=0; for(i=0;s[i]!='\0';i++) { if(s[i]=='A'||s[i]=='a'||s[i]=='E'||s[i]=='e'||s[i]=='I'||s[i]=='i'|| s[i]=='O'||s[i]=='o'||s[i]=='U'||s[i]=='u') nv++; } printf("Number of vowels=%d \n ",nv); return 0; } 9. Write a C program to sort N numbers. Use functions #include<stdio.h> int main () { inti,t,n,number[30];

void sort(int[]); printf ("Enter the value of N\n"); scanf ("%d", &n); printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]); sort(number) getch(); } void sort(intnum[30]) { inti,j; for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j) { if (num[i] >num[j]) { t=num[i]; num[i] = num[j]; num[j] = t; } } } printf ("The numbers arranged in ascending order are given below\n"); for (i=0; i<n; ++i) printf ("%10d\n",num[i]); return 0; } 10. Write a C program to sort N names alphabetically. Use pointers #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { intn,i,j; char *name[30],*temp; printf("How many names do u want to sort\n"); scanf("%d",&n); printf("Enter the Names\n"); for(i=0;i<n;i++) { name[i] = (char *)malloc(sizeof(char) * 10); scanf("%s",name[i]); } for (i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(name[i],name[j])>0) { temp=name[i]; name[i]=name[j];

name[j]=temp; } printf("Name's in Alphabetical order:\n"); for(i=0;i<n;i++) printf("%s\n",name[i]); return 0; } 11. Write a C program to sort N names alphabetically. Use functions #include<stdio.h> #include<string.h> int main() { intn,i,j; char name[10][10],temp[10]; void sort(char[][]); printf("How many names do u want to sort\n"); scanf("%d",&n); printf("Enter the Names\n"); for(i=0;i<n;i++) { scanf("%s",name[i]); } sort(name); return 0; } void sort(name1[10][10]) { inti,j; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(name1[i],name1[j])>0) { temp=name1[i]; name1[i]=name1[j]; name1[j]=temp; } printf("Name's in Alphabetical order:\n"); for(i=0;i<n;i++) printf("%s\n",name1[i]); } 12. Develop a C program to compute the roots of a quadratic equation #include<stdio.h> #include<math.h> int main( ) { int a , b, c, d; float x1, x2; printf("Enter the value of a, b, c:"); scanf("%d%d%d", &a, &b, &c); d=(b*b)-(4*a*c); if(d==0)

{ printf("\nRoots are real and equal"); x1= x2=(-b)/(2*a); printf("\nRoots are: %f \t %f ", x1, x2); } else if(d<0) printf("\nRoots are imaginary"); else { x1=((-b)+sqrt(d))/(2*a); x2=((-b)-sqrt(d))/(2*a); printf("\nRoots are real"); printf("\nRoots are : %f \t %f", x1, x2); } return 0; } 13. Given the number of elements and size of elements, write a C program to allocate memory using functions( Hint: Using malloc()) #include<stdio.h> #include<stdlib.h> int main() { intn,i,*ptr; printf("Enter number of elements :\n"); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array :\n"); for(i=0;i<n;++i) scanf("%d",ptr+i); printf("The elements are\n"); for(i=0;i<n;i++) printf("%d\n",*(ptr+i)); free(ptr); return 0; } 14. Write a C program that prints the address of a variable along with its value #include<stdio.h> int main() { int a; printf("Enter a value :\n"); scanf("%d",&a); printf("Value : %d\nAddress : %p\n",a,&a); return 0;

} 15. Write a C program to swap the values stored in two locations in the memory #include <stdio.h> int main() { int x, y, temp; printf("Enter the value of x and y\n"); scanf("%d%d",&x,&y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); return 0; } 16. Write a C program to find the position of an element using linear search #include<stdio.h> #include<stdlib.h> int main() { intn,a[10],k,i,flag=0; printf("Enter the number of element\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the elements %d :\n",i+1); scanf("%d",&a[i]); } printf("Enter the element to find\n"); scanf("%d",&k); i=0; while(i<n) { if(a[i]==k) { flag = 1; break; } i++; } if(flag==1) printf("The element found on Position %d value %d",i+1,a[i]); else printf("Element not fount."); return 0; } 17. Write a C program with function that accepts an integer in between 1-12 to represent the month and displays the corresponding month of the year #include<stdio.h>

int main() { intmonth; voidprint_month(int); printf("Please enter month:\n"); scanf("%d",&month); print_month(month); return 0; } voidprint_month(intm) { switch (m) { case1: printf("January");break; case2: printf("February");break; case3: printf("March");break; case4: printf("April");break; case5: printf("May");break; case6: printf("June");break; case7: printf("July");break; case8: printf("August");break; case9: printf("September");break; case10: printf("October");break; case11: printf("November");break; case12: printf("December");break; } } 18. Write a C program to read marks of 10 students in the range of 0-100. Then make 10 groups: 0-10, 10-20, 20-30, etc. Count the number of values that falls in each group and display the results. #include<stdio.h> int main () { inti,mark[10],count[10]={0,0,0,0,0,0,0,0,0,0}; printf ("Enter the marks \n"); for (i=0; i<10; ++i) scanf ("%d",&mark[i]); for (i=0; i<10; ++i) {

if(mark[i]>=0 && mark[i]<=10) count[0]++; else if(mark[i]>10 && mark[i]<=20) count[1]++; else if(mark[i]>20 && mark[i]<=30) count[2]++; else if(mark[i]>30 && mark[i]<=40) count[3]++; else if(mark[i]>40 && mark[i]<=50) count[4]++; else if(mark[i]>50 && mark[i]<=60) count[5]++; else if(mark[i]>60 && mark[i]<=70) count[6]++; else if(mark[i]>70 && mark[i]<=80) count[7]++; else if(mark[i]>80 && mark[i]<=90) count[8]++; else if(mark[i]>90 && mark[i]<=100) count[9]++; } printf ("Range Count\n"); printf ("0 to 10 %d\n",count[0]); printf ("10 to 20 %d\n",count[1]); printf ("20 to 30 %d\n",count[2]); printf ("30 to 40 %d\n",count[3]); printf ("40 to 50 %d\n",count[4]); printf ("50 to 60 %d\n",count[5]); printf ("60 to 70 %d\n",count[6]); printf ("70 to 80 %d\n",count[7]); printf ("80 to 90 %d\n",count[8]); printf ("90 to 100 %d\n",count[9]); return 0; } 19. Write a program to copy 4 characters of a character array from the 4 position in another character array #include<stdio.h> #include<stdlib.h> char* copyCharacter(char*); int main() { char *src,*des; src = (char *) malloc (sizeof(char) * 25); printf("Enter the string to copy :\n"); scanf("%s",src); des = copyCharacter(src); printf("The copied string is %s\n",des); return 0; } char* copyCharacter(char *src)
th

{ int i=3,j=0; char *des; des = (char *) malloc (sizeof(char) * 25); while(i<7){ *(des+j)=*(src+i); i++; j++; } return des; } 20. Write a C program to find the maximum and minimum number entered in an array using the array and pointer as function arguments #include<stdio.h> #include<stdlib.h> int max(int, int*); int min(int, int*); int main() { int n,*a,i; a = (int *)malloc (n*sizeof(int*)); printf("Enter the number of elements :\n"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",a+i); printf("The maximum value is %d\n",max(n,a)); printf("The minimum value is %d\n",min(n,a)); return 0; } int max(int n , int *a){ inti,max=*(a+0); for(i=0;i<n;i++) if(max<*(a+i)) max = *(a+i); return max; } int min(int n , int *a){ inti,min=*(a+0); for(i=0;i<n;i++) if(min>*(a+i)) min = *(a+i); return min; }

21. Write a program using pointer variables to read a character until ; is entered. If the character is in upper case, print in lower case and vice versa. Also count the number of upper and lower case characters entered #include<stdio.h> #include<stdlib.h>

int main() { int i=0,up=0,lw=0; char *s,*u,*l; s= (char*)malloc(sizeof(char)*10); u= (char*)malloc(sizeof(char)*10); l= (char*)malloc(sizeof(char)*10); printf("Read a character until ; is entered :\n"); do{ scanf("%c",&s[i]); if(s[i] >= 'a' && s[i] <= 'z'){ u[up] = ('A' + s[i] - 'a'); up++; } else if(s[i] >= 'A' && s[i] <= 'Z'){ l[lw] = ('a' + s[i] - 'A'); lw++; } i++; }while(s[i-1]!=';'); printf("The entered uppercase letters %d\n",up); i=0; while(i<up){ printf("%c ",u[i]); i++; } printf("\nThe entered lowercase letters %d\n",lw); i=0; while(i<lw){ printf("%c ",l[i]); i++; } return 0; } 22. Write a C program to insert and delete an element in array #include <stdio.h> #include <stdlib.h> void insert(int*,int,int,int); void delete(int*,int,int); intmain() { int *array, position, c, n ,value ; printf("Enter number of elements in array\n"); scanf("%d", &n); array = (int *) malloc (n * sizeof(int)); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an element\n");

scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); insert(array,position,value,n); printf("Resultant array is\n"); for (c = 0; c <= n; c++) printf("%d\n", array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); n+=1; if ( position >= n+1 ) printf("Deletion not possible.\n"); else { delete(array,position,n); printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; } void insert(int *array,intposition,intvalue,int n){ int c; for (c = n - 1; c >= position - 1; c--) array[c+1] = array[c]; array[position-1] = value; } void delete(int *array,intposition,int n) { int c; for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; } 23. Write a C program using pointers to allocate a memory for the string University and reallocate the memory for the string to University Examinations #include<stdio.h> #include<stdlib.h> int main() { char *n; n = (char*) malloc (10*sizeof(char)); printf("Enter the string :\n"); scanf("%s",n); printf("Memory %p contains : %s\n",n,n); n=realloc(n,25); printf("Enter the string (after memory reallocate) :\n"); scanf("%s",n); printf("Memory %p contains : %s\n",n,n); return 0; }

1. Develop a C program to copy the contents of one file into another 2. Create two files test1.txt and test2.txt and write a C program to read the file text1.txt character by character on the screen and paste it at the end of test2.txt 3. Write a C program to merge two files in to third file. The names of the files must be entered using the command line arguments. 4. Write a C program to find the number of characters in a file and also find the frequency of all alphabets in a string 5. Write a C program that reads the file name and text of 20 words as command line arguments. Write the text into a file whose name is given as the file name. 6. Write a program to create two files and perform merge operation on it. 7. Write a C program to create a file, which contains register number, name, age etc. and displaythe contents of the file. 8. Write a C program to create a file, which contains two numbers followed by an operator in a line.Read a line of the file, while reading it; with respect to the operator perform the operation againstthe numbers. 9. Create a file test2.txt and write a C program to perform the read and write operation in to the file.

Você também pode gostar