Year 2017
2.Write a program to add four integer taken from user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> void main() { int a,b,c,d,s; printf("Enter four integers:\n"); scanf("%d%d%d%d",&a,&b,&c,&d); s = a+b+c+d; printf("The sum of given four integers = %d.",s); } Output: Enter four integers: 1 2 3 4 The sum of given four integers = 10. |
3. Explain dynamic memory allocation with example
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.
Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
malloc():-allocates single block of requested memory.
The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
calloc():-allocates multiple block of requested memory.
The syntax of calloc() function is given below:
ptr=(cast-type*)calloc(number, byte-size)
realloc():-reallocates the memory occupied by malloc() or calloc() functions
ptr=realloc(ptr, new-size)
free():-frees the dynamically allocated memory
The syntax for free() function is given below:
free(ptr)
Program to for dynamic memory allocation
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 |
#include<stdio.h> #include<stdlib.h> int main() { int n,i,*ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int*)malloc(n*sizeof(int)); if(ptr==NULL) { printf("Sorry! unable to allocate memory"); exit(0); } printf("Enter elements of array: "); for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr); getch(); return 0; } output: Enter number of elements: 5 Enter elements of array: 1 2 3 4 5 Sum=15 |
4.List and explain different file opening modes
A file represents a sequence of bytes on the disk where a group of related data is stored.File is created for permanent storage of data. It is a ready-made structure.The fopen() function is used to create a new file or to open an existing file.
Different modes in file handling.
S.N | Modes and Description |
1 | “r”: Opens a file for reading. The file must exists |
2 | “w”: Creates an empty file for writing. If a file with same name already exists, its content is erased and the file is considered as a new empty file. |
3 | “a”: Appends to a file. Writing operations append data at the end of file. The new file is created if it does not exist. |
4 | “r+”: Opens a file to update both reading and writing. The file must exist |
5 | “w+”: Creates an empty file for both reading and writing |
6 | “a+” :Opens a file for reading and appending |
5.Write a function to check whether a given number is 987 or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<conio.h> #include<stdio.h> int main() { int n; printf("Enter any number\n"); scanf("%d",&n); if(n==987) printf("The number is equal to 987\n"); else printf("The number is not equal to 987\n"); getch(); return 0; } |
6. Write a function to copy content of one array to another array. You can consider any type of arrays.
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 |
#include<conio.h> #include<stdio.h> #include<string.h> int main() { int arr1[] = {2,7,5,6,3,4}; int arr2[6]; int i; printf("Content of first array\n"); for(i=0;i<6;i++) printf("%d\t",arr1[i]); for(i=0;i<6;i++) { arr2[i]=arr1[i]; } printf("\nContent of second array\n"); for(i=0;i<6;i++) printf("%d\t",arr2[i]); getch(); return 0; } Output: Content of first array 2 7 5 6 3 4 Content of second array 2 7 5 6 3 4 |
Group “C”
7.List any five string handling functions. Write a program to swap contents of two string variables with each other.
The five string handling functions are:
- strlen():- This function is used to find length of string
- strcmp():-This function is used to compare two strings
- strcpy():-This function is used to copy one string into another
- strrev():- This function is used to reverse the content of string
- strlwr():- This function is used to convert uppercase into lowercase
12345678910111213141516171819202122232425#include<conio.h>#include<stdio.h>#include<string.h>int main(){char str1[20],str2[20],temp[20];printf("Enter first string\n");gets(str1);printf("Enter second string\n");gets(str2);strcpy(temp,str1);strcpy(str1,str2);strcpy(str2,temp);printf("The first string = %s\n",str1);printf("The second string = %s",str2);getch();return 0;}Output:Enter first stringKathmanduEnter second stringNepalganjThe first string = NepalganjThe second string = Kathmandu
8.Write a program to print following pattern using for loop
2
4
8
14
22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<conio.h> #include<stdio.h> int main() { int t,d,i; t=2;d=0; for(i=1;i<=5;i++) { t = t+d; d = d+2; printf("%d\n",t); } getch(); return 0; } Output: 2 4 8 14 22 |
9. Write a program to store value of two integer variables in a file named “Two_Rupees.txt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> void main() { FILE *fp; fp = fopen("Two_Ruppes.txt","w"); int m,n; printf("Enter any two numbers: "); scanf("%d%d",n); if(fprintf(fp,"%d%d",m,n)) printf("Successful"); else printf("Unsuccessful"); fclose(fp); } Output: Enter any two numbers: 500 600 Successful |