Professional Documents
Culture Documents
printf("%f", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%f", y);
getchar(); return 0; }
Output: 1.000000
Ans: 2
// x will tell how many values scanned.
Note:
y = scanf("%c",&y); is not correct, as discussed in the int (or
float) case, previously. Though, it will run but will give you
incorrect output when you print the value of y.
y=(float) x; /* y=10.0 */
printf("\n%f",c); // 5.75
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
More types: const
const means a variable which doesn't vary – useful for
physical constants or things like pi or e
– You can also declare variables as being constants
– Use the const qualifier:
const double pi=3.1415926;
const int maxlength=2356;
const int val=(3*7+6)*5; #include<stdio.h>
int main()
•(scientific) notation {
(mantissa/exponent) const float n = 6.18e2;
const double n = 6.18e2; printf("%f",n); 618.00
getchar();
return 0;
• 6.18e2 = 6.18x10^2 }
Constants
– Constants are useful for a number of reasons
• Tells the reader of the code that a value does not
change
• Tells the compiler that a value does not change
– The compiler can potentially compile faster code
circum = 2*pi*radius;
getchar();
return 0;
}
radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza