SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
SJEM2231 STRUCTURED PROGRAMMING 
NAME : WAN MOHAMAD FARHAN BIN AB RAHMAN 
TUTORIAL 2/ LAB 2 (29TH SEPTEMBER 2014) 
(Use different loop structures for problems 4 to 6, and understand how it works) 
QUESTION 1 
Write a program that asks the user to type the width and length of a rectangle and then print the area and perimeter of the rectangle. 
#include <iostream> 
using namespace std; 
int main () 
{ 
int width, height, area, perimeter; 
cout <<"Please enter width of rectangle:"; 
cin >> width; 
cout <<"Please enter height of rectangle:"; 
cin >> height; 
area=width*height; 
perimeter=(2*width)+(2*height); // same as 2*(height+width) 
cout << "The area of rectangle is : " << area << endl; 
cout << "The perimeter of rectangle is: " << perimeter << endl; 
return 0; 
} 
// Output: 
Please enter width of rectangle:10 
Please enter height of rectangle:10 
The area of rectangle is : 100 
The perimeter of rectangle is: 40 
QUESTION 2 
Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a length in inches, the output would be 42.926 cm.(One inch equals 2.54centimeters) 
#include <iostream> 
using namespace std; 
int main() 
{
float length_inch, length_cm; 
cout << "Please enter length in inches: "; 
cin >> length_inch; 
length_cm = length_inch*2.54; 
cout << "The length in centimeters is : "<< length_cm << endl; 
return 0; 
} 
//Output : 
Please enter length in inches: 16.9 
The length in centimeters is : 42.926 
QUESTION 3 
Write a program that reads the user’s age and then prints “You are a child.” if the age < 18, “You are an adult.” if 18 ≤ age < 65, and “You are a senior citizen.” if age ≥ 65. 
// Method 1, using do while-loop 
#include <iostream> using namespace std; int main() { int age; cout<< "Please enter your age:"; do { cin >> age; if (age < 0) cout << "You have not entered a valid age. Please try again:"; }
while(age <= 0); 
if (age < 18) cout << "You are a child"<<endl; else if (age>=18 && age<65) cout <<"You are an adult" << endl; else cout<< "You are a senior citizen" << endl; return 0; } 
//Output_method1: Please enter your age: -10 You have not entered a valid age. Please try again:22 You are an adult Please enter your age:-2 You have not entered a valid age. Please try again:10 You are a child Please enter your age:-4 You have not entered a valid age. Please try again:81 You are a senior citizen 
// Method 2, using if else 
#include<iostream> // allows program to perform input and output 
using namespace std; // program uses names from the std namespace 
int main () 
{ 
int age; 
cout<< "Please enter your age:"; //prompt user for data
cin >> age; //read data from user 
if (age <0) cout << "Error! You have not entered a valid age" <<endl; 
else if (age >= 0 && age < 18) 
cout << "You are a child" << endl; 
else if (age >= 18 && age < 65) 
cout<< "You are an adult" << endl; 
else 
cout<< "You are a senior citizen" << endl; 
return 0; 
} 
//Output_method2: 
Please enter your age:-10 Error! You have not entered a valid age 
Please enter your age:14 
You are a child 
Please enter your age:40 
You are an adult 
Please enter your age:78 
You are a senior citizen 
QUESTION 4 
Write a program to find the factorial of a given number (N!). // Using the first way of while-loop 
#include <iostream> 
using namespace std; 
int main()
{ 
long double N, factorial; 
cout<< "Please enter a number up to 170: "; 
cin>> N; 
factorial=1; 
while ( N > 0) 
/* while loop continues until test condition N>0 is true */ 
{ 
factorial= factorial*N; 
--N; 
} 
cout<< "Factorial = " << factorial << endl; 
return 0; 
} //Output : 
Please enter a number up to 170: 170 
Factorial = 7.25742e+306 
//Using the second way of while-loop 
#include<iostream> 
using namespace std; 
int main () 
{ 
long double n,N, factorial; 
cout << "Enter the value of N up to 170 : "; 
cin >> N; 
cout << endl;
factorial = 1; 
n= 1; 
while(n <= N) 
{ 
factorial= factorial*n; 
n= n+1; // or can write as n++ 
} 
cout << "N!= " << factorial << endl; 
return 0; 
} 
//Output: 
Enter the value of N up to 170 : 170 
N!= 7.25742e+306 
// Using for-loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
long double N, int a, factorial=1; /*try use int N */ 
cout << "Please enter a number up to 170: "; 
cin>> N; 
if (N< 0) 
cout << “Factorial of negative number does not exist n”;
else 
{ 
for (a=1; a<=N; a++) //for loop terminates if a > N 
{ 
factorial= factorial*a; //same meaning as factorial*=count 
} 
cout<< "Factorial of N is equal to " << factorial<< endl; 
} 
return 0; 
} 
//Output: 
Please enter a number up to 170: 170 
Factorial of N is equal to 7.25742e+306 
QUESTION 5 
Write a program to find the sum of first N natural numbers. (1 + 2 + 3 + 4 + 5 + ⋯+N) 
//Using while-loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int a, N, sum=0; 
cout<<"Please enter an integer: "; 
cin>> N; 
a=1; 
while( a <= N) // while loop terminates if a > N 
{
sum = sum+ a; // also can be write as sum+ = a 
a++; // increment a by 1 
} 
cout<< "Sum = " << sum << endl; 
return 0; 
} 
//Output : 
Please enter an integer: 10 
Sum = 55 
//Using for-loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int a, N, sum=0; 
cout<< "Please enter an integer: " ; 
cin>> N; 
for(a=1; a<=N; a++) // for loop terminates if a>N 
{ 
sum+= a; // same meaning as sum= sum+a 
} 
cout<< "Sum = " << sum <<endl ; 
return 0; 
} //Output : 
Please enter an integer: 10 
Sum = 55
// The program below displays error message when we enters 0 or negative number and displays the sum of natural numbers if we enters positive number 
#include <iostream> 
using namespace std; 
int main() 
{ 
int a, N, sum=0; 
cout << "Please enter an integer: " ; 
cin >> N; 
if (N <= 0) 
cout<< "Cannot execute! Error !n" ; 
else 
{ 
for(a=1; a <=N; a++) // for loop terminates if a > N 
{ 
sum+= a; // same meaning as sum= sum+a 
} 
cout<< "Sum = " <<sum<< endl; 
} 
return 0; 
} 
//Output: 
Please enter an integer: 10 
Sum = 55
QUESTION 6 
Write a program to the sum of a given number of squares. For example, if 5 is input, then the program will print 55, which equals 12+ 22+ 32+ 42+ 52 
//Using while-loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int a, int N, sum=0; 
cout<< "Please enter an integer: " ; 
cin >> N; 
a=1; 
while( a <= N) // while loop terminates if a > N 
{ 
sum = sum+ a*a; // also can be write as sum+ = a*a 
a++; // same meaning as a=a+1 
} 
cout << "Sum = " << sum << endl; 
return 0; 
} 
//Output : 
Please enter an integer: 5 
Sum = 55
//Using for-loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int N, sum=0; 
cout<< "Please enter an integer: " ; 
cin>> N; 
for(int a=1; a<=N; a++) // for loop terminates if a>N 
{ 
sum+= a*a; // same meaning as sum= sum+a*a 
} 
cout<< "The sum of the squares is equal to " << sum <<endl ; 
return 0; 
} 
//Output: 
Please enter an integer: 5 
The sum of the squares is equal to 55

