#include #include /* This is a comment enclosed in special symbols */ /* Notice: 1. The way variable names have been chosen. The names make sense. 2. Insert comments about your program. It makes reading of the program easy. 3. Look at the program indentation. Very important. 4. Within a single program some blanks lines are inserted so that the program looks paragraphed. */ #ifdef P1 /* This program converts Celsius to Fahrenheit. */ /* Since Celsius and Fahrenheit both can be real numbers we choose 'float' data type for these. */ main() { float Celsius; float Fahrenheit; printf("Input temperature in Celsius --> "); scanf("%f",&Celsius); Fahrenheit = (9.0/5.0)*Celsius+32; printf("%f Celsius = %f Fahrenheit\n",Celsius,Fahrenheit); } /* If you write only (9/5), compiler does an integer division and produces integral answer of 1. To prevent this write at least one of the two constants as 'float' numbers that is either (9.0/5) or (9/5.0) or (9.0/5.0). */ #endif #ifdef P2 /* This program calculates the time required by a ball to reach ground if dropped from a height h */ main() { float Height; float Time; printf("Input height in meters --> "); scanf("%f",&Height); if ( Height >= 0.0 ) { Time = sqrt(2*Height/9.8); printf("Time required = %f seconds\n",Time); } else printf("Height must be nonnegative\n"); } #endif #ifdef P3 /* This program calculates income taxes. */ /* We dont worry too much about paise. So we choose int data type for income and tax. */ main() { int Income; int Tax; printf("Input income in Rupees (Rounded) --> "); scanf("%d",&Income); /* ^^^ %d here and not %f */ if ( Income >= 150000 ) { Tax = 19000 + (Income - 150000)*0.3; } else { if ( Income >= 60000 ) Tax = 1000 + (Income - 60000)*0.2; else if ( Income >= 50000 ) Tax = (Income - 50000)*0.1; else Tax = 0; } printf("Net Tax = %d Rs\n",Tax); } #endif #ifdef P4 /* This program calculates sine function */ /* All variables are float type. Why? */ main() { float Degrees; float Radians; float SineValue; float Pi; printf("Enter angle in degrees --> "); scanf("%f",&Degrees); /* Calculate value of pi using tan inverse function */ Pi = 4.0 * atan(1.0); Radians = Degrees * Pi / 180.0; SineValue = Radians - pow(Radians,3)/6.0 + pow(Radians,5)/120.0; if ( Radians < Pi / 2 ) printf("sin(%f) = %f\n",Degrees,SineValue); else printf("sin(%f) = %f (Approximately)\n",Degrees,SineValue); } #endif