SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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))
 

Semelhante 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
 

Semelhante 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

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Último (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

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