Wednesday, November 11, 2020

C Program Assignments and Answers

Question 1
Determine the output and value of each variable after the execution of each code. All variables have the value of 2 before execution of each statement
a) printf(“ %d %d ”,++c+x++,c);
b) printf(“ %d ”,sum+c-2);
c) printf(“%d”, ++sum+x+c--);
d) printf(“ %d %d %d”,--sum+x , ++c , x);

 Answers:

a)
#include <stdio .h="" >
int main()
{
    int c, x;
    c = 2;
    x = 2;
    printf("%d %d", ++c + x++, c);   
}

Answer
5  3

b)
#include <stdio .h> 
int main()
{
    int sum, c;
    sum = 2;
    c = 2;
    printf("% d", sum + c - 2);
}
Answer
2

c)
#include <stdio .h>
int main()
{
    int sum, x, c;
    sum = 2;
    x = 2;
    c = 2;
    printf(" % d", ++sum + x + c--);
}

Answer
7

d)
#include <stdio .h>
int main()
{
    int sum, x, c;
    sum = 2;
    x = 2;
    c = 2;
   printf(" %d %d %d", --sum + x, ++c, x);
}


Answer
3 3 2

Question 2
Write a single C statement for each of the following:
a) Decrease the value of A by 1, decrease the value of B by 1 and divide A from B. Assign the result to R.
b) Reduce the value of P by 1, then subtract P from Q and then decrease the value of Q by 1. Assign the result to Z.
c) Increase the value of A and B by 1 and multiply A from B. Assign the result to C.
d) Reduce 1 from A and deduct 1 from B and add A and B. Assign the result to X.

 a)
Answer for the Question
    R = --A / --B;

//Testing Program
#include <stdio .h>
int main()
{
    /*Decrease the value of A by 1, decrease the value of B by 1
    and divide A by B.Assign the result to R.*/
    int A, B, R;
    A = 7;
    B = 3;
    R = --A / --B;
    printf("%d", R);
}

Answer Testing Program
3

 b)
Answer for the Question
    Z = Q-- - --P;

//Testing Program
#include <stdio .h> 
int main()
{
    /*Reduce the value of P by 1, then subtract P from Q 
    and then decrease the value of Q by 1. Assign the result to Z. */
    int P, Q, Z;
    P = 7;
    Q = 10;
    Z = Q-- - --P;
    printf("%d %d", Z,Q);
}


Answer Testing Program
4  9


c)
Answer for the Question
    C = ++A * ++B;

//Testing Program
#include <stdio .h>
int main()
{
   /*Increase the value of A and B by 1 and multiply A from B. 
   Assign the result to C. */
    int A, B, C;
    A = 2;
    B = 3;
    C = ++A * ++B;
    printf("%d", C);
}

Answer Testing Program
12

 d)
Answer for the Question
    X = --A + --B;

//Testing Program
#include <stdio .h="" > 
int main()
{
   /*Reduce 1 from A and deduct 1 from B and add A and B. 
   Assign the result to X. */
    int A, B, X;
    A = 2;
    B = 3;
    X = --A + --B;
    printf("%d", X);
}

Answer Testing Program
3

Question 3
A Teacher conduct exam for her/his students. Write a Programme in C to input the marks of 20 students into an array and output the average marks of the class.

 #include < stdio.h >
main()
{
	int i;
	float income[5], Tot,Avg;
	i = 0;
	Tot = 0;
	Avg = 0;
	while (i < 5)
	{
		printf("%s","Enter Daily Income?");
		scanf_s("%f", &income[i]);
		Tot = Tot + income[i];
		i = i + 1;
	}
	Avg = Tot /5;
	printf("Average bus Income %10.2f", Avg);
	
}

Question 4
Write a program to:
*Input item no, quantity and price for items selected by a customer.
*Cashier should enter -99 as item number to get the final total.
* Program should display the bill as mention below.
*Customer will get a discount for their total bill as given below:
5% discount – if total bill amount > = 5,000
7% discount – if total bill amount > = 10,000
10% discount – if total bill amount > = 10,0000
*Print the bill with details of input with Total, Discount amount and Net Amount

 #include <stdio .h>
int main()
{
     
    int itemno[30], qty[30];
    float price[30];  
    double tot,Gtot, discount;
    char T,i,j;
    T = 1;
    i = 0;
    while (T != 0)
    {
        printf("%s", "Enter a Item No:");
        scanf_s("%d", &itemno[i]);
        if (itemno[i] == -99)
        {
            break;
        }
        printf("%s", "Enter a Item Qty:");
        scanf_s("%d", &qty[i]);
        printf("%s", "Enter a Item Price:");
        scanf_s("%f", &price[i]);
        i = i + 1;
    }
    tot = 0;
    Gtot = 0;
    discount = 0.00;
    printf("%s\t%s\t%s\t\%s\n", "Item", "Qty", "Price", "Total");
    for (j = 0;j <= i-1;j++)
    {
        tot = (double)qty[j] * price[j];
        printf("%d\t%d\t%f\t\%lf\n", itemno[j], qty[j], price[j],tot);
        Gtot = Gtot + tot;
   
    }
    if (Gtot >= 100000)
    {
        discount = (double)Gtot * 10 / 100;
    }
    else
    {
        if (Gtot >= 10000)
        {
            discount = (double)Gtot * 7 / 100;
        }
        else
        {
            if (Gtot >=5000)
                discount = (double)Gtot * 5 / 100;
        }
    }
    printf("%c", '\n');
    printf("Total\t\t\t%lf\n", Gtot);
    printf("Discount\t\t%lf\n", discount);
    printf("Net\t\t\t%lf\n", (double)Gtot - discount);

}


