SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Oops practical file
Oops practical fileOops practical file
Oops practical file
Ankit Dixit
 
Travel management
Travel managementTravel management
Travel management
1Parimal2
 
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
Abhishek 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
 

Was ist angesagt? (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))
 

Ähnlich wie C++ TUTORIAL 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)
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 operator
Jussi Pohjolainen
 
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
ssuser3cbb4c
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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Ữ Â...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
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.
 
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
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

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