Programs with line-by-line explanations
Prepared by: Dr. Tojo Mathew
Warning! You are warned against blindly copying the code statements. Read the explanations given for the code statements and understand the implementation. Then, try to do it yourself. Blindly re-typing the code statements is a waste of time & effort.
Table of contents
Note: Descriptions in brown font enclosed within /* and */ or starting with // are comments for human readers to understand the program better. No need to type in comments in the actual program. Compilers ignore comments while generating executable code of the program.
Write a program that accepts the marks in 3 subjects of a student , calculates the average mark of the student and prints the student's grade. If the average mark is greater than or equal to 90, then the grade is 'A'. If the average mark is 80 and between 80 and 90, then the grade is 'B'. If the average mark is 70 and between 70 and 80, then the grade is 'C'. If the average mark is 60 and between 60 and 70, then the grade is 'D'. If the average mark is 50 and between 50 and 60, then the grade is 'E'. If the average mark is less than 50, then the grade is 'F'.
Input Format:
Input consists of 3 lines. Each line consists of an integer.
Output Format:
Output consists of a single line. Refer sample output for the format.
Sample Input 1 :
45 45 45
Sample Output 1 :
The grade is F
Sample Input 2:
91 95 100
Sample Output 2:
The grade is A
Solution:
General description of program: Given marks in 3 subjects as the input, The task is to compute the average of the marks to display the corresponding grade.
Write a C program to find and output all the roots of a given quadratic equation, for non-zero
coefficients. (Using if…else statement)
Input Format:
Input consists of 3 integers corresponding to the coefficients of the Quadratic equation ax^2 + bx + c
Output Format:
The output consists of the roots of the quadratic equation.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
SAMPLE INPUT OUTPUT 1 :
Enter the coefficients of a, b and c :
1 1 1
-0.50+0.87i and -0.50-0.87i
SAMPLE INPUT OUTPUT 2 :
Enter the coefficients of a, b and c :
3 7 2
-0.33 and -2.00
SAMPLE INPUT OUTPUT 3 :
Enter the coefficients of a, b and c :
1 2 1
-1.00
Solution:
General description of program:
Program: Roots of quadratic equation(QE) Inputs to the program are 3 coefficients (a, b, c) of QE that form QE==> ax^2 + bx + c = 0 Task is to find roots of this QE. Normal mathematical solution for the problem of solving a quadratic equation is implemented here. Using the formula for discriminant computation ( d= b^2 - 4ac) from coefficients, d is calculated. Roots are computed based on the value of d<0, d is equal to 0 or d>0 by applying respective formulae.
Write a C program to simulate a simple calculator that performs arithmetic operations like addition, subtraction, multiplication, and division only on integers. Error message should be reported, if any attempt is made to divide by zero. (Using switch statement)
Input Format:
The first input consists of an integer which corresponds to the first operand, second input consists of an operator(+,-,*,/,%) and the third input consists of an integer which corresponds to the second operand.
Output Format:
Refer Sample Output
Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input 1:
10+5
Sample Output 1:
The sum is 15
Sample Input 2:
10-5
Sample Output 2:
The difference is 5
Sample Input 3:
10*5
Sample Output 3:
The product is 50
Sample Input 4:
10/5
Sample Output 4:
The quotient is 2
Sample Input 5:
10/0
Sample Output 5:
Divide by Zero error
Sample Input 6:
10%5
Sample Output 6:
The remainder is 0
Sample Input 7:
10=5
Sample Output 7:
Invalid Input
Solution:
General description of program: Simple calculator Calculator for addition, subtraction, multiplication, division, modulus operations using switch statements in C. Divide-by-zero condition is separately handled in case of division.