SlideShare a Scribd company logo
Introduction to Computers
Lab 11
First Year (2016– 2017)
Introduction to Computers Lab First Year 2016– 2017
Problem 1
• Write a program that repeatedly collects positive
integers from the user, stopping when the user enters
a negative number or zero. After that, output the
product of all positive entries.
• A sample run should appear on the screen like the text
below.
Enter a number: 3
Enter a number: 10
Enter a number: 2
Enter a number: -13
The product of all your positive numbers is 60.
2
Solution
#include <iostream>
using namespace std;
int main( )
{
int x = 1;
int product = 1;
while ( x > 0 )
{
product *= x;
cout << "Enter a number: ";
cin >> x;
}
cout << "The product of all your positive numbers
is " << product << endl;
return 0;
} 3
Another Solution:
using break
4
#include <iostream>
using namespace std;
int main( )
{
int x = 1, product = 1;
while (true)
{
cout << "Enter a number: ";
cin >> x;
if (x <= 0)
break;
product *= x;
}
cout << "The product of all your positive numbers is " <<
product << endl;
return 0;
}
Problem 2
• Write a program that accepts from the user a list of
positive integers and displays the number of even and
odd values in the list. The program accepts numbers
from the user until he enters a negative value.
• A sample run of the previous program is given below:
Enter the numbers:
1
6
3
4
8
-1
The number of even values = 3
The number of odd values = 2
5
6
#include <iostream>
using namespace std;
int main()
{
int x;
int odd=0, even=0;
cout<<"Enter positive numbers (negative number to terminate):"<<endl;
while(true)
{
cin>>x;
if(x<0)
break;
else
{
if(x%2==0)
even++;
else
odd++;
}
}
cout<<"Even Numbers= "<< even <<endl;
cout<<"Odd numbers= "<< odd <<endl;
return 0;
}
Solution
Problem 3
• Write a program that asks the user to enter an integer
and reports all divisors in ascending order.
• An example is shown below, where the user entered
30.
Enter a number: 30
The divisors are: 1 2 3 5 6 10 15 30
7
Solution
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
cout << "The divisors are: ";
for (int i=1; i<=num; i++)
{
if (num%i == 0)
cout << i << " ";
}
return 0;
}
8
Problem 4
• Write a program that accepts numbers from the user
and then tells the maximum two numbers of them.
The results should be displayed when the user
enters a negative number.
• A sample run of the program should be like:
Enter numbers (less than 0 to end): 5
33
7
53
52
14
12
45
-1
The maximum two numbers are 53 and 52.
9
int main()
{
int x, max_Num1=0, max_Num2=0;
cout<<"Enter numbers (less than 0 to end): "<<endl;
while (true)
{
cin>> x;
if(x<0)
break;
else
{
if(x<max_Num1)
{
if(x>max_Num2)
max_Num2=x;
}
else
{
max_Num2=max_Num1;
max_Num1=x;
}
}
}
cout<<"Max 1= "<<max_Num1<<endl;
cout<<"Max 2= "<<max_Num2<<endl;
return 0;
} 10
Solution
Problem 5
• Write a program that takes as input the number of
seconds after midnight. It then displays the time in
hours:minutes:seconds format.
• Your program should show output as in the example
below.
Enter number of seconds after midnight: 3601
The time is 01:00:01
11
Solution
#include <iostream>
using namespace std;
int main( )
{
int seconds;
cout << "Enter number of seconds after midnight: ";
cin >> seconds;
int hours = seconds / 3600;
int mins = (seconds-hours*3600)/60;
int secs = (seconds-hours*3600)%60;
cout << "The time is ";
if (hours < 10)
cout << "0";
cout << hours << ":";
if (mins < 10)
cout << "0";
cout << mins << ":";
if (secs < 10)
cout << "0";
cout << secs << "n";
return 0;
} 12
Thank you 

More Related Content

Similar to Lab-11-C-Problems.pptx

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
premrings
 
