SlideShare a Scribd company logo
1 of 76
Structured Programming Language
# include <math.h>
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Advanced problems on Operators
Flow Chart
• This whole slide is based on the following flow chart:
# 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
Problem
• Input: x (double)
• Output: y (double)
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥
Solution
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h>
int main()
{
return 0;
}
Solution
𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h>
int main()
{
double x,y; ///variable declaration
return 0;
}
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;
}
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;
}
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;
}
Practice
• Input: a (double),
b(double) ,
c(double)
• Output: x (double)
𝑥 =
5𝑎2
+ 6𝑏 + 𝑐
2𝑎 + 𝑐
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.
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
𝐾𝑊𝐻
Code Sample
#include <stdio.h>
int main()
{
return 0;
}
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;
}
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;
}
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;
}
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
Code Sample
#include <stdio.h>
int main()
{
return 0;
}
Code Sample
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b); ///taking inputs
return 0;
}
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;
}
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;
}
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;
}
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;
}
Problem
• Input: temperature(double) in °C format.
• Output: temperature(double) in °F format.
Problem
• Input: temperature(double) in °C format.
• Output: temperature(double) in °F format.
𝐶 − 0
100 − 0
=
𝐹 − 32
212 − 32
Code Sample
#include <stdio.h>
int main()
{
return 0;
}
Code Sample
#include <stdio.h>
int main()
{
double c,f;
printf("Enter temperature in Celsius: ");
scanf("%lf",&c); ///taking input Celsius scale temperature
return 0;
}
Code Sample
#include <stdio.h>
int main()
{
double c,f;
printf("Enter temperature in Celsius: ");
scanf("%lf",&c); ///taking input Celsius scale temperature
f= 9*c/5+32; /// processing
return 0;
}
Code Sample
#include <stdio.h>
int main()
{
double c,f;
printf("Enter temperature in Celsius: ");
scanf("%lf",&c); ///taking input Celsius scale temperature
f= 9*c/5+32; /// processing
printf("%.2lf Celsius = %.2lf Fahrenheit",c,f); ///output
return 0;
}
Problem
• Input: r (double, radius),
h (double, height)
• Output: the volume of a sphere
Problem
• Input: r (double, radius),
h (double, height)
• Output: the volume of a sphere
𝜋𝑟2ℎ
Code Sample
#include <stdio.h>
int main()
{
return 0;
}
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;
}
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;
}
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;
}
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)
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)
𝑎𝑟𝑒𝑎 =
1
2
𝑥1 − 𝑥2 ∗ 𝑦2 − 𝑦3 − 𝑦1 − 𝑦2 ∗ 𝑥2 − 𝑥3
=
1
2
[𝑥1 ∗ 𝑦2 − 𝑦3 + 𝑥2 ∗ 𝑦3 − 𝑦1 + 𝑥3 ∗ 𝑦1 − 𝑦2 ]
Code Sample#include <stdio.h>
int main()
{
return 0;
}
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;
}
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;
}
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;
}
Practice
• Input: a , b (base and height of a right angle triangle)
• Output: the value of the hypotenuse
Practice
• Input: a , b (base and height of a right angle triangle)
• Output: the value of the hypotenuse
ℎ𝑦𝑝 = 𝑏𝑎𝑠𝑒2 + ℎ𝑒𝑖𝑔ℎ𝑡2
base
hypotenuse
height
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
Code Sample#include <stdio.h>
int main()
{
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int input;
scanf("%d",&input); ///taking input
int day,hr,min,sec,rem_sec;
return 0;
}
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;
}
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;
}
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;
}
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;
}
Problem
• Input: an integer of exactly 4 digits (for example, 1234)
• Output: 1 thousands, 2 hundreds and 34
Code Sample#include <stdio.h>
int main()
{
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int num;
scanf("%4d",&num); ///taking input
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int num;
scanf("%4d",&num); ///taking input
int thous,hund,rem,temp; ///declaring variables
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int num;
scanf("%4d",&num); ///taking input
int thous,hund,rem,temp; ///declaring variables
thous=num/1000; /// calculating thousands
temp=num%1000; /// remaining amounts
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int num;
scanf("%4d",&num); ///taking input
int thous,hund,rem,temp; ///declaring variables
thous=num/1000; /// calculating thousands
temp=num%1000; /// remaining amounts
hund=temp/100; ///calculating hundreds
rem=temp%100; ///final remaining amounts
return 0;
}
Code Sample#include <stdio.h>
int main()
{
int num;
scanf("%4d",&num); ///taking input
int thous,hund,rem,temp; ///declaring variables
thous=num/1000; /// calculating thousands
temp=num%1000; /// remaining amounts
hund=temp/100; ///calculating hundreds
rem=temp%100; ///final remaining amounts
printf("%d thousands, %d hundreds and %dn",thous,hund,rem); ///output
return 0;
}
Practice
• Input: an integer of exactly 4 digits (let, 1234)
• Output: the sum of the digits (10)
Problem
• Input: a, b, c of the equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0
• Output: the roots of this equation.
Problem
• Input: a, b, c of the equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0
• Output: the roots of this equation.
𝑥1 =
−𝑏 + 𝑏2 − 4𝑎𝑐
2𝑎
𝑥2 =
−𝑏 − 𝑏2 − 4𝑎𝑐
2𝑎
Code Sample
#include <stdio.h>
#include <math.h>
int main()
{
return 0;
}
Code Sample
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c; ///ax^2+bx+c=0, coefficient input
scanf("%d %d %d",&a,&b,&c);
return 0;
}
Code Sample
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c; ///ax^2+bx+c=0, coefficient input
scanf("%d %d %d",&a,&b,&c);
double x1,x2; ///roots calculation
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
return 0;
}
Code Sample
#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c; ///ax^2+bx+c=0, coefficient input
scanf("%d %d %d",&a,&b,&c);
double x1,x2; ///roots calculation
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
printf("Solutions are %lf and %lfn",x1,x2); ///output
return 0;
}
Practice
• Input: a1, b1, c1 and a2, b2, c2
where,
𝑎1 𝑥 + 𝑏1 𝑦 + 𝑐1 = 0
𝑎2 𝑥 + 𝑏2 𝑦 + 𝑐2 = 0
• Output: the intersection point of that two straight
lines.
Practice
• Input: a1, b1, c1 and a2, b2, c2
where,
𝑎1 𝑥 + 𝑏1 𝑦 + 𝑐1 = 0
𝑎2 𝑥 + 𝑏2 𝑦 + 𝑐2 = 0
• Output: the intersection point of that two straight
lines.
𝑥 =
𝑏1 𝑐2 − 𝑏2 𝑐1
𝑎1 𝑏2 − 𝑎2 𝑏1
𝑦 =
𝑐1 𝑎2 − 𝑐2 𝑎1
𝑎1 𝑏2 − 𝑎2 𝑏1
Code Sample
Practice
• Input: two integers indicating the hours and minutes
of an analog clock
• Output: the angle between the two indicators.
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 = 𝜋
Code Sample
#include <stdio.h>
int main()
{
double A,B,C;
scanf("%lf %lf %lf",&A,&B,&C);
char result;
result=(A+B+C-180.0 < 0.00000001)?'Y':'N';
printf("%cn",result);
return 0;
}
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
Practice
• Input: Grade points(double) of 6 subjects and their
corresponding credit hours(double).
• Output: weighted GPA
𝐺𝑃𝐴 =
σ𝑖=1
𝑛
𝐶𝑖 ∗ 𝐺𝑖
σ𝑖=1
𝑛
𝐶𝑖
Course Credits, 𝑪𝒊 Grade Points, 𝑮𝒊 𝑪𝒊 * 𝑮𝒊
CSE 100 2.00 4.00 8.00
EEE 163 3.00 4.00 12.00
EEE 164 1.50 3.75 5.625
MATH 141 3.00 3.00 9.00
ME 160 1.50 3.50 5.250
ME 165 3.00 4.00 12.00
PHY 109 4.00 3.75 15.00
PHY 102 1.50 3.50 5.250
Total 19.50 72.125
GPA calculation
𝐺𝑃𝐴 =
72.125
19.50
= 3.7
Practice
• Input: Total credits(double) of 4 semesters and their
corresponding GPA(double) earned.
• Output: Cumulative GPA
C𝐺𝑃𝐴 =
σ𝑖=1
𝑛
𝑇𝐶𝑖 ∗ 𝐺𝑃𝐴𝑖
σ𝑖=1
𝑛
𝑇𝐶𝑖
Semester Total Credits, 𝐓𝑪𝒊 GPA earned, 𝑮𝑷𝑨𝒊 𝑻𝑪𝒊 * 𝑮𝑷𝑨𝒊
1 19.50 3.70 72.150
2 20.50 3.93 80.565
3 21.25 3.96 84.150
4 20.25 4.00 81.00
Total 81.50 317.865
CGPA calculation
𝐶𝐺𝑃𝐴 =
317.865
81.50
= 3.90

