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

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
Raihan 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
 
Travel management
Travel managementTravel management
Travel management
1Parimal2
 

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

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
Malikireddy Bramhananda Reddy
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
LokeshK66
 
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 solving
Syed 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

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

C++ TUTORIAL 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