Question 5
Write a C program to input elements to 4x4 matrix and print matrix.

 Answer
#include <stdio .h>

int main() {
    /* matrix 4x4 declaration*/
    int disp[4][4];
    int i, j;
    for (i = 0; i < 4; i++) {
        for (j = 0;j < 4;j++) {
            printf("Enter matrix value [%d][%d]:", i + 1, j + 1);
            scanf_s("%d", &disp[i][j]);
        }
    }
    //Displaying array elements
    printf("Two Dimensional array elements:\n");
    for (i = 0; i < 4; i++) {
        for (j = 0;j < 4;j++) {
            printf("%d\t ", disp[i][j]);
            
            }
            printf("\n");
       
    }
    getch();
}


Question 6
A Driver Want to Calculate his Income. Write a Programme in C to input customer number, distance travelled (in kms) by his customer, charge per Kilometer and output the income received from all customers who have completed hires. A kilometer charge can be different for each customer. Use -999 for customer number to terminate and display the output

 #include < stdio.h >
#define  datacount 5
main()
{
	int Custno[datacount],C;
	float Distance[datacount],Charge[datacount],Income;

	C = 0;
	Income = 0;
	while (C < datacount)
	{
		printf("%s", "Enter a Customer no?");
		scanf_s("%d", &Custno[C]);
		if (Custno[C] == 999)
			break;
		printf("%s", "Enter a Distance Travelled?");
		scanf_s("%f", &Distance[C]);
		printf("%s", "Enter a Charge Per Kilo Meter?");
		scanf_s("%f", &Charge[C]);
		Income = Income + Distance[C] * Charge[C];
		C = C + 1;
	}
	for (int i = 0; i  < C; i++)
	{
		printf("Customer No:%d  Distanced Travelled: %10.2f _ 
        Kilometer Travelled %10.2f\n", Custno[i], Distance[i],Charge[i]);		
	}
	printf("Total Incode is : %10.2f", Income);
	
}

Note
Dynamic Memory Allocation

When we define a array we should know the size of the array. But if we don't know the size then we can allocate memory dynamically. There are some functions defined in the stdlib.h header file in C language. The functions are

malloc()
//format ptr=(castType*) malloc(size) -allocate memory without uninitialised for a given size
Example
int *ptr;
ptr=(int*) malloc(50*sizeof(int));

calloc()
//format ptr=(castType*) calloc(n,size) -allocate memory with initialised with zero
Example
int *ptr;
ptr=(int*) calloc(25,sizeof(int));

//free(ptr) free the space allocated
Example
int *ptr;
ptr=(int*) calloc(25,sizeof(int));
free(prt);

realloc()
//ptr =realloc(ptr,x) - change the size of the allocated memory size. n is reallocated size
int *ptr;
ptr=(int*) malloc(100,sizeif(int));

ptr=realloc(ptr,50,sizeif(int));

free(ptr);

if we use the dynamic memory allocation for the above program than C Program will be as follows:

 #include < stdio.h >
#include <stdlib.h>
#define  datacount 5
main()
{
	int* Custno,C;
	float * Distance, * Charge, Income;	
	Custno = (int*)calloc(datacount, sizeof(int));
	Distance = (float*)calloc(datacount, sizeof(float));
	Charge = (float*)calloc(datacount, sizeof(float));

	C = 0;
	Income = 0;
	while (C <datacount)
	{
		printf("%s", "Enter a Customer no?");
		scanf_s("%d", Custno+C);
		if (*(Custno+C) == 999)
			break;
		printf("%s", "Enter a Distance Travelled?");
		scanf_s("%f", Distance+C);
		printf("%s", "Enter a Charge Per Kilo Meter?");
		scanf_s("%f", Charge+C);
		Income = Income + *(Distance+C) * *(Charge+C);
		C = C + 1;
	}
	for (int i = 0; i < C; i++)
	{
		printf("Customer No:%d  Distanced Travelled: %10.2f -
        Charge per km % 10.2f\n",Custno[i], Distance[i],Charge[i]);		
	}
	printf("Total Incode is : %10.2f", Income);
	free(Custno);
	free(Distance);
	free(Charge);
}


Question 7

Write a program in C to calculate the discount amount and display the discounted amount. Use separate functions to

a) Input amount and discount amount
b) Calculate discount amount
c) Display the Calculated Amount
provide meaning full names for functions and parameters.

 #include <stdio.h>

void billInput(float *amt, float *discount);
float calculateDiscount(float amt, float discount);
void displayDiscountAmt(float Amount, float amt);

main()
{
	float Amount, Discount;
	billInput(&Amount, &Discount);
	displayDiscountAmt(Amount, Discount);
}

void billInput(float *amt, float *discount)
{
	printf("Enter the Bill Amount ?");
	scanf_s("%f", amt);
	printf("Enter the Discount Amount ?");
	scanf_s("%f", discount);
}

float calculateDiscount(float amt, float discount)
{
	float d;
	d = amt * discount / 100;
	return d;
}

void displayDiscountAmt(float amt, float amount)
{
	float disAmt;
	disAmt = amt - calculateDiscount(amt, amount);
	printf("Discounted Amount=%10.2f",disAmt);
}

No comments:

Post a Comment