More Related Content

What's hot (20)

Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
C++ file
C++ fileC++ file
C++ file
 
Struct examples
Struct examplesStruct examples
Struct examples
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
C program
C programC program
C program
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Code optimization
Code optimization Code optimization
Code optimization
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
C Programming
C ProgrammingC Programming
C Programming
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Ray Tracing with ZIO
Ray Tracing with ZIORay Tracing with ZIO
Ray Tracing with ZIO
 

Similar to SPL 6.1 | Advanced problems on Operators and Math.h function in C

Similar to SPL 6.1 | Advanced problems on Operators and Math.h function in C (20)

Muzzammilrashid
MuzzammilrashidMuzzammilrashid
Muzzammilrashid
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
901131 examples
901131 examples901131 examples
901131 examples
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Tu1
Tu1Tu1
Tu1
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
C programming
C programmingC programming
C programming
 
C file
C fileC file
C file
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
C lab
C labC lab
C lab
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
C lab programs
C lab programsC lab programs
C lab programs
 

More from Mohammad Imam Hossain

DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchMohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaMohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaMohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckMohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

SPL 6.1 | Advanced problems on Operators and Math.h function in C

  • 1. Structured Programming Language # include <math.h> Mohammad Imam Hossain, Lecturer, CSE, UIU Advanced problems on Operators
  • 2. Flow Chart • This whole slide is based on the following flow chart:
  • 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 𝑥 − 𝑒 𝑥 + 𝑥
  • 5. Solution 𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h> int main() { return 0; }
  • 6. Solution 𝑦 = 5𝑥4 + 3𝑥2 + 7 tan 𝑥 − 𝑒 𝑥 + 𝑥#include <stdio.h> int main() { double x,y; ///variable declaration return 0; }
  • 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 𝐾𝑊𝐻
  • 13. Code Sample #include <stdio.h> int main() { return 0; }
  • 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
  • 18. Code Sample #include <stdio.h> int main() { return 0; }
  • 19. Code Sample #include <stdio.h> int main() { int a,b; scanf("%d %d",&a,&b); ///taking inputs return 0; }
  • 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; }
  • 24. Problem • Input: temperature(double) in °C format. • Output: temperature(double) in °F format.
  • 25. Problem • Input: temperature(double) in °C format. • Output: temperature(double) in °F format. 𝐶 − 0 100 − 0 = 𝐹 − 32 212 − 32
  • 26. Code Sample #include <stdio.h> int main() { return 0; }
  • 27. Code Sample #include <stdio.h> int main() { double c,f; printf("Enter temperature in Celsius: "); scanf("%lf",&c); ///taking input Celsius scale temperature return 0; }
  • 28. Code Sample #include <stdio.h> int main() { double c,f; printf("Enter temperature in Celsius: "); scanf("%lf",&c); ///taking input Celsius scale temperature f= 9*c/5+32; /// processing return 0; }
  • 29. Code Sample #include <stdio.h> int main() { double c,f; printf("Enter temperature in Celsius: "); scanf("%lf",&c); ///taking input Celsius scale temperature f= 9*c/5+32; /// processing printf("%.2lf Celsius = %.2lf Fahrenheit",c,f); ///output return 0; }
  • 30. Problem • Input: r (double, radius), h (double, height) • Output: the volume of a sphere
  • 31. Problem • Input: r (double, radius), h (double, height) • Output: the volume of a sphere 𝜋𝑟2ℎ
  • 32. Code Sample #include <stdio.h> int main() { 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)
  • 37. 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) 𝑎𝑟𝑒𝑎 = 1 2 𝑥1 − 𝑥2 ∗ 𝑦2 − 𝑦3 − 𝑦1 − 𝑦2 ∗ 𝑥2 − 𝑥3 = 1 2 [𝑥1 ∗ 𝑦2 − 𝑦3 + 𝑥2 ∗ 𝑦3 − 𝑦1 + 𝑥3 ∗ 𝑦1 − 𝑦2 ]
  • 38. Code Sample#include <stdio.h> int main() { return 0; }
  • 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
  • 45. Code Sample#include <stdio.h> int main() { return 0; }
  • 46. Code Sample#include <stdio.h> int main() { int input; scanf("%d",&input); ///taking input return 0; }
  • 47. Code Sample#include <stdio.h> int main() { int input; scanf("%d",&input); ///taking input int day,hr,min,sec,rem_sec; return 0; }
  • 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
  • 53. Code Sample#include <stdio.h> int main() { return 0; }
  • 54. Code Sample#include <stdio.h> int main() { int num; scanf("%4d",&num); ///taking input return 0; }
  • 55. Code Sample#include <stdio.h> int main() { int num; scanf("%4d",&num); ///taking input int thous,hund,rem,temp; ///declaring variables return 0; }
  • 56. Code Sample#include <stdio.h> int main() { int num; scanf("%4d",&num); ///taking input int thous,hund,rem,temp; ///declaring variables thous=num/1000; /// calculating thousands temp=num%1000; /// remaining amounts return 0; }
  • 57. Code Sample#include <stdio.h> int main() { int num; scanf("%4d",&num); ///taking input int thous,hund,rem,temp; ///declaring variables thous=num/1000; /// calculating thousands temp=num%1000; /// remaining amounts hund=temp/100; ///calculating hundreds rem=temp%100; ///final remaining amounts return 0; }
  • 58. Code Sample#include <stdio.h> int main() { int num; scanf("%4d",&num); ///taking input int thous,hund,rem,temp; ///declaring variables thous=num/1000; /// calculating thousands temp=num%1000; /// remaining amounts hund=temp/100; ///calculating hundreds rem=temp%100; ///final remaining amounts printf("%d thousands, %d hundreds and %dn",thous,hund,rem); ///output return 0; }
  • 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𝑎
  • 62. Code Sample #include <stdio.h> #include <math.h> int main() { return 0; }
  • 63. Code Sample #include <stdio.h> #include <math.h> int main() { int a,b,c; ///ax^2+bx+c=0, coefficient input scanf("%d %d %d",&a,&b,&c); return 0; }
  • 64. Code Sample #include <stdio.h> #include <math.h> int main() { int a,b,c; ///ax^2+bx+c=0, coefficient input scanf("%d %d %d",&a,&b,&c); double x1,x2; ///roots calculation x1=(-b+sqrt(b*b-4*a*c))/(2*a); x2=(-b-sqrt(b*b-4*a*c))/(2*a); return 0; }
  • 65. Code Sample #include <stdio.h> #include <math.h> int main() { int a,b,c; ///ax^2+bx+c=0, coefficient input scanf("%d %d %d",&a,&b,&c); double x1,x2; ///roots calculation x1=(-b+sqrt(b*b-4*a*c))/(2*a); x2=(-b-sqrt(b*b-4*a*c))/(2*a); printf("Solutions are %lf and %lfn",x1,x2); ///output return 0; }
  • 66. Practice • Input: a1, b1, c1 and a2, b2, c2 where, 𝑎1 𝑥 + 𝑏1 𝑦 + 𝑐1 = 0 𝑎2 𝑥 + 𝑏2 𝑦 + 𝑐2 = 0 • Output: the intersection point of that two straight lines.
  • 67. Practice • Input: a1, b1, c1 and a2, b2, c2 where, 𝑎1 𝑥 + 𝑏1 𝑦 + 𝑐1 = 0 𝑎2 𝑥 + 𝑏2 𝑦 + 𝑐2 = 0 • Output: the intersection point of that two straight lines. 𝑥 = 𝑏1 𝑐2 − 𝑏2 𝑐1 𝑎1 𝑏2 − 𝑎2 𝑏1 𝑦 = 𝑐1 𝑎2 − 𝑐2 𝑎1 𝑎1 𝑏2 − 𝑎2 𝑏1
  • 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 = 𝜋
  • 71. Code Sample #include <stdio.h> int main() { double A,B,C; scanf("%lf %lf %lf",&A,&B,&C); char result; result=(A+B+C-180.0 < 0.00000001)?'Y':'N'; printf("%cn",result); return 0; }
  • 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 𝑛 𝐶𝑖
  • 74. Course Credits, 𝑪𝒊 Grade Points, 𝑮𝒊 𝑪𝒊 * 𝑮𝒊 CSE 100 2.00 4.00 8.00 EEE 163 3.00 4.00 12.00 EEE 164 1.50 3.75 5.625 MATH 141 3.00 3.00 9.00 ME 160 1.50 3.50 5.250 ME 165 3.00 4.00 12.00 PHY 109 4.00 3.75 15.00 PHY 102 1.50 3.50 5.250 Total 19.50 72.125 GPA calculation 𝐺𝑃𝐴 = 72.125 19.50 = 3.7
  • 75. Practice • Input: Total credits(double) of 4 semesters and their corresponding GPA(double) earned. • Output: Cumulative GPA C𝐺𝑃𝐴 = σ𝑖=1 𝑛 𝑇𝐶𝑖 ∗ 𝐺𝑃𝐴𝑖 σ𝑖=1 𝑛 𝑇𝐶𝑖
  • 76. Semester Total Credits, 𝐓𝑪𝒊 GPA earned, 𝑮𝑷𝑨𝒊 𝑻𝑪𝒊 * 𝑮𝑷𝑨𝒊 1 19.50 3.70 72.150 2 20.50 3.93 80.565 3 21.25 3.96 84.150 4 20.25 4.00 81.00 Total 81.50 317.865 CGPA calculation 𝐶𝐺𝑃𝐴 = 317.865 81.50 = 3.90