SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN 
SJEM2231 STRUCTURED PROGRAMMING 
TUTORIAL 4/ LAB 4 (27th OCTOBER 2014) 
QUESTION 1 
Write a function program to find the factorial of a given number (N!) 
//Using for loop: 
#include <iostream> 
using namespace std; 
// Function definition (header and body) 
int factorial(int N) 
{ 
int a, factorial=1; 
for(a=1; a<=N; a++) 
{ 
factorial = factorial*a; 
} 
return factorial; // This value is returned to caller 
} 
int main() 
{ 
int N; 
cout << "Please enter a number = "; 
cin >> N; 
cout << "The factorial for " << N << " is " << factorial(N) << endl; 
return 0; 
} 
//Output for for loop: 
Please enter a number = 6 
The factorial for 6 is 720
//Using if else statement 
#include <iostream> 
using namespace std; 
long factorial ( int a ) 
{ 
if (a>1) 
return (a*factorial(a-1)); 
else 
return 1 ; 
} 
int main () 
{ 
long number; 
cout << "Please insert a number : "; 
cin >> number; 
cout << number <<"! =" << factorial(number)<<endl; 
return 0; 
} 
//Output of if-else statement 
Please insert a number : 8 
8! =40320
// Using the first way of while loop: 
#include <iostream> 
using namespace std; 
// Function definition (header and body) 
int factorial(int N) 
{ 
int factorial=1; 
while ( N > 0) 
/* while loop continues until test condition N>0 is true */ 
{ 
factorial= factorial*N; 
--N; 
} 
return factorial; // This value is returned to caller 
} 
int main() 
{ 
int N; 
cout << "Please enter a number = "; 
cin >> N; 
cout << "The factorial for " << N << " is " << factorial(N) << endl; 
return 0; 
}
//Output of first way of using while loop: 
Please enter a number = 8 
The factorial for 8 is 40320 
//Using the second way of while loop: 
#include <iostream> 
using namespace std; 
// Function definition (header and body) 
int factorial(int N) 
{ 
int n=1,factorial=1; 
while ( n <= N) 
/* while loop continues until test condition N>0 is true */ 
{ 
factorial= factorial*n; 
n++; 
} 
return factorial; // This value is returned to caller 
} 
int main() 
{ 
int N; 
cout << "Please enter a number = "; 
cin >> N; 
cout << "The factorial for " << N << " is " << factorial(N) << endl;
return 0; 
} 
//Output using second way of while loop 
Please enter a number = 8 
The factorial for 8 is 40320 
QUESTION2 
Using function program ODD() and EVEN(), find the sum of odd (1+3+ ... +N) and even (2+4+6+…+N) numbers less than N 
//Using for loop 
#include <iostream> 
using namespace std; 
// Function definition (header and body) 
int sum_even(int N) 
{ 
int i, sum_even=0; 
for(i=1; i<N; i++) 
{ 
if(i%2==0) 
sum_even=sum_even +i; 
} 
return sum_even; 
} 
int sum_odd(int N) 
{ 
int i,sum_odd=0; 
for(i=1; i<N; i++) 
{ 
if(i%2!=0) 
sum_odd = sum_odd +i; 
}
return sum_odd; 
} 
int main() 
{ 
int N; 
cout<<"Please enter a number N: "; 
cin>>N; 
cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; 
cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl; 
return 0; 
} 
//Output by using for loop: 
Please enter a number N: 10 
The summation of even number of N is: 20 
The summation of odd number of N is: 25 
//Using while loop 
#include <iostream> 
using namespace std; 
// Function definition (header and body) 
int sum_even(int N) 
{ 
int i=1, sum_even=0; 
while (i< N)
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
i++; 
} 
return sum_even; 
} 
int sum_odd(int N) 
{ 
int i=1,sum_odd=0; 
while (i< N) 
{ 
if (i%2 !=0) 
sum_odd = sum_odd + i; 
i++; 
} 
return sum_odd; 
} 
int main() 
{ 
int N; 
cout<<"Please enter a number N: "; 
cin>>N; 
cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; 
cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl;
return 0; 
} 
//Output for while loop 
Please enter a number N: 8 
The summation of even number of N is: 12 
The summation of odd number of N is: 16 
//Using do while loop: 
#include <iostream> 
using namespace std; 
/* Function definition (header and body) */ 
int sum_even(int N) 
{ 
int i=1, sum_even=0; 
do 
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
i++; 
} while (i< N); 
return sum_even; 
} 
int sum_odd(int N) 
{ 
int i=1,sum_odd=0; 
do
{ 
if (i%2 !=0) 
sum_odd = sum_odd + i; 
i++; 
} while (i< N); 
return sum_odd; 
} 
int main() 
{ 
int N; 
cout<<"Please enter a number N: "; 
cin>>N; 
cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; 
cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl; 
return 0; 
} 
//Output for do while loop 
Please enter a number N: 8 
The summation of even number of N is: 12 
The summation of odd number of N is: 16
QUESTION 3 
Write a function program to solve the quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown. 
#include <iostream> 
#include <cmath> 
using namespace std; 
/**function prototype or declaration, when int main() is placed on the top**/ 
float discriminant(float a, float b, float c); 
void TWOREAL(float a,float b,float c); 
void ONEREAL(float a,float b,float c); //also called as two equal solution 
void COMPLEX(float a,float b,float c); 
int main() 
{ 
float a, b, c; 
cout << "This program is used to solve Quadratic Equationn"; 
cout << "in the form of ax^2 + bx +cnn"; 
do 
{ 
cout << "Enter value a:"; 
cin >> a; 
} 
while (a == 0); // this is a condition where value of a cannot equal to 0 
cout << "Enter value b:"; 
cin >> b; 
cout << "Enter value c:";
cin >> c; 
if (discriminant(a,b,c) > 0) 
TWOREAL(a,b,c); 
else if (discriminant(a,b,c) == 0) 
ONEREAL(a,b,c); 
else 
COMPLEX(a,b,c); 
return 0; 
} 
float discriminant(float a, float b, float c) 
{ 
float d = b*b-4*a*c; 
return d; 
} 
void TWOREAL(float a,float b,float c) 
{ 
float x1,x2; 
x1 = (-b + sqrtf(discriminant(a,b,c)) )/(2*a); 
x2 = (-b - sqrtf(discriminant(a,b,c)) )/(2*a); 
cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; 
cout << "It has TWO REAL solution which are " << x1 << " , " << x2 << endl; 
}
void ONEREAL(float a,float b,float c) // or we can call it as two equal solution 
{ 
float x1; 
x1 = -b/(2*a); 
cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; 
cout << "It has ONE REAL solution which is " << x1 << endl; 
} 
void COMPLEX(float a,float b,float c) 
{ 
float x1,x2; 
x1 = -b/(2*a); 
x2 = sqrtf(-discriminant(a,b,c))/(2*a); 
cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; 
cout << "It has TWO COMPLEX solutions which are "; 
cout << x1 << "+" << x2 << "i , "<< x1 << -x2 << "i" << endl; 
} 
// Output for Question 3: 
Output1 : 
This program is used to solve Quadratic Equation 
in the form of ax^2 + bx +c 
Enter value a:1 
Enter value b:6
Enter value c:8 
The DISCRIMINANT is 4 
It has TWO REAL solution which are -2 , -4 
Output2 : 
This program is used to solve Quadratic Equation 
in the form of ax^2 + bx +c 
Enter value a:2 
Enter value b:3 
Enter value c:4 
The DISCRIMINANT is -23 
It has TWO COMPLEX solutions which are -0.75+1.19896i , -0.75-1.19896i 
Output 3: 
This program is used to solve Quadratic Equation 
in the form of ax^2 + bx +c 
Enter value a:1 
Enter value b:8 
Enter value c:16 
The DISCRIMINANT is 0 
It has ONE REAL solution which is -4 
QUESTION 4 
Write a function program to find the mean (average) of N numbers. 
/*The programming codes below are to calculate summation of(1+2+3…N)*/ 
//Using for loop: 
#include <iostream> 
using namespace std; 
float average(int N)
{ 
int a,b,sum=0; 
float result; 
for ( a=1;a<=N;a++) 
{ 
cout << “ Value “ << a << “ is: “ ; 
cin >> b; 
sum=sum+b; 
result=float (sum)/N; 
} 
return (result); 
} 
int main () 
{ 
int N; 
cout << "Please insert a value,N : "; 
cin >> N; 
cout << "The average of N values is " << average(N) << endl; 
return 0; 
} 
//Output for for loop : 
Please insert a value,N : 5 
Value 1 is:10 
Value 2 is:3 
Value 3 is:7 
Value 4 is:2 
Value 5 is:114 
The average of N values is 27.2 
//Using while loop 
#include <iostream> 
using namespace std; 
float average(int N)
{ 
int a=1,b, sum=0; 
float result; 
while(a<=N) 
{ 
cout << "Value " << a << " is: " ; 
cin >> b; 
sum= sum + b ; 
a++; 
result=float (sum)/N; 
} 
return (result); 
} 
int main () 
{ 
int N; 
cout << "Please insert a value N : "; 
cin >> N; 
cout << "The average of N values is " << average(N) << endl; 
return 0; 
} //Output for while loop: 
Please insert a value,N : 5 
Value 1 is:10 
Value 2 is:3 
Value 3 is:7 
Value 4 is:2 
Value 5 is:114 
The average of N values is 27.2
//Using do while loop 
#include <iostream> 
using namespace std; 
float average(int N) 
{ 
int a=1, b, sum=0; 
float result; 
do 
{ 
cout << "Value " << a << " is: " ; 
cin >> b; 
sum= sum + b ; 
a++; 
result=float (sum)/N; 
} while(a<=N) ; 
return (result); 
} 
int main () 
{ 
int N; 
cout << "Please insert a value N : "; 
cin >> N; 
cout << "The average of N values is " << average(N) << endl; 
return 0; 
}
//Output for do while loop 
Please insert a value,N : 5 
Value 1 is:10 
Value 2 is:3 
Value 3 is:7 
Value 4 is:2 
Value 5 is:114 
The average of N values is 27.2 
QUESTION 5 
Using the function program, calculate the formula C(n,k )= ( ) 
#include <iostream> 
using namespace std; 
/*** function prototype or declaration***/ 
int fact(int n); 
void COMB(int n,int k); 
int main () 
{ 
int n,k; 
cout << "This program is used to calculate C(n,k)." << endl; 
do 
{ 
cout << "Value of n should be larger than k, n>k" << endl; 
cout << "Enter positive integer n:"; 
cin >> n; 
cout << "Enter positive integer k:"; 
cin >> k; 
}
while (n < 0 || k<0 || n<k); 
COMB(n,k); 
return 0; 
} 
int factorial(int n) 
{ 
int fact = 1; 
if (n == 0) 
return 1; 
else 
{ 
for (int i = 1; i <= n; i++) 
fact = fact * i; 
return fact; 
} 
} 
void COMB(int n,int k) 
{ 
int comb = factorial(n)/(factorial(k)*factorial(n-k)); 
cout << "C(" << n << "," << k << ")= " << comb << endl; 
} 
//Output for Question 5: 
This program is used to calculate C(n,k). 
Value of n should be larger than k, n>k 
Enter positive integer n:5
Enter positive integer k:7 
Value of n should be larger than k, n>k 
Enter positive integer n:-4 
Enter positive integer k:-7 
Value of n should be larger than k, n>k 
Enter positive integer n:3 
Enter positive integer k:2 
C(3,2)= 3

