SlideShare a Scribd company logo
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

More Related Content

What's hot

C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
Ch shampi Ch shampi
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
AhalyaR
 
Cpp programs
Cpp programsCpp programs
Cpp programs
harman kaur
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
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 commands
Vishvjeet Yadav
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
C- Programs - Harsh
C- Programs - HarshC- Programs - Harsh
C- Programs - Harsh
Harsh Sharma
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris 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
 
Cquestions
Cquestions Cquestions
Cquestions
mohamed sikander
 
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
 

What's hot (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 to 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-ee01083101
premrings
 
C++ Programm.pptx
C++ Programm.pptxC++ Programm.pptx
C++ Programm.pptx
Åįjâž Ali
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
Sena Nama
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
T17Rockstar
 
C++ file
C++ fileC++ file
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++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
Thesis Scientist Private Limited
 
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.pdf
yamew16788
 
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
MuhammadTalha436
 
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 6rohassanie
 
12
1212
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 

Similar to 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
 

Recently uploaded

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 

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