C Programming 2019 Makeup
Year 2019 Makeup Write a function that accepts two integers as arguments and returns average of even numbers between them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int avg(int a, int b) { int avg = 0, sum = 0, count = 0, min = a<b?a:b, max = a>b?a:b; for( int i = min ; i <= max ; i++ ) { if(i%2==0) { sum+=i; count++; } } avg = sum/count; return avg; } void main() { int a,b; printf("Enter two numbers:\n"); scanf("%d%d",&a,&b); printf("Average of even numbers between them: %d.",avg(a,b)); } |
Write a program to read a file named ‘some.txt’ which contains 20 words and display only ten words in the monitor.
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() { FILE *fp = fopen("some.txt","r"); if(fp){ printf("Reading contents of the file...\n"); printf("Displaying the ten words from the file:\n"); int count = 0,c; while((c = fgetc(fp)) != EOF) { if(c == ' ' || c == '\n' || c == '\t') { printf("\n"); ++count; } else { printf("%c", c); } if(count == 10) break; } } else printf("Error reading file."); fclose(fp); } |
Write a program to take 11 records of car (model, price, color, […]
Continue Reading