SlideShare a Scribd company logo
1 of 17
Download to read offline
SJEM2231 STRUCTURED PROGRAMMING 
NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN 
TUTORIAL 3/ LAB 3 (13rd OCTOBER 2014) 
(Use different loop structures for problems 2, 4 and 5, and understand how it works) 
QUESTION 1 
Write a program to find the given number is odd or even. First test whether the given number is non zero positive number. 
#include <iostream> 
using namespace std; 
int main() 
{ 
int number; 
ULANG: 
cout << "Please enter any positive integer: "; 
cin >> number; 
if(number > 0) 
{ 
cout << "The given number is non zero positive number"<<endl; 
} 
else 
{ 
cout << "The given number is not a positive number. Try again" <<endl; 
goto ULANG; 
} 
if(number % 2 ==0) 
cout <<"Thus, " << number << " is an even number" << endl;
else 
cout <<"Thus, " << number << " is an odd number" <<endl; 
return 0; 
} 
// Output : 
Please enter any positive integer: -1 
The given number is not a positive number. Try again 
Please enter any positive integer: 0 
The given number is not a positive number. Try again 
Please enter any positive integer: 5 
The given number is non zero positive number 
Thus, 5 is an odd number 
//Alternative method for question 1 
#include<iostream> 
using namespace std; 
int main() 
{ 
int n; 
cout << "Enter a number: "; 
cin >> n; 
if (n>0 && n%2==0) 
{ 
cout<<"nThe number is non zero positive number"<<endl; 
cout << "The number is even number"<<endl; 
} 
else if (n>0 && n%2!=0)
{ 
cout<<"nThe number is non zero positive number"<<endl; 
cout << "The number is odd number"<<endl; 
} 
else 
cout<<"nThe number is zero or negative number"<<endl; 
return 0; 
} 
//Output: 
Enter a number: 0 
The number is zero or negative number 
Enter a number: 8 
The number is non zero positive number 
The number is even number 
Enter a number: 11 
The number is non zero positive number 
The number is odd number 
QUESTION 2 
Write a program to print the multiplication table as shown below(lower triangular matrix). 
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
… 
10 20 30 40 50 60 70 80 90 100
//Using for loop : 
#include <iostream> 
using namespace std; 
int main() 
{ 
for (int i=1 ; i<=10; i++) 
{ 
for(int j=1; j<=10; j++) 
{ 
if (j>i) break; 
cout << i*j << "t"; 
} 
cout << endl; 
} 
return 0; 
} 
// Or we can write this way, still using for loop but slightly different: 
#include <iostream> 
using namespace std; 
int main() 
{ 
for (int i=1 ; i<=10; i++) 
{ 
for(int j=1; j<=i; j++) 
{ 
cout << i*j << "t"; 
}
cout << endl; 
} 
return 0; 
} 
//Using while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i=1; 
while(i<=10) 
{ 
int j=1; 
while(j<=i) 
{ 
cout<<i*j <<"t"; 
j++; 
} 
cout <<endl; 
i++; 
} 
return 0; 
} 
//Using do while loop 
#include<iostream> 
using namespace std;
int main() 
{ 
int i=1, j=1; 
do 
{ 
do 
{ 
/*We may replace int j=1 here instead of declaring j=1 above */ 
cout << i*j <<"t"; 
j++; 
} while(j<=i); 
j=1; /*we can eliminate this j=1 if we declare int j=1 above */ 
cout<<endl; 
i++; 
} while(i<=10) 
return 0; 
} //The output we will get is just the same: 
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100
QUESTION 3 
Write a program that solves quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown (This will NOT apply if a is zero, so that condition must be checked separately. The formula also fails to work (for real numbers) if the expression under the square root is negative) 
#include <iostream> 
#include <cmath> 
using namespace std; 
int main() 
{ 
// Variable Declarations 
double a, b, c; 
//Variable Inputs 
cout << "Enter the value of a: "; 
cin >> a; 
cout << "Enter the value of b: "; 
cin >> b; 
cout << "Enter the value of c: "; 
cin >> c; 
//Computations 
double discriminant = (pow(b,2) - 4*a*c); 
double positive_root = (((-b) + sqrt(discriminant))/(2*a)); 
double negative_root = (((-b) - sqrt(discriminant))/(2*a)); 
if (discriminant == 0) 
{ 
cout << "nThe discriminant is "<< discriminant << endl; 
cout << "The equation has a single root.n"; 
}
else if (discriminant < 0) 
{ 
cout << "nThe discriminant is "<< discriminant << endl; 
cout << "The equation has two complex roots.n"; 
} 
else 
{ 
cout << "nnThe discriminant is " << discriminant << endl; 
cout << "The equation has two real roots.n"; 
} 
//Final Root Values 
cout << "The roots of the quadratic equation are x = "; 
cout << negative_root<< "," << positive_root << endl; 
return 0; 
} //Output: 
Enter the value of a: 1 
Enter the value of b: 6 
Enter the value of c: 8 
The discriminant is 4 
The equation has two real roots. 
The roots of the quadratic equation are x = -4,-2 
//Alternative method for question 3 
#include<iostream> 
#include<cmath> 
using namespace std;
int main() 
{ 
double a, b, c, discriminant, root_1, root_2; 
cout<<"Please enter value of a: "; 
cin>>a; 
cout<<"nPlease enter value of b: "; 
cin>>b; 
cout<<"nPlease enter value of c: "; 
cin>>c; 
discriminant= (b*b)-(4*a*c); 
if (discriminant>0) 
{ 
cout<<"nThe equation has 2 distinct root" <<endl; 
} 
else if(discriminant==0) 
{ 
cout<<"nThe equation has equal root"<<endl; 
} 
else if(discriminant<0) 
{ 
cout<<"nThe equation has complex root" <<endl; 
} 
root_1= ((-b + (sqrt(d)))/(2*a)); 
root_2= ((-b - (sqrt(d)))/(2*a)); 
cout<< "The first root is "<<root_1 <<"n"<<"while the second root is " << root_2 <<endl; 
return 0; 
}
//Output: 
Please enter value of a: 1 
Please enter value of b: 6 
Please enter value of c: 8 
The equation has 2 distinct root 
The first root is -2 
while the second root is -4 
QUESTION 4 
Write a program to find the mean (average) of N numbers. 
//Using while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int N, i, b, sum=0; 
float average; 
cout<<"Please enter the value of N: "; 
cin>> N; 
i =1; 
while(i<=N) 
{ 
cout << "Value " << i << " is:"; 
cin >> b; 
sum= sum + b; 
i++; 
}
cout<<"The sum is: "<< sum <<endl; 
average= float (sum)/ N; 
cout<<"Thus, the average is: "<<average<< endl; 
return 0; 
} 
//Output for while loop: 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2 
//Using do while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i=1, b,sum=0,N; 
float average; 
cout<<"Please enter a number N: "; 
cin>> N;
do 
{ 
cout << "Value " << i << " is:"; 
cin >> b; 
sum=sum+b; 
i++; 
} while(i<=N); 
cout<<"nThe summation of N number is: " << sum <<endl; 
average= float (sum)/N; 
cout<<"The average is " << average <<endl; 
return 0; 
} 
//Output for do while loop 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2 
//Using for loop 
#include<iostream> 
using namespace std; 
int main()
{ 
int i, b, sum=0, N; 
float average; 
cout<<"Please enter a number N: "; 
cin>> N; 
for (i=1; i<=N ; i++) 
{ 
cout << "Value " << i << " is: "; 
cin >> b; 
sum= sum +b; 
average= float(sum)/N; 
} 
cout<<"nThe summation of N number is: "<< sum <<endl; 
cout<<"The average is " << average <<endl; 
return 0; 
} 
//Output for for loop: 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2
QUESTION 5 
Write a program to find the sum of EVEN numbers (2+4+6+…+N) and ODD numbers (1+3+ ... +N) up to N numbers. 
//Using for loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i, N, sum_odd=0, sum_even=0; 
cout<<"Please enter a number N: "; 
cin>>N; 
for(i=1; i<=N; i++) 
{ 
if(i%2==0) 
sum_even=sum_even +i; 
} 
cout<<"nThe summation of even number of N is: " <<sum_even <<endl; 
for(i=1; i<=N; i++) 
{ 
if(i%2!=0) 
sum_odd=sum_odd +i; 
} 
cout<<"The summation of odd number of N is: " <<sum_odd <<endl; 
return 0; 
}
//Output for for loop 
Please enter a number N: 10 
The summation of even number of N is: 30 
The summation of odd number of N is: 25 
//Using do while loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int i=1, N, sum_even=0, sum_odd=0; 
cout << "Enter any positive integer, N: "; 
cin >> N; 
while (i<= N) 
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
else 
sum_odd = sum_odd + I; 
i++; 
} 
cout << "nThe summation of even numbers is " << sum_even << endl; 
cout << "The summation of odd numbers is " << sum_odd << endl; 
return 0; 
}
//Output for while loop 
Enter any positive integer, N: 10 
The summation of even numbers is 30 
The summation of odd numbers is 25 
//Using do while loop: 
#include <iostream> 
using namespace std; 
int main() 
{ 
int i=1, N, sum_even=0, sum_odd=0; 
cout << "Enter any positive integer, N: "; 
cin >> N; 
do 
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
else 
sum_odd = sum_odd + i; 
i++; 
} while (i<= N); 
cout << "nThe summation of even numbers is " << sum_even << endl; 
cout << "The summation of odd numbers is " << sum_odd << endl; 
return 0; 
}
//Output for do while loop 
Enter any positive integer, N: 10 
The summation of even numbers is 30 
The summation of odd numbers is 25

