2) Write a program to check if two matrices are equal or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<stdio.h> int main(){ int a[100][100],b[100][100]; int r1,c1,r2,c2,i,j,flag=0; printf(“enter rows and columns of first and second matrix\n”); scanf(“%d%d%d%d”,&r1,&c1,&r2,&c2); printf(“enter elements of first matrix”); for(i=0;i<r1;i++){ for(j=0;j<c1;j++){ scanf(“%d”,&a[i][j]); } } printf(“enter elements of second matrix”); for(i=0;i<r1;i++){ for(j=0;j<c1;j++){ scanf(“%d”,&b[i][j]); } } if((r1 == r2) && (c1 == c2)){ for(i=0;i<r1;i++){ for(j=0;j<c1;j++){ if(a[i][j] ! == b[i][j]){ flag = 1; } } } if(flag == 1) printf("they are not equal"); else printf("they are equal"); } else{ printf("they are not equal"); } } |
4)Write a program using pointer to input 10 numbers and store it in array and sort in ascending order using pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
int main(){ int array[100],n,i,j; int *ptr; printf("enter size of array\n"); scanf("%d",&n); printf("enter %d elements ",n); for(i=0;i<n;i++){ scanf("%d",&a[i]); } ptr = &a; for(i=0;i<n;i++){ for(j=i+1;j<n;j++){ if(*(ptr+i) > *(ptr+j)){ temp = *(ptr + i); *(ptr + i) = *(ptr + j); *(ptr + j) = temp; } } } printf("the array in sorted order is :"); for(i=0;i<n;i++){ printf("%d",a[i]); } } |
5) write a program that reads 2 numbers from user and calculate sum and write it in a file name “sum.dat”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main(){ int a,b; FILE *ptr; ptr = fopen("sum.dat","w"); if(ptr == NULL){ return 0; } printf("enter 2 numbers\n"); scanf("%d%d",&a,&b); fprintf(ptr,"%d",a+b); fclose(ptr); return 0; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
6) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 int main(){ int i,j; for(i=1;i<=5;i++){ for(j=1;j<=i;j++){ printf("1 "); } printf("\n"); } return 0; } long question |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
7) #include<stdio.h> struct stu{ char name[20],add[20]; int roll; char email[20]; }s; int main(){ FILE *ptr; ptr = fopen("college.txt","w"); int i; printf("enter records of 20 students"); for(i=0;i<20;i++){ scanf("%s %s %d %s",s.name,s.add,&s.roll,s.email); fprintf(ptr,"%s %s %d %s\n",s.name,s.add,s.roll,s.email); } fclose(ptr); ptr = fopen("college.txt","r"); while(!feof(ptr)){ while(fscanf(ptr,"%s %s %d %s\n",s.name,s.add,&s.roll,s.email)!=-1){ if(strcmp(s.add,"pokhara")){ printf("%s %s %d %s",s.name,s.add,s.roll,s.email) } } } fclose(ptr) return 0; } |
8)check if string is palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int main(){ char string[100],temp[100]; printf("enter string"); gets(string); strcpy(temp,string); strrev(string); if(strcmp(string,temp)==0){ printf("the string is palindrome"); } else{ printf("the string is not palindrome"); } return 0; } |
9)write a function to generate fibonacci series upto n number supplied in function parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void fibonacci(int n){ if(n== 1 || n==2) return 1; return fibonacci(n-1) + fibonacci(n-2); } int main(){ int i,n,ans; printf("enter the term \n"); scanf("%d",n); for(i=1;i<=n;i++){ ans = fibonacci(n); printf("%d ",ans); } return 0; } |