C Programming Exercises on Control Structures
C Programming Exercises on Control Structures
The program calculates the total and average marks for three subjects. Based on the average, it assigns grades: 'O' for average >90 to 100, 'A+' for >80 to 90, 'A' for >70 to 80, 'B+' for >60 to 70, 'B' for 55 to <60, 'C' for 50 to <55, and 'RA' for below 50 .
The program incorrectly uses integer division for the area calculations, failing to store them as floats, and misuses printing format specifiers. For instance, the area of a triangle uses '1/2*b*h' instead of '0.5*b*h'. The program incorrectly uses '%d' instead of '%f' for float outputs: Corrected code: - Replace '1/2' with '0.5' for the triangle area. - Use '%f' in printf for floats. - Remove redundant semicolon after 'switch(ch)' .
The program classifies numbers based on the parity (even or odd) of their last two digits. If both digits are even, it returns 'EE'; if both are odd, it returns 'OO'. If the unit digit (ud) is even and tens digit (td) is odd, it returns 'OE', and the reverse condition also returns 'OE', which is a redundancy due to logic overlap .
The program checks if the year is divisible by 4 and simultaneously either divisible by 400 or not divisible by 100. These conditions are necessary because, traditionally, a year is a leap year if it is divisible by 4. However, if a year is divisible by 100, it is only a leap year if it is also divisible by 400. These adjustments correct for the slight discrepancy between the solar year and the Julian calendar .
An automorphic number is a number whose square ends in the number itself. The program checks if the last digit of the square of 'n' is equal to 'n'. This is accomplished by squaring the input and checking the condition s%10==n. If true, it declares the number automorphic; otherwise, not .