More Related Content

What's hot

Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional creditRaihan Bin-Mofidul
 
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
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 

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
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Oop1
Oop1Oop1
Oop1
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ programs
C++ programsC++ programs
C++ programs
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Opp compile
Opp compileOpp compile
Opp compile
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Ee 3122 numerical methods and statistics sessional credit
Ee 3122 numerical methods and statistics sessional  creditEe 3122 numerical methods and statistics sessional  credit
Ee 3122 numerical methods and statistics sessional credit
 
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))
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Travel management
Travel managementTravel management
Travel management
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 

Similar to C++ TUTORIAL 3

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
 
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
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 
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
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 

Similar to C++ TUTORIAL 3 (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
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.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++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
C++ practical
C++ practicalC++ practical
C++ practical
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
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
 
C++ file
C++ fileC++ file
C++ file
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
10 template code program
10 template code program10 template code program
10 template code program
 
Ch4
Ch4Ch4
Ch4
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

C++ TUTORIAL 3

  • 1. SJEM2231 STRUCTURED PROGRAMMING NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN TUTORIAL 3/ LAB 3 (13rd OCTOBER 2014) (Use different loop structures for problems 2, 4 and 5, and understand how it works) QUESTION 1 Write a program to find the given number is odd or even. First test whether the given number is non zero positive number. #include <iostream> using namespace std; int main() { int number; ULANG: cout << "Please enter any positive integer: "; cin >> number; if(number > 0) { cout << "The given number is non zero positive number"<<endl; } else { cout << "The given number is not a positive number. Try again" <<endl; goto ULANG; } if(number % 2 ==0) cout <<"Thus, " << number << " is an even number" << endl;
  • 2. else cout <<"Thus, " << number << " is an odd number" <<endl; return 0; } // Output : Please enter any positive integer: -1 The given number is not a positive number. Try again Please enter any positive integer: 0 The given number is not a positive number. Try again Please enter any positive integer: 5 The given number is non zero positive number Thus, 5 is an odd number //Alternative method for question 1 #include<iostream> using namespace std; int main() { int n; cout << "Enter a number: "; cin >> n; if (n>0 && n%2==0) { cout<<"nThe number is non zero positive number"<<endl; cout << "The number is even number"<<endl; } else if (n>0 && n%2!=0)
  • 3. { cout<<"nThe number is non zero positive number"<<endl; cout << "The number is odd number"<<endl; } else cout<<"nThe number is zero or negative number"<<endl; return 0; } //Output: Enter a number: 0 The number is zero or negative number Enter a number: 8 The number is non zero positive number The number is even number Enter a number: 11 The number is non zero positive number The number is odd number QUESTION 2 Write a program to print the multiplication table as shown below(lower triangular matrix). 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 … 10 20 30 40 50 60 70 80 90 100
  • 4. //Using for loop : #include <iostream> using namespace std; int main() { for (int i=1 ; i<=10; i++) { for(int j=1; j<=10; j++) { if (j>i) break; cout << i*j << "t"; } cout << endl; } return 0; } // Or we can write this way, still using for loop but slightly different: #include <iostream> using namespace std; int main() { for (int i=1 ; i<=10; i++) { for(int j=1; j<=i; j++) { cout << i*j << "t"; }
  • 5. cout << endl; } return 0; } //Using while loop #include<iostream> using namespace std; int main() { int i=1; while(i<=10) { int j=1; while(j<=i) { cout<<i*j <<"t"; j++; } cout <<endl; i++; } return 0; } //Using do while loop #include<iostream> using namespace std;
  • 6. int main() { int i=1, j=1; do { do { /*We may replace int j=1 here instead of declaring j=1 above */ cout << i*j <<"t"; j++; } while(j<=i); j=1; /*we can eliminate this j=1 if we declare int j=1 above */ cout<<endl; i++; } while(i<=10) return 0; } //The output we will get is just the same: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
  • 7. QUESTION 3 Write a program that solves quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown (This will NOT apply if a is zero, so that condition must be checked separately. The formula also fails to work (for real numbers) if the expression under the square root is negative) #include <iostream> #include <cmath> using namespace std; int main() { // Variable Declarations double a, b, c; //Variable Inputs cout << "Enter the value of a: "; cin >> a; cout << "Enter the value of b: "; cin >> b; cout << "Enter the value of c: "; cin >> c; //Computations double discriminant = (pow(b,2) - 4*a*c); double positive_root = (((-b) + sqrt(discriminant))/(2*a)); double negative_root = (((-b) - sqrt(discriminant))/(2*a)); if (discriminant == 0) { cout << "nThe discriminant is "<< discriminant << endl; cout << "The equation has a single root.n"; }
  • 8. else if (discriminant < 0) { cout << "nThe discriminant is "<< discriminant << endl; cout << "The equation has two complex roots.n"; } else { cout << "nnThe discriminant is " << discriminant << endl; cout << "The equation has two real roots.n"; } //Final Root Values cout << "The roots of the quadratic equation are x = "; cout << negative_root<< "," << positive_root << endl; return 0; } //Output: Enter the value of a: 1 Enter the value of b: 6 Enter the value of c: 8 The discriminant is 4 The equation has two real roots. The roots of the quadratic equation are x = -4,-2 //Alternative method for question 3 #include<iostream> #include<cmath> using namespace std;
  • 9. int main() { double a, b, c, discriminant, root_1, root_2; cout<<"Please enter value of a: "; cin>>a; cout<<"nPlease enter value of b: "; cin>>b; cout<<"nPlease enter value of c: "; cin>>c; discriminant= (b*b)-(4*a*c); if (discriminant>0) { cout<<"nThe equation has 2 distinct root" <<endl; } else if(discriminant==0) { cout<<"nThe equation has equal root"<<endl; } else if(discriminant<0) { cout<<"nThe equation has complex root" <<endl; } root_1= ((-b + (sqrt(d)))/(2*a)); root_2= ((-b - (sqrt(d)))/(2*a)); cout<< "The first root is "<<root_1 <<"n"<<"while the second root is " << root_2 <<endl; return 0; }
  • 10. //Output: Please enter value of a: 1 Please enter value of b: 6 Please enter value of c: 8 The equation has 2 distinct root The first root is -2 while the second root is -4 QUESTION 4 Write a program to find the mean (average) of N numbers. //Using while loop #include<iostream> using namespace std; int main() { int N, i, b, sum=0; float average; cout<<"Please enter the value of N: "; cin>> N; i =1; while(i<=N) { cout << "Value " << i << " is:"; cin >> b; sum= sum + b; i++; }
  • 11. cout<<"The sum is: "<< sum <<endl; average= float (sum)/ N; cout<<"Thus, the average is: "<<average<< endl; return 0; } //Output for while loop: Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2 //Using do while loop #include<iostream> using namespace std; int main() { int i=1, b,sum=0,N; float average; cout<<"Please enter a number N: "; cin>> N;
  • 12. do { cout << "Value " << i << " is:"; cin >> b; sum=sum+b; i++; } while(i<=N); cout<<"nThe summation of N number is: " << sum <<endl; average= float (sum)/N; cout<<"The average is " << average <<endl; return 0; } //Output for do while loop Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2 //Using for loop #include<iostream> using namespace std; int main()
  • 13. { int i, b, sum=0, N; float average; cout<<"Please enter a number N: "; cin>> N; for (i=1; i<=N ; i++) { cout << "Value " << i << " is: "; cin >> b; sum= sum +b; average= float(sum)/N; } cout<<"nThe summation of N number is: "<< sum <<endl; cout<<"The average is " << average <<endl; return 0; } //Output for for loop: Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2
  • 14. QUESTION 5 Write a program to find the sum of EVEN numbers (2+4+6+…+N) and ODD numbers (1+3+ ... +N) up to N numbers. //Using for loop #include<iostream> using namespace std; int main() { int i, N, sum_odd=0, sum_even=0; cout<<"Please enter a number N: "; cin>>N; for(i=1; i<=N; i++) { if(i%2==0) sum_even=sum_even +i; } cout<<"nThe summation of even number of N is: " <<sum_even <<endl; for(i=1; i<=N; i++) { if(i%2!=0) sum_odd=sum_odd +i; } cout<<"The summation of odd number of N is: " <<sum_odd <<endl; return 0; }
  • 15. //Output for for loop Please enter a number N: 10 The summation of even number of N is: 30 The summation of odd number of N is: 25 //Using do while loop #include <iostream> using namespace std; int main() { int i=1, N, sum_even=0, sum_odd=0; cout << "Enter any positive integer, N: "; cin >> N; while (i<= N) { if (i%2 ==0) sum_even = sum_even + i; else sum_odd = sum_odd + I; i++; } cout << "nThe summation of even numbers is " << sum_even << endl; cout << "The summation of odd numbers is " << sum_odd << endl; return 0; }
  • 16. //Output for while loop Enter any positive integer, N: 10 The summation of even numbers is 30 The summation of odd numbers is 25 //Using do while loop: #include <iostream> using namespace std; int main() { int i=1, N, sum_even=0, sum_odd=0; cout << "Enter any positive integer, N: "; cin >> N; do { if (i%2 ==0) sum_even = sum_even + i; else sum_odd = sum_odd + i; i++; } while (i<= N); cout << "nThe summation of even numbers is " << sum_even << endl; cout << "The summation of odd numbers is " << sum_odd << endl; return 0; }
  • 17. //Output for do while loop Enter any positive integer, N: 10 The summation of even numbers is 30 The summation of odd numbers is 25