You are on page 1of 20

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Mathematical Functions

Dr. Yousaf, PIEAS


#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;

y = sin(PI/2.0); // argument is in radian

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

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h> k = floor(15.2);
#include<math.h>
printf(" k = %d\n",k);
#define PI 3.14 // output: 15
j = ceil (15.2);
int main()
{ printf(" j = %d\n",j);
// output: 16
int k, j, r;
double x = 25.0, y, z; r = abs (-67);
printf(" r = %d\n",r);
y = sqrt(x); // output: 67
printf("y = %lf\n", y); // y = 5.000000 getchar();
return 0;
z = pow(y,3); // y^3, z = 125.000000
}
printf(" z = %lf\n", z);

Dr. Yousaf, PIEAS


Dr. Yousaf, PIEAS
The scanf statement
int number, check;
scanf ("%d",&number);
check= number;
//Correct

int number, check;


check= scanf ("%d",&number);
/*The program may run without error.
However, on printing the value of check, it
would not be same as number */

Dr. Muhammad Yousaf Hamza


The scanf statement
#include <stdio.h>
int main(void)
{
int x, y, z;
x = scanf("%d %d", &y, &z);
printf("%d", x);
getchar();
return 0;
}

Ans: 2
// x will tell how many values scanned.

Dr. Muhammad Yousaf Hamza


#include<stdio.h>
int main()
{
char x = 'h', y;
printf("Character x is %c\n", x); // note the use of %c
printf("Please type a character and then press enter\n");

y = getchar(); // It is valid. It will give correct output.

printf("Character y is %c\n", y); // note the use of %c


getchar(); return 0; }

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.

Dr. Yousaf, PIEAS


Type Conversion
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b; // 5.00
printf("\n%f",c); // 5.00
getchar();
return 0;
}
Actually 23/4 = 5.75, but here output is 5.00 In order to have
correct answer (with decimal value), we need type casting.
Dr. Muhammad Yousaf Hamza
Type Conversion
• C allows for conversions between the basic types, implicitly or
explicitly. It is also called casting.
• A cast is a way of telling one variable type to temporarily look
like another.
• Explicit conversion uses the cast operator.
• Example :
int x=10;
float y, z=3.14;

y=(float) x; /* y=10.0 */

x=(int) z; /* x=3 */ OR x = z, both are OK


Dr. Muhammad Yousaf Hamza
Casting of Variables
By using (type) in front of a variable we tell
the variable to act like another type of variable.
We can cast between any type usually.

Dr. Muhammad Yousaf Hamza


Casting of Variables
#include<stdio.h>
int main()
{
int a= 23, b= 4;
float c;
c = a/b;
printf("\n%f",c); // 5.00
Cast ints a and b to be float
c= (float)a/(float)b;
// c = (float)a/b; or c= a/(float)b; are also same.

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 }

Dr. Muhammad Yousaf Hamza


More types: const

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

• Use constants where appropriate

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{

const double pi=3.1415926;


double radius = 4.5, circum ;

circum = 2*pi*radius;

printf("\nCircumference is= %lf", circum); // 28.274334

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More types: const
#include<stdio.h>
int main()
{
const double pi=3.1415926;
float radius = 4.5,circum ;
circum = 2*pi*radius;
printf("\n%f", circum);

radius = 7.3;
pi = 2.9; // Error
circum = 2*pi*radius;
printf("\n%f", circum);
getchar();
return 0;
} Dr. Muhammad Yousaf Hamza

You might also like