C programming 2012
Year 2012 Q.No 3 a)
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> #include<stdlib.h> int main() { float *p; int n,i; float g; printf("How many float number you want to enter? "); scanf("%d",&n); p=(float*)malloc(n*sizeof(float)); printf("Enter %d float numbers ",n); for(i=0;i<n;i++) scanf("%f",&p[i]); for(i=0;i<n-1;i++) { if(p[i]>p[i+1]) { g=p[i]; } else { g=p[i+1]; } } printf("greatest number is %f",g); return 0; } |
b)
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 |
#include<stdio.h> #include<stdlib.h> int main() { float *num; int n,i,j; float a; printf("How many float number you want to enter? "); scanf("%d",&n); num=(float*)malloc(n*sizeof(float)); printf("Enter %d float numbers ",n); for(i=0;i<n;i++) scanf("%f",&num[i]); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (num[i] < num[j]) { a = num[i]; num[i] = num[j]; num[j] = a; } } } printf("Displaying in descending order\n"); for(i=0;i<n;i++) { printf("%f\n",num[i]); } return 0; } |
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 |
#include<stdio.h> #include<stdlib.h> int main() { float *num; FILE *fp; int n,i,j; float a,sum,avg; printf("How many float number you want to enter? "); scanf("%d",&n); num=(float*)malloc(n*sizeof(float)); printf("Enter %d float numbers ",n); for(i=0;i<n;i++) scanf("%f",&num[i]); fp=fopen("data.tu","w"); for(i=0;i<n;i++) { sum=sum+num[i]; } avg=sum/n; fprintf(fp,"Sum = %f\n",sum); fprintf(fp,"Avg = %f\n",avg); fclose(fp); return 0; } |
Q.No 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { printf("\n"); for(j=1;j<=5;j++) { if(i<=j) printf("%c",'c'); else printf(" "); printf(" "); } } return 0; } |
Q.No .5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { int a[15],i,sum=0; float b; printf("Enter 15 integers"); for(i=0;i<15;i++) scanf("%d",&a[i]); for(i=0;i<15;i++) { sum=sum+a[i]; } b=sum/15; printf("Average %f",b); return 0; } |
Q.No 6
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 |
#include<stdio.h> struct cattle { int cid; char name[20]; int age; float weight; }; int main() { struct cattle a[28]; int i,sum=0; double avg; for(i=0;i<28;i++) { printf("%d record is\n ",i+1); printf("Enter id "); scanf("%d",&a[i].cid); printf("Enter name "); scanf("%s",a[i].name); printf("Enter age "); scanf("%d",&a[i].age); printf("Enter weight "); scanf("%f",&a[i].weight); } for(i=0;i<28;i++) sum=sum+a[i].weight; avg=sum/28; printf("Avg = %f",avg); return 0; } |