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
59. Practice
โข Input: an integer of exactly 4 digits (let, 1234)
โข Output: the sum of the digits (10)
60. Problem
โข Input: a, b, c of the equation ๐๐ฅ2 + ๐๐ฅ + ๐ = 0
โข Output: the roots of this equation.
61. Problem
โข Input: a, b, c of the equation ๐๐ฅ2 + ๐๐ฅ + ๐ = 0
โข Output: the roots of this equation.
๐ฅ1 =
โ๐ + ๐2 โ 4๐๐
2๐
๐ฅ2 =
โ๐ โ ๐2 โ 4๐๐
2๐
69. Practice
โข Input: two integers indicating the hours and minutes
of an analog clock
โข Output: the angle between the two indicators.
70. Problem
โข Input: 3 angles(double) of a triangle.
โข Output: a character
โYโ (if valid triangle)
โNโ (if not a valid triangle)
A
B C
A+B+C = ๐
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
๐
๐ถ๐