Contenu connexe

Tendances

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
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
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
C++ project
C++ projectC++ project
C++ projectSonu S S
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQLvikram mahendra
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++prashant_sainii
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 

Tendances (20)

C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
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
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ project
C++ projectC++ project
C++ project
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
C++ programs
C++ programsC++ programs
C++ programs
 
Oop1
Oop1Oop1
Oop1
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 

Similaire à C++ TUTORIAL 2

ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 
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
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdfT17Rockstar
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++Marco Izzotti
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)Sena Nama
 
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
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfseoagam1
 
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
 
clc02_cpp_presentation_edit3 (1) 1.pptx
clc02_cpp_presentation_edit3 (1) 1.pptxclc02_cpp_presentation_edit3 (1) 1.pptx
clc02_cpp_presentation_edit3 (1) 1.pptxjminrin0212
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 

Similaire à C++ TUTORIAL 2 (20)

ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
10 template code program
10 template code program10 template code program
10 template code program
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
Project in programming
Project in programmingProject in programming
Project in programming
 
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
 
Statement
StatementStatement
Statement
 
3 rd animation
3 rd animation3 rd animation
3 rd animation
 
Computer Practical XII
Computer Practical XIIComputer Practical XII
Computer Practical XII
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
 
C++ file
C++ fileC++ file
C++ file
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
clc02_cpp_presentation_edit3 (1) 1.pptx
clc02_cpp_presentation_edit3 (1) 1.pptxclc02_cpp_presentation_edit3 (1) 1.pptx
clc02_cpp_presentation_edit3 (1) 1.pptx
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 

