C Programming 2019 Makeup

2nd Semester

Year 2019 Makeup

  1. Write a function that accepts two integers as arguments and returns average of even numbers between them.
#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));
}
  1. Write a program to read a file named ‘some.txt’ which contains 20 words and display only ten words in the monitor.
#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);
}
  1. Write a program to take 11 records of car (model, price, color, cc) from user and display the records of red cars only.
#include<stdio.h>
#include<string.h>
struct car{
    char model[20];
    int price;
    char color[10];
    int cc;
};
void main()
{
    struct car C[11];
    int i;
    for(i=0;i<11;i++)
    {
        printf("\nEnter %dth car details :\n",i+1);
        printf("\nEnter car model: ");
        scanf("%s", C[i].model);
        printf("\nEnter car price: ");
        scanf("%d", &C[i].price);
        printf("\nEnter car color: ");
        scanf("%s", C[i].color);
        printf("\nEnter car cc: ");
        scanf("%d", &C[i].cc);
    }
    printf("\n\nDetails of red cars:\n");
    char color[10];
    for(i=0;i<11;i++)
    {
        strcpy(color,C[i].color); //use this to simply prevent change in the original data i.e ReD may be changed to red. define color.
        if(strcmp(strlwr(color),"red")==0)
        {
            printf("\n\n%dth car details: ",i+1);
            printf("\nModel: %s.",C[i].model);
            printf("\nPrice: %d.",C[i].price);
            printf("\nColor: %s.",C[i].color);
            printf("\nCC: %d.",C[i].cc);
        }
    }
}
  1. Write a function which accepts two integers as arguments and returns sum, difference, and product of the numbers.
#include<stdio.h>
typedef struct operations{
    int sum;
    int difference;
    int product;
}operations;
operations action(int a, int b)
{
    operations O;
    O.sum = a + b;
    O.difference = a - b;
    O.product = a * b;
    return O;
}
void main()
{
    int a,b;
    printf("Enter two numbers:\n");
    scanf("%d%d",&a,&b);
    operations O = action(a,b);
    printf("Sum : %d.\nDifference : %d.\nProduct : %d.",O.sum,O.difference,O.product);
}

 

  1. Write a program that takes integers in an array of size 3 by 3, and display sum of all the elements in the array.
#include<stdio.h>
void main()
{
    int a[3][3];
    int i, j, sum = 0;
    printf("Enter the elements of the array:\n");
    for( i = 0 ; i < 3 ; i++ )
    {
        for( j = 0 ; j < 3 ; j++ )
        {
            printf("Enter [%d][%d] element: ",i,j);
            scanf("%d",&a[i][j]);
            sum+=a[i][j];
        }
    }
    printf("The sum of all the elements in the array is: %d.",sum);
}
  1. Write a program to display the following pattern:

a
b b
c c c
e e e e

#include<stdio.h>
void main()
{
    char a = 'a';
    int i,j;
    for(i=1;i<=4;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%c\t",a);
        }
        a = a + 1;
        if(a=='d')
            a = 'e';
        printf("\n");
    }
}

 

Leave a Reply

Your email address will not be published.