Tag: C programming
C programming 2013
2013 Q.no 3
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include<stdio.h> int min(int a[],int n) //Returns minimum element in array { int i,minn=a[0]; for(i=0;i<n;i++) { if(a[i]<minn) minn=a[i]; } return minn; } int max(int a[],int n) //Returns maximum element in array { int i,maxx=a[0]; for(i=0;i<n;i++) { if(a[i]>maxx) maxx=a[i]; } return maxx; } void main() { int n,i,a[n],j,count=0,min_element,max_element,pov=0,neg=0,cou=0,small; printf("Enter how many number to enter "); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } small=min(a,n); Printf("smallest number is %d",small); min_element=min(a,n); max_element=max(a,n); for(i=min_element;i<max_element;i++) //Run loop from min element to max element { //and get count of each element in array as usual. count=0; for(j=0;j<n;j++) { if(a[j]==i) count++; } if(count) printf("Count of %d is %d\n",i,count); } for(i=0;i<n;i++) { if(i<0) { neg=neg+a[i]; cou++; } else { pov=pov+a[i]; } } printf("Sum of positive number = %d\nSum of negative number = %d\n",pov,neg); for(i=0;i<n;i++) { if(a[i]%2==0) { cou++; } } printf("Total no of even elements = %d",cou); } |
Q.no. 4
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<stdio.h> #include<string.h> int main() { int n,i; int id[n]; char name[n][20],oname[n][20],occ[n][20]; printf("Enter number of input to be entered"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter id "); scanf("%d",&id[i]); printf("Enter name "); scanf("%s",name[i]); printf("Enter office name "); scanf("%s",oname[i]); printf("Enter occupation "); gets(occ[i]); } for(i=0;i<n;i++) { if(strcmp(oname[i],"Nepal Bank")==0 && strcmp(occ[i],"manager")==0) printf("%s\t %d\t %s\t %s\t",name[i],id[i],oname[i],occ[i]); } } |
Q.no 5
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include<stdio.h> int min(int a[],int n) //Returns minimum rainfall month { int i,minn=a[0]; for(i=0;i<n;i++) { if(a[i]<minn) minn=i; } return minn; } int max(int a[],int n) //Returns maximum rainfall month { int i,maxx=a[0]; for(i=0;i<n;i++) { if(a[i]>maxx) maxx=i; } return maxx; } void result(int a) { switch(a) { case 1: printf("Baisakh\n"); break; case 2: printf("Jestha\n"); break; case 3: printf("Ashad\n"); break; case 4: printf("Shrawan\n"); break; case 5: printf("Bhadoh\n"); break; case 6: printf("Ashosh\n"); break; case 7: printf("Kartik\n"); break; case 8: printf("mangsir\n"); break; case 9: printf("poush\n"); break; case 10: printf("magh\n"); break; case 11: printf("falgun\n"); break; case 12: printf("chaitra\n"); } } void main() { int n[12],i,total; float avg; for(i=0;i<12;i++) { printf("Enter total no of rainfall for %d month\n",i+1); scanf("%d",&n[i]); total=total+n[i]; } avg=total/12; printf("Average = %f\n",avg); for(i=0;i<12;i++) { if(max(n,12)==i) { printf("month with highest rainfall is "); result(i+1); } if(min(n,12)==i) { printf("Month with lowest rainfall is "); result(i); } } } |
C programming 2014
year 2014 Q.no 3 Write function that display the factorial of 7 only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<stdio.h> void fact(int n) { int fact = 1; if(n==7) { for(int i=1;i<=n;i++) fact*=i; printf("The factorial of 7 is: %d.",fact); } else printf("This function gives the factorial of the number 7 only"); } void main() { fact(7); } Output: The factorial of 7 is: 5040. |
Q.no 4 Write program that takes string as input from the user, and then display the length.
1 2 3 4 5 6 7 8 9 |
#include<stdio.h> #include<string.h> void main() { char srt[50]; printf("Enter a string:\n"); gets(str); printf("The length of string is %d.",strlen(srt)); } |
Q.no 5 Write a program to display the following pattern. * # * # * ? ? * ? ? +
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> void main() { int i; for(i=0;i<4;i++) { printf("*\t"); if(i<2) printf("#"); if(i==2) printf("?\t?"); if(i==3) printf("?\t?\t+"); printf("\n"); } } |
Group […]
Continue Readingc programming 2017
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 […]
Continue Readingc programming 2018 Makeup
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 […]
Continue Readingc programming 2018
Year 2018 Group B 2.Write a program to input two integer as arguments and display sum of those numbers //i am using arguments without return type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> #include<conio.h> void sum(int a,int b); void main() { int a,b; printf("enter two numbers:"); scanf("%d%d",&a,&b); sum(a,b); getch(); } void sum(int a,int b) { int sum=0; sum=a+b; printf("the sum of %d and %d is:%d",a,b,sum); } |
write a program to write “hello world” to file hello.dat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> #include<conio.h> void main() { FILE *ptr; ptr=fopen("E:\\hello.dat","w");//opens file in E local disk char ch[]="HELLO WORLD"; if(ptr==NULL) { printf("unable to open"); } else { fputs(ch,ptr); fclose(ptr); } getch(); } |
Write a program for nested structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<stdio.h> #include<conio.h> struct student { int id; char name[20]; struct dateofbirth { int day,month,year; }dob; }s; void main() { printf("enter id,name and date of birth of an student(day-month-year)"); scanf("%d%s%d%d%d",&s.id,&s.name,&s.dob.day,&s.dob.month,&s.dob.year); printf("id\tname\tdate of birth(day-month-year)"); printf("\n%d\t%s\t%d-%d-%d",s.id,s.name,s.dob.day,s.dob.month,s.dob.year); getch(); } |
Write a program to input on integer […]
Continue Reading