Plus de Farhan Ab Rahman

Plus de Farhan Ab Rahman (6)

C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2VIBRATIONS AND WAVES TUTORIAL#2
VIBRATIONS AND WAVES TUTORIAL#2
 
Notis Surau
Notis SurauNotis Surau
Notis Surau
 
Kitab Bakurah.amani
Kitab Bakurah.amaniKitab Bakurah.amani
Kitab Bakurah.amani
 

Dernier

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Dernier (20)

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

C++ TUTORIAL 2

  • 1. SJEM2231 STRUCTURED PROGRAMMING NAME : WAN MOHAMAD FARHAN BIN AB RAHMAN TUTORIAL 2/ LAB 2 (29TH SEPTEMBER 2014) (Use different loop structures for problems 4 to 6, and understand how it works) QUESTION 1 Write a program that asks the user to type the width and length of a rectangle and then print the area and perimeter of the rectangle. #include <iostream> using namespace std; int main () { int width, height, area, perimeter; cout <<"Please enter width of rectangle:"; cin >> width; cout <<"Please enter height of rectangle:"; cin >> height; area=width*height; perimeter=(2*width)+(2*height); // same as 2*(height+width) cout << "The area of rectangle is : " << area << endl; cout << "The perimeter of rectangle is: " << perimeter << endl; return 0; } // Output: Please enter width of rectangle:10 Please enter height of rectangle:10 The area of rectangle is : 100 The perimeter of rectangle is: 40 QUESTION 2 Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a length in inches, the output would be 42.926 cm.(One inch equals 2.54centimeters) #include <iostream> using namespace std; int main() {
  • 2. float length_inch, length_cm; cout << "Please enter length in inches: "; cin >> length_inch; length_cm = length_inch*2.54; cout << "The length in centimeters is : "<< length_cm << endl; return 0; } //Output : Please enter length in inches: 16.9 The length in centimeters is : 42.926 QUESTION 3 Write a program that reads the user’s age and then prints “You are a child.” if the age < 18, “You are an adult.” if 18 ≤ age < 65, and “You are a senior citizen.” if age ≥ 65. // Method 1, using do while-loop #include <iostream> using namespace std; int main() { int age; cout<< "Please enter your age:"; do { cin >> age; if (age < 0) cout << "You have not entered a valid age. Please try again:"; }
  • 3. while(age <= 0); if (age < 18) cout << "You are a child"<<endl; else if (age>=18 && age<65) cout <<"You are an adult" << endl; else cout<< "You are a senior citizen" << endl; return 0; } //Output_method1: Please enter your age: -10 You have not entered a valid age. Please try again:22 You are an adult Please enter your age:-2 You have not entered a valid age. Please try again:10 You are a child Please enter your age:-4 You have not entered a valid age. Please try again:81 You are a senior citizen // Method 2, using if else #include<iostream> // allows program to perform input and output using namespace std; // program uses names from the std namespace int main () { int age; cout<< "Please enter your age:"; //prompt user for data
  • 4. cin >> age; //read data from user if (age <0) cout << "Error! You have not entered a valid age" <<endl; else if (age >= 0 && age < 18) cout << "You are a child" << endl; else if (age >= 18 && age < 65) cout<< "You are an adult" << endl; else cout<< "You are a senior citizen" << endl; return 0; } //Output_method2: Please enter your age:-10 Error! You have not entered a valid age Please enter your age:14 You are a child Please enter your age:40 You are an adult Please enter your age:78 You are a senior citizen QUESTION 4 Write a program to find the factorial of a given number (N!). // Using the first way of while-loop #include <iostream> using namespace std; int main()
  • 5. { long double N, factorial; cout<< "Please enter a number up to 170: "; cin>> N; factorial=1; while ( N > 0) /* while loop continues until test condition N>0 is true */ { factorial= factorial*N; --N; } cout<< "Factorial = " << factorial << endl; return 0; } //Output : Please enter a number up to 170: 170 Factorial = 7.25742e+306 //Using the second way of while-loop #include<iostream> using namespace std; int main () { long double n,N, factorial; cout << "Enter the value of N up to 170 : "; cin >> N; cout << endl;
  • 6. factorial = 1; n= 1; while(n <= N) { factorial= factorial*n; n= n+1; // or can write as n++ } cout << "N!= " << factorial << endl; return 0; } //Output: Enter the value of N up to 170 : 170 N!= 7.25742e+306 // Using for-loop #include <iostream> using namespace std; int main() { long double N, int a, factorial=1; /*try use int N */ cout << "Please enter a number up to 170: "; cin>> N; if (N< 0) cout << “Factorial of negative number does not exist n”;
  • 7. else { for (a=1; a<=N; a++) //for loop terminates if a > N { factorial= factorial*a; //same meaning as factorial*=count } cout<< "Factorial of N is equal to " << factorial<< endl; } return 0; } //Output: Please enter a number up to 170: 170 Factorial of N is equal to 7.25742e+306 QUESTION 5 Write a program to find the sum of first N natural numbers. (1 + 2 + 3 + 4 + 5 + ⋯+N) //Using while-loop #include <iostream> using namespace std; int main() { int a, N, sum=0; cout<<"Please enter an integer: "; cin>> N; a=1; while( a <= N) // while loop terminates if a > N {
  • 8. sum = sum+ a; // also can be write as sum+ = a a++; // increment a by 1 } cout<< "Sum = " << sum << endl; return 0; } //Output : Please enter an integer: 10 Sum = 55 //Using for-loop #include <iostream> using namespace std; int main() { int a, N, sum=0; cout<< "Please enter an integer: " ; cin>> N; for(a=1; a<=N; a++) // for loop terminates if a>N { sum+= a; // same meaning as sum= sum+a } cout<< "Sum = " << sum <<endl ; return 0; } //Output : Please enter an integer: 10 Sum = 55
  • 9. // The program below displays error message when we enters 0 or negative number and displays the sum of natural numbers if we enters positive number #include <iostream> using namespace std; int main() { int a, N, sum=0; cout << "Please enter an integer: " ; cin >> N; if (N <= 0) cout<< "Cannot execute! Error !n" ; else { for(a=1; a <=N; a++) // for loop terminates if a > N { sum+= a; // same meaning as sum= sum+a } cout<< "Sum = " <<sum<< endl; } return 0; } //Output: Please enter an integer: 10 Sum = 55
  • 10. QUESTION 6 Write a program to the sum of a given number of squares. For example, if 5 is input, then the program will print 55, which equals 12+ 22+ 32+ 42+ 52 //Using while-loop #include <iostream> using namespace std; int main() { int a, int N, sum=0; cout<< "Please enter an integer: " ; cin >> N; a=1; while( a <= N) // while loop terminates if a > N { sum = sum+ a*a; // also can be write as sum+ = a*a a++; // same meaning as a=a+1 } cout << "Sum = " << sum << endl; return 0; } //Output : Please enter an integer: 5 Sum = 55
  • 11. //Using for-loop #include <iostream> using namespace std; int main() { int N, sum=0; cout<< "Please enter an integer: " ; cin>> N; for(int a=1; a<=N; a++) // for loop terminates if a>N { sum+= a*a; // same meaning as sum= sum+a*a } cout<< "The sum of the squares is equal to " << sum <<endl ; return 0; } //Output: Please enter an integer: 5 The sum of the squares is equal to 55