SlideShare una empresa de Scribd logo
1 de 11
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

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

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

Más de Farhan Ab Rahman

Más 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
 

Último

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
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.pptxheathfieldcps1
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
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...Poonam Aher Patil
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
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)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 

Último (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
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)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

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