Más contenido relacionado

La actualidad más candente

Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Oops practical file
Oops practical fileOops practical file
Oops practical fileAnkit Dixit
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsVishvjeet Yadav
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2Mouna Guru
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - HarshHarsh Sharma
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programsAbhishek Jena
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 

La actualidad más candente (20)

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Oop1
Oop1Oop1
Oop1
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ programs
C++ programsC++ programs
C++ programs
 
Cpp programs
Cpp programsCpp programs
Cpp programs
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Oops practical file
Oops practical fileOops practical file
Oops practical file
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - Harsh
 
Opp compile
Opp compileOpp compile
Opp compile
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Travel management
Travel managementTravel management
Travel management
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
 
Cquestions
Cquestions Cquestions
Cquestions
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 

Similar a C++ TUTORIAL 4

54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)Sena Nama
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdfT17Rockstar
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Syed Umair
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Solved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsSolved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsMuhammadTalha436
 
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.pptxssuser3cbb4c
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 

Similar a C++ TUTORIAL 4 (20)

54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Solved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsSolved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ Exams
 
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
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
12
1212
12
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 

Último

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
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
 

Último (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 

C++ TUTORIAL 4

  • 1. NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN SJEM2231 STRUCTURED PROGRAMMING TUTORIAL 4/ LAB 4 (27th OCTOBER 2014) QUESTION 1 Write a function program to find the factorial of a given number (N!) //Using for loop: #include <iostream> using namespace std; // Function definition (header and body) int factorial(int N) { int a, factorial=1; for(a=1; a<=N; a++) { factorial = factorial*a; } return factorial; // This value is returned to caller } int main() { int N; cout << "Please enter a number = "; cin >> N; cout << "The factorial for " << N << " is " << factorial(N) << endl; return 0; } //Output for for loop: Please enter a number = 6 The factorial for 6 is 720
  • 2. //Using if else statement #include <iostream> using namespace std; long factorial ( int a ) { if (a>1) return (a*factorial(a-1)); else return 1 ; } int main () { long number; cout << "Please insert a number : "; cin >> number; cout << number <<"! =" << factorial(number)<<endl; return 0; } //Output of if-else statement Please insert a number : 8 8! =40320
  • 3. // Using the first way of while loop: #include <iostream> using namespace std; // Function definition (header and body) int factorial(int N) { int factorial=1; while ( N > 0) /* while loop continues until test condition N>0 is true */ { factorial= factorial*N; --N; } return factorial; // This value is returned to caller } int main() { int N; cout << "Please enter a number = "; cin >> N; cout << "The factorial for " << N << " is " << factorial(N) << endl; return 0; }
  • 4. //Output of first way of using while loop: Please enter a number = 8 The factorial for 8 is 40320 //Using the second way of while loop: #include <iostream> using namespace std; // Function definition (header and body) int factorial(int N) { int n=1,factorial=1; while ( n <= N) /* while loop continues until test condition N>0 is true */ { factorial= factorial*n; n++; } return factorial; // This value is returned to caller } int main() { int N; cout << "Please enter a number = "; cin >> N; cout << "The factorial for " << N << " is " << factorial(N) << endl;
  • 5. return 0; } //Output using second way of while loop Please enter a number = 8 The factorial for 8 is 40320 QUESTION2 Using function program ODD() and EVEN(), find the sum of odd (1+3+ ... +N) and even (2+4+6+…+N) numbers less than N //Using for loop #include <iostream> using namespace std; // Function definition (header and body) int sum_even(int N) { int i, sum_even=0; for(i=1; i<N; i++) { if(i%2==0) sum_even=sum_even +i; } return sum_even; } int sum_odd(int N) { int i,sum_odd=0; for(i=1; i<N; i++) { if(i%2!=0) sum_odd = sum_odd +i; }
  • 6. return sum_odd; } int main() { int N; cout<<"Please enter a number N: "; cin>>N; cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl; return 0; } //Output by using for loop: Please enter a number N: 10 The summation of even number of N is: 20 The summation of odd number of N is: 25 //Using while loop #include <iostream> using namespace std; // Function definition (header and body) int sum_even(int N) { int i=1, sum_even=0; while (i< N)
  • 7. { if (i%2 ==0) sum_even = sum_even + i; i++; } return sum_even; } int sum_odd(int N) { int i=1,sum_odd=0; while (i< N) { if (i%2 !=0) sum_odd = sum_odd + i; i++; } return sum_odd; } int main() { int N; cout<<"Please enter a number N: "; cin>>N; cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl;
  • 8. return 0; } //Output for while loop Please enter a number N: 8 The summation of even number of N is: 12 The summation of odd number of N is: 16 //Using do while loop: #include <iostream> using namespace std; /* Function definition (header and body) */ int sum_even(int N) { int i=1, sum_even=0; do { if (i%2 ==0) sum_even = sum_even + i; i++; } while (i< N); return sum_even; } int sum_odd(int N) { int i=1,sum_odd=0; do
  • 9. { if (i%2 !=0) sum_odd = sum_odd + i; i++; } while (i< N); return sum_odd; } int main() { int N; cout<<"Please enter a number N: "; cin>>N; cout<<"nThe summation of even number of N is: " <<sum_even(N) <<endl; cout<<"The summation of odd number of N is: " <<sum_odd(N) <<endl; return 0; } //Output for do while loop Please enter a number N: 8 The summation of even number of N is: 12 The summation of odd number of N is: 16
  • 10. QUESTION 3 Write a function program to solve the quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown. #include <iostream> #include <cmath> using namespace std; /**function prototype or declaration, when int main() is placed on the top**/ float discriminant(float a, float b, float c); void TWOREAL(float a,float b,float c); void ONEREAL(float a,float b,float c); //also called as two equal solution void COMPLEX(float a,float b,float c); int main() { float a, b, c; cout << "This program is used to solve Quadratic Equationn"; cout << "in the form of ax^2 + bx +cnn"; do { cout << "Enter value a:"; cin >> a; } while (a == 0); // this is a condition where value of a cannot equal to 0 cout << "Enter value b:"; cin >> b; cout << "Enter value c:";
  • 11. cin >> c; if (discriminant(a,b,c) > 0) TWOREAL(a,b,c); else if (discriminant(a,b,c) == 0) ONEREAL(a,b,c); else COMPLEX(a,b,c); return 0; } float discriminant(float a, float b, float c) { float d = b*b-4*a*c; return d; } void TWOREAL(float a,float b,float c) { float x1,x2; x1 = (-b + sqrtf(discriminant(a,b,c)) )/(2*a); x2 = (-b - sqrtf(discriminant(a,b,c)) )/(2*a); cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; cout << "It has TWO REAL solution which are " << x1 << " , " << x2 << endl; }
  • 12. void ONEREAL(float a,float b,float c) // or we can call it as two equal solution { float x1; x1 = -b/(2*a); cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; cout << "It has ONE REAL solution which is " << x1 << endl; } void COMPLEX(float a,float b,float c) { float x1,x2; x1 = -b/(2*a); x2 = sqrtf(-discriminant(a,b,c))/(2*a); cout << "nThe DISCRIMINANT is " << discriminant(a,b,c) << endl; cout << "It has TWO COMPLEX solutions which are "; cout << x1 << "+" << x2 << "i , "<< x1 << -x2 << "i" << endl; } // Output for Question 3: Output1 : This program is used to solve Quadratic Equation in the form of ax^2 + bx +c Enter value a:1 Enter value b:6
  • 13. Enter value c:8 The DISCRIMINANT is 4 It has TWO REAL solution which are -2 , -4 Output2 : This program is used to solve Quadratic Equation in the form of ax^2 + bx +c Enter value a:2 Enter value b:3 Enter value c:4 The DISCRIMINANT is -23 It has TWO COMPLEX solutions which are -0.75+1.19896i , -0.75-1.19896i Output 3: This program is used to solve Quadratic Equation in the form of ax^2 + bx +c Enter value a:1 Enter value b:8 Enter value c:16 The DISCRIMINANT is 0 It has ONE REAL solution which is -4 QUESTION 4 Write a function program to find the mean (average) of N numbers. /*The programming codes below are to calculate summation of(1+2+3…N)*/ //Using for loop: #include <iostream> using namespace std; float average(int N)
  • 14. { int a,b,sum=0; float result; for ( a=1;a<=N;a++) { cout << “ Value “ << a << “ is: “ ; cin >> b; sum=sum+b; result=float (sum)/N; } return (result); } int main () { int N; cout << "Please insert a value,N : "; cin >> N; cout << "The average of N values is " << average(N) << endl; return 0; } //Output for for loop : Please insert a value,N : 5 Value 1 is:10 Value 2 is:3 Value 3 is:7 Value 4 is:2 Value 5 is:114 The average of N values is 27.2 //Using while loop #include <iostream> using namespace std; float average(int N)
  • 15. { int a=1,b, sum=0; float result; while(a<=N) { cout << "Value " << a << " is: " ; cin >> b; sum= sum + b ; a++; result=float (sum)/N; } return (result); } int main () { int N; cout << "Please insert a value N : "; cin >> N; cout << "The average of N values is " << average(N) << endl; return 0; } //Output for while loop: Please insert a value,N : 5 Value 1 is:10 Value 2 is:3 Value 3 is:7 Value 4 is:2 Value 5 is:114 The average of N values is 27.2
  • 16. //Using do while loop #include <iostream> using namespace std; float average(int N) { int a=1, b, sum=0; float result; do { cout << "Value " << a << " is: " ; cin >> b; sum= sum + b ; a++; result=float (sum)/N; } while(a<=N) ; return (result); } int main () { int N; cout << "Please insert a value N : "; cin >> N; cout << "The average of N values is " << average(N) << endl; return 0; }
  • 17. //Output for do while loop Please insert a value,N : 5 Value 1 is:10 Value 2 is:3 Value 3 is:7 Value 4 is:2 Value 5 is:114 The average of N values is 27.2 QUESTION 5 Using the function program, calculate the formula C(n,k )= ( ) #include <iostream> using namespace std; /*** function prototype or declaration***/ int fact(int n); void COMB(int n,int k); int main () { int n,k; cout << "This program is used to calculate C(n,k)." << endl; do { cout << "Value of n should be larger than k, n>k" << endl; cout << "Enter positive integer n:"; cin >> n; cout << "Enter positive integer k:"; cin >> k; }
  • 18. while (n < 0 || k<0 || n<k); COMB(n,k); return 0; } int factorial(int n) { int fact = 1; if (n == 0) return 1; else { for (int i = 1; i <= n; i++) fact = fact * i; return fact; } } void COMB(int n,int k) { int comb = factorial(n)/(factorial(k)*factorial(n-k)); cout << "C(" << n << "," << k << ")= " << comb << endl; } //Output for Question 5: This program is used to calculate C(n,k). Value of n should be larger than k, n>k Enter positive integer n:5
  • 19. Enter positive integer k:7 Value of n should be larger than k, n>k Enter positive integer n:-4 Enter positive integer k:-7 Value of n should be larger than k, n>k Enter positive integer n:3 Enter positive integer k:2 C(3,2)= 3