3. # include <math.h>
The math.h header declares a set of functions
to compute common mathematical operations
and transformations.
Ref: MathDotH.pdf
“ Computers are good
at following
instructions, but not at
reading your mind. ” -
Donald Knuth
4. Problem
• Input: x (double)
• Output: y (double)
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥
7. Solution
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h>
int main()
{
double x,y; ///variable declaration
printf("Please enter the value of x: ");
scanf(" %lf",&x); ///taking input the value of x
return 0;
}
8. Solution
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h>
#include <math.h>
int main()
{
double x,y; ///variable declaration
printf("Please enter the value of x: ");
scanf(" %lf",&x); ///taking input the value of x
y=5*pow(x,4)+3*x*x+sqrt(7)*tan(x)-exp(x)+ceil(fabs(x)); ///Processing
return 0;
}
9. Solution
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h>
#include <math.h>
int main()
{
double x,y; ///variable declaration
printf("Please enter the value of x: ");
scanf(" %lf",&x); ///taking input the value of x
y=5*pow(x,4)+3*x*x+sqrt(7)*tan(x)-exp(x)+ceil(fabs(x)); ///Processing
printf("The resulting value is %lfn",y); ///printing output
return 0;
}
10. Practice
• Input: a (double),
b(double) ,
c(double)
• Output: x (double)
𝑥 =
5𝑎2
+ 6𝑏 + 𝑐
2𝑎 + 𝑐
11. Problem
• Input: p (integer, no of watts of a light),
t (double, no of hours it is turned on)
• Output: no of B.O.T units in kwh format.
12. Problem
• Input: p (integer, no of watts of a light),
t (double, no of hours it is turned on)
• Output: no of B.O.T units in kwh format.
units =
𝑝𝑡
1000
𝐾𝑊𝐻
14. Code Sample
#include <stdio.h>
int main()
{
double p,t,result;
printf("Enter the power(in watts) & used time(in hours): ");
scanf("%lf %lf",&p,&t); ///taking input
return 0;
}
15. Code Sample
#include <stdio.h>
int main()
{
double p,t,result;
printf("Enter the power(in watts) & used time(in hours): ");
scanf("%lf %lf",&p,&t); ///taking input
result=p*t/1000; ///calculating no of units
return 0;
}
16. Code Sample
#include <stdio.h>
int main()
{
double p,t,result;
printf("Enter the power(in watts) & used time(in hours): ");
scanf("%lf %lf",&p,&t); ///taking input
result=p*t/1000; ///calculating no of units
printf("No of units: %.2lfn",result); ///output
return 0;
}
17. Problem
• Write a program that will initialize two integer types
of variables (a ,b) and interchange(swap) the values
between them and finally show the output.
• Input: a=10, b=20
• Output: a=20, b=10
20. Code Sample
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b); ///taking inputs
int temp; /// to hold temporary value
temp=a; /// saving a into temp
return 0;
}
21. Code Sample
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b); ///taking inputs
int temp; /// to hold temporary value
temp=a; /// saving a into temp
a=b; ///now a has the new value of b
return 0;
}
22. Code Sample
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b); ///taking inputs
int temp; /// to hold temporary value
temp=a; /// saving a into temp
a=b; ///now a has the new value of b
b=temp; ///now b has the value of temp(old value of a)
return 0;
}
23. Code Sample
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b); ///taking inputs
int temp; /// to hold temporary value
temp=a; /// saving a into temp
a=b; ///now a has the new value of b
b=temp; ///now b has the value of temp(old value of a)
printf("a=%d and b=%dn",a,b); ///output
return 0;
}
33. Code Sample
#include <stdio.h>
int main()
{
double radius,height,volume;
printf("please enter the radius and height of a cylinder: ");
scanf("%lf %lf",&radius,&height); ///taking inputs
return 0;
}
34. Code Sample
#include <stdio.h>
#define PI 3.1416
int main()
{
double radius,height,volume;
printf("please enter the radius and height of a cylinder: ");
scanf("%lf %lf",&radius,&height); ///taking inputs
volume = PI*radius*radius*height; ///calculating volume
return 0;
}
35. Code Sample
#include <stdio.h>
#define PI 3.1416
int main()
{
double radius,height,volume;
printf("please enter the radius and height of a cylinder: ");
scanf("%lf %lf",&radius,&height); ///taking inputs
volume = PI*radius*radius*height; ///calculating volume
printf("The volume of cylinder is %.3lfn",volume); ///output
return 0;
}
36. Problem
• Input: 6 floating point numbers
(x1, y1),
(x2, y2),
(x3, y3)
• Output: area (double, area of a triangle)
(𝑥1, 𝑦1)
(𝑥2, 𝑦2) (𝑥3, 𝑦3)
39. Code Sample#include <stdio.h>
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter the 3 vertices of a triangle: ");
scanf("%lf %lf",&x1,&y1);
scanf("%lf %lf",&x2,&y2);
scanf("%lf %lf",&x3,&y3); ///taking input
return 0;
}
40. Code Sample#include <stdio.h>
#include <math.h> ///for function fabs
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter the 3 vertices of a triangle: ");
scanf("%lf %lf",&x1,&y1);
scanf("%lf %lf",&x2,&y2);
scanf("%lf %lf",&x3,&y3); ///taking input
double area;
area=0.5*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));///calculating
return 0;
}
41. Code Sample#include <stdio.h>
#include <math.h> ///for function fabs
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter the 3 vertices of a triangle: ");
scanf("%lf %lf",&x1,&y1);
scanf("%lf %lf",&x2,&y2);
scanf("%lf %lf",&x3,&y3); ///taking input
double area;
area=0.5*(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2));///calculating
printf("The area is %.3lf sq units.n“,fabs(area));///outputs
return 0;
}
42. Practice
• Input: a , b (base and height of a right angle triangle)
• Output: the value of the hypotenuse
43. Practice
• Input: a , b (base and height of a right angle triangle)
• Output: the value of the hypotenuse
ℎ𝑦𝑝 = 𝑏𝑎𝑠𝑒2 + ℎ𝑒𝑖𝑔ℎ𝑡2
base
hypotenuse
height
44. Problem
• Input: t (integer, time in seconds)
• Output: ddd days hh hours mm minutes and ss seconds
• Sample I/O:
Input: 123456
Output: 001 days 10 hours 17 minutes and 36 seconds
“ First, solve the
problem. Then,
write the code. ” -
John Johnson
48. Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
int day,hr,min,sec,rem_sec;
day=input/(24*60*60); ///calculating no of days
rem_sec=input%(24*60*60); ///the remaining seconds
return 0;
}
49. Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
int day,hr,min,sec,rem_sec;
day=input/(24*60*60); ///calculating no of days
rem_sec=input%(24*60*60); ///the remaining seconds
hr=rem_sec/(60*60); ///calculating no of hours
rem_sec=rem_sec%(60*60); ///remaining seconds
return 0;
}
50. Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
int day,hr,min,sec,rem_sec;
day=input/(24*60*60); ///calculating no of days
rem_sec=input%(24*60*60); ///the remaining seconds
hr=rem_sec/(60*60); ///calculating no of hours
rem_sec=rem_sec%(60*60); ///remaining seconds
min=rem_sec/60; ///calculating no of minutes
sec=rem_sec%60; ///final remaining seconds
return 0;
}
51. Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
int day,hr,min,sec,rem_sec;
day=input/(24*60*60); ///calculating no of days
rem_sec=input%(24*60*60); ///the remaining seconds
hr=rem_sec/(60*60); ///calculating no of hours
rem_sec=rem_sec%(60*60); ///remaining seconds
min=rem_sec/60; ///calculating no of minutes
sec=rem_sec%60; ///final remaining seconds
printf("%03d days %02d hours %02d minutes and %02d seconds",day,hr,min,sec); ///output
return 0;
}
52. Problem
• Input: an integer of exactly 4 digits (for example, 1234)
• Output: 1 thousands, 2 hundreds and 34
72. Practice
• Input: 3 double numbers from the user
• Output: average (double) of that 3 numbers.
Sample I/O:
• Input: 10.0,20.0,30.0
• Output: 20.000000
73. Practice
• Input: Grade points(double) of 6 subjects and their
corresponding credit hours(double).
• Output: weighted GPA
𝐺𝑃𝐴 =
σ𝑖=1
𝑛
𝐶𝑖 ∗ 𝐺𝑖
σ𝑖=1
𝑛
𝐶𝑖