Year 2018 Makeup
- Write a program to print transpose of a given matrix.
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 36 37 38 39 |
#include<stdio.h> void main() { int r,c; printf("Enter the size of the matrix: "); scanf("%d%d",&r,&c); int a[r][c], t[c][r]; int i,j; printf("Enter the elements of the matrix:\n"); //Inputting the elements of the matrix for( i = 0 ; i < r ; i++ ) { for( j = 0 ; j < c ; j++) { printf("Enter [%d][%d] element: ",i,j); scanf("%d",&a[i][j]); } } printf("Given matrix:\n"); //Displaying the elements of the matrix for( i = 0 ; i < r ; i++ ) { for( j = 0 ; j < c ; j++) printf("\t%d",a[i][j]); printf("\n"); } //Calculating transpose of the matrix for( i = 0 ; i < c ; i++ ) for( j = 0 ; j < r ; j++) t[i][j] = a[j][i]; printf("Transpose of the given matrix:\n"); //Displaying the elements of the transpose matrix for( i = 0 ; i < c ; i++ ) { for( j = 0 ; j < r ; j++) printf("\t%d",t[i][j]); printf("\n"); } } |
- Write a C program to read the file “hello.txt” and count the number of characters written in this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> void main() { FILE *f = fopen("hello.txt","r"); int count = 0; if(f) { int c; while((c=fgetc(f))!=EOF) count++; printf("Number of characters in the file 'hello.txt': %d.",count); } else printf("Error reading the file 'hello.txt'."); fclose(f); } |
- Write a function to calculate the factorial value of the given number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int fact(int n) { if(n<2) return 1; return n*fact(n-1); } void main() { int n; printf("Enter a number: "); scanf("%d",&n); printf("Factorial of %d is %d.",n,fact(n)); } |
- Write a program to read n numbers from a user and sort those numbers in ascending order.
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 |
#include<stdio.h> void main() { int n; printf("How many numbers do you want to insert?\n: "); scanf("%d",&n); int arr[n],i,j,temp; printf("Enter %d numbers:\n",n ); for( i = 0 ; i < n ; i++ ) scanf("%d",&arr[i]); //Sorting for( i = 0; i < n ; i++ ) { for( j = i+1; j < n ; j++) { if(arr[i]>arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } printf("\nSorted data:\n"); for( i = 0 ; i < n ; i++ ) printf("%d\t",arr[i]); } |
- Define a structure name “hotel”. It should have members that include the name, address, average room charge, and number of rooms. Read input of 10 hotels from user and write them to file named “hotel.dat”. Also display the name of hotels that have more than 5 rooms.
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 36 37 38 39 40 41 42 43 |
#include<stdio.h> struct hotel{ char name[40]; char address[50]; int average_room_charge; int number_of_rooms; }; void main() { int i; struct hotel H[10]; FILE *f = fopen("hotel.dat","w"); printf("Enter the hotel details of 10 hotels:\n"); for( i = 0 ; i < 10 ; i++ ) { printf("\nHotel %d:\n",i+1); printf(" Name: "); scanf("%s",H[i].name); /*using scanf instead of gets to prevent input problem, but as a result, strings'(with white-spaces) only initial part before the white space is taken. */ printf(" Address: "); scanf("%s",H[i].address); printf(" Average room charge: "); scanf("%d",&H[i].average_room_charge); printf(" Number of rooms: "); scanf("%d",&H[i].number_of_rooms); } fprintf(f,"Hotel details:\n\n"); for( i = 0 ; i < 10 ; i++ ) { fprintf(f,"Hotel %d:\n\n Name: %s\n Address: %s\n Average room charge: %d\n Number of rooms: %d\n\n",i+1,H[i].name,H[i].address,H[i].average_room_charge,H[i].number_of_rooms); } printf("\n\nHotels with more than 5 rooms:\n"); for( i = 0 ; i < 10 ; i++ ) { if(H[i].number_of_rooms>5){ printf("\n\nHotel %d:\n",i+1); printf("\n Name: %s",H[i].name); printf("\n Address: %s",H[i].address); printf("\n Average room charge: %d",H[i].average_room_charge); printf("\n Number of rooms: %d",H[i].number_of_rooms); } } fclose(f); } |
- Write a program to count the number of vowels in a given string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> #include<string.h> void main() { char str[100]; printf("Enter a sentence: "); gets(str); int i = 0, vowels = 0; while(str[i]!='\0') { if( str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u' ) vowels++; i++; } printf("Number of vowels in the given sentence: %d.\n",vowels); } |
9. Write a program to print the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> void main() { int i,j; for( i = 1 ; i <= 5 ; i++ ) { for( j = 1 ; j <= i ; j++ ) { printf("%d\t",j); } printf("\n"); } <} |