prathamesh .ppt 1.pptx
prathamesh  .ppt 1.pptxprathamesh  .ppt 1.pptx
prathamesh .ppt 1.pptx
akashgorade1
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
melbruce90096
 
P3
P3P3
P3
lksoo
 
10 template code program
10 template code program10 template code program
10 template code program
Bint EL-maghrabi
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
T17Rockstar
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
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
jminrin0212
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
Syed Umair
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
ssuserc77a341
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
co4spmeley
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
Marco Izzotti
 
C++ project
C++ projectC++ project
C++ project
Sonu S S
 
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
sivakumarmcs
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
Prof. Dr. K. Adisesha
 
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docxCMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
clarebernice
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Mohamed El Desouki
 

Similar to Lab-11-C-Problems.pptx (18)

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
 
prathamesh .ppt 1.pptx
prathamesh  .ppt 1.pptxprathamesh  .ppt 1.pptx
prathamesh .ppt 1.pptx
 
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
Week 2PRG 218   Variables and Input and Output OperationsWrite.docxWeek 2PRG 218   Variables and Input and Output OperationsWrite.docx
Week 2PRG 218 Variables and Input and Output OperationsWrite.docx
 
P3
P3P3
P3
 
10 template code program
10 template code program10 template code program
10 template code program
 
C101-PracticeProblems.pdf
C101-PracticeProblems.pdfC101-PracticeProblems.pdf
C101-PracticeProblems.pdf
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
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
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
 
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docxWeek 2PRG 218Variables and Input and Output OperationsWrite .docx
Week 2PRG 218Variables and Input and Output OperationsWrite .docx
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
C++ project
C++ projectC++ project
C++ project
 
Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04Object Oriented Design and Programming Unit-04
Object Oriented Design and Programming Unit-04
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docxCMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 

More from ShimoFcis

Motif Finding.pdf
Motif Finding.pdfMotif Finding.pdf
Motif Finding.pdf
ShimoFcis
 
05_SQA_Overview.ppt
05_SQA_Overview.ppt05_SQA_Overview.ppt
05_SQA_Overview.ppt
ShimoFcis
 
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptxTopic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
ShimoFcis
 
4-DES.pdf
4-DES.pdf4-DES.pdf
4-DES.pdf
ShimoFcis
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
ShimoFcis
 
Mid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptxMid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptx
ShimoFcis
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
ShimoFcis
 
Lecture Cloud Security.pptx
Lecture Cloud Security.pptxLecture Cloud Security.pptx
Lecture Cloud Security.pptx
ShimoFcis
 
mapreduce.pptx
mapreduce.pptxmapreduce.pptx
mapreduce.pptx
ShimoFcis
 
storage-systems.pptx
storage-systems.pptxstorage-systems.pptx
storage-systems.pptx
ShimoFcis
 
mapreduce-advanced.pptx
mapreduce-advanced.pptxmapreduce-advanced.pptx
mapreduce-advanced.pptx
ShimoFcis
 

More from ShimoFcis (11)

Motif Finding.pdf
Motif Finding.pdfMotif Finding.pdf
Motif Finding.pdf
 
05_SQA_Overview.ppt
05_SQA_Overview.ppt05_SQA_Overview.ppt
05_SQA_Overview.ppt
 
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptxTopic21 Elect. Codebook, Cipher Block Chaining.pptx
Topic21 Elect. Codebook, Cipher Block Chaining.pptx
 
4-DES.pdf
4-DES.pdf4-DES.pdf
4-DES.pdf
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 
Mid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptxMid-Term Problem Solving Part.pptx
Mid-Term Problem Solving Part.pptx
 
Lecture 6.pptx
Lecture 6.pptxLecture 6.pptx
Lecture 6.pptx
 
Lecture Cloud Security.pptx
Lecture Cloud Security.pptxLecture Cloud Security.pptx
Lecture Cloud Security.pptx
 
mapreduce.pptx
mapreduce.pptxmapreduce.pptx
mapreduce.pptx
 
storage-systems.pptx
storage-systems.pptxstorage-systems.pptx
storage-systems.pptx
 
mapreduce-advanced.pptx
mapreduce-advanced.pptxmapreduce-advanced.pptx
mapreduce-advanced.pptx
 

Recently uploaded

How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

Lab-11-C-Problems.pptx

  • 1. Introduction to Computers Lab 11 First Year (2016– 2017) Introduction to Computers Lab First Year 2016– 2017
  • 2. Problem 1 • Write a program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the product of all positive entries. • A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -13 The product of all your positive numbers is 60. 2
  • 3. Solution #include <iostream> using namespace std; int main( ) { int x = 1; int product = 1; while ( x > 0 ) { product *= x; cout << "Enter a number: "; cin >> x; } cout << "The product of all your positive numbers is " << product << endl; return 0; } 3
  • 4. Another Solution: using break 4 #include <iostream> using namespace std; int main( ) { int x = 1, product = 1; while (true) { cout << "Enter a number: "; cin >> x; if (x <= 0) break; product *= x; } cout << "The product of all your positive numbers is " << product << endl; return 0; }
  • 5. Problem 2 • Write a program that accepts from the user a list of positive integers and displays the number of even and odd values in the list. The program accepts numbers from the user until he enters a negative value. • A sample run of the previous program is given below: Enter the numbers: 1 6 3 4 8 -1 The number of even values = 3 The number of odd values = 2 5
  • 6. 6 #include <iostream> using namespace std; int main() { int x; int odd=0, even=0; cout<<"Enter positive numbers (negative number to terminate):"<<endl; while(true) { cin>>x; if(x<0) break; else { if(x%2==0) even++; else odd++; } } cout<<"Even Numbers= "<< even <<endl; cout<<"Odd numbers= "<< odd <<endl; return 0; } Solution
  • 7. Problem 3 • Write a program that asks the user to enter an integer and reports all divisors in ascending order. • An example is shown below, where the user entered 30. Enter a number: 30 The divisors are: 1 2 3 5 6 10 15 30 7
  • 8. Solution #include <iostream> using namespace std; int main() { cout << "Enter a number: "; int num; cin >> num; cout << "The divisors are: "; for (int i=1; i<=num; i++) { if (num%i == 0) cout << i << " "; } return 0; } 8
  • 9. Problem 4 • Write a program that accepts numbers from the user and then tells the maximum two numbers of them. The results should be displayed when the user enters a negative number. • A sample run of the program should be like: Enter numbers (less than 0 to end): 5 33 7 53 52 14 12 45 -1 The maximum two numbers are 53 and 52. 9
  • 10. int main() { int x, max_Num1=0, max_Num2=0; cout<<"Enter numbers (less than 0 to end): "<<endl; while (true) { cin>> x; if(x<0) break; else { if(x<max_Num1) { if(x>max_Num2) max_Num2=x; } else { max_Num2=max_Num1; max_Num1=x; } } } cout<<"Max 1= "<<max_Num1<<endl; cout<<"Max 2= "<<max_Num2<<endl; return 0; } 10 Solution
  • 11. Problem 5 • Write a program that takes as input the number of seconds after midnight. It then displays the time in hours:minutes:seconds format. • Your program should show output as in the example below. Enter number of seconds after midnight: 3601 The time is 01:00:01 11
  • 12. Solution #include <iostream> using namespace std; int main( ) { int seconds; cout << "Enter number of seconds after midnight: "; cin >> seconds; int hours = seconds / 3600; int mins = (seconds-hours*3600)/60; int secs = (seconds-hours*3600)%60; cout << "The time is "; if (hours < 10) cout << "0"; cout << hours << ":"; if (mins < 10) cout << "0"; cout << mins << ":"; if (secs < 10) cout << "0"; cout << secs << "n"; return 0; } 12