0% found this document useful (0 votes)
65 views15 pages

Chapter 1 4 Progbook

Uploaded by

lemuelcanva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views15 pages

Chapter 1 4 Progbook

Uploaded by

lemuelcanva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CHAPTER 1 – 4 ANSWERS

Benedict N. Dela Cruz Prof: Melody Gabas


BSIT – 1B Date:31/05/2024

CHAPTER 1
1.
A. FALSE
B. FALSE
C. FALSE
D. TRUE
E. TRUE
F. FALSE
G. TRUE
H. TRUE
I. FALSE
J. TRUE
K. FALSE

2. A, C, D and E
3. NO, THEY ARE NOT THE SAME
4. If x=5, y=6, z=4, and w=3.5, evaluate each of the following statements, possible, if is not
possible, state the reason.

A. Possible
B. Modulus operator are not applicable to float
C. Modulus operator are not applicable to float
D. Possible
E. Possible
F. Possible
G. Possible
H. Possible

5. A = 3;
B = 4;
C = (a%b)*6; = 18
D = c/b; = 4
E = (a+b+c+d)/4; = 7

6. The following valid c++ assignment statements are


A, C and D

7.

A. 32*a
B. char resultA= '8'
C. char a[100] = "Julie Nelson"
D. (b * b - 4 * a * c) / (2 * a)
E. ((a + b) / (c * e * f)) - (g * h * (b + (b * b - 4 * a * c)) / (2 * a))

8.

x = 20
z=6
w = 11.5
t = 4.5

9. a = .5
b = 24.5
c = it’s error it should be cout<<a/static_cast<double>(b)+2<<endl; to get 4.6
d = 8.3
e = 10
f = 38.75
10. A and C

CHAPTER 2

1. FUNCTION
2. MODULUS
3. RELATIONAL OPERATOR
4. ARITHMETIC OPERATOR
5. What separates employing an increment or decrement operator as a prefix and postfix in
expression evaluation is the timing of the value change. The precise moment of the value
change is considered in expression assessment. When (++x or --x) is used as a prefix, the
value is incremented or decremented first, and the modified value is utilized in the
expression. The expression starts with the current value and then modifies it by adding or
subtracting it when it appears as a postfix (x++ or x--).
6.

A. = 56
B. =1
C. =8
D. =7
E. Floating-point exception

7.
i = j++ - -- k = -4
i = k * -- i = 24
i = j * i++ = 5
8.
#include <iostream>

using namespace std;

int main() {

float area, cir, pie = 3.1416, rad;

cout << "Enter the radius: ";

cin >> rad ;

area = (rad * rad) * pie;

cir = 2*pie*rad;

cout << "\nThe are of the circle is: "<< area;

cout << "\nThe circumference of the circle is: "<< cir;

return 0;

9.
#include <iostream>

using namespace std;

int main() {

int num_grade;

int grade;

cout << "Enter the number of grades to be assessed: ";

cin >> num_grade;

for (int num = 1; num <= num_grade; num++)

cout << "\nEnter grade " <<num<< ": ";

cin >> grade;


if (grade <= 5)

cout << "\nPassed";

else

cout << "\nFailed";

return 0;

10.

#include <iostream>

using namespace std;

int main() {

int num_grade;

int grade;

cout << "Enter the number of grades to be assessed: ";

cin >> num_grade;

for (int num = 1; num <= num_grade; num++)

cout << "\nEnter grade: ";

cin >> grade;

if (grade <= 3)

cout << "\nPassed";

}
else

cout << "\nFailed";

return 0;

Chapter 3.

1. WHAT IS THE DIFFERENCE BETWEEN A STATEMENT AND COMPOUND STATEMENT?

 A compound statement is a collection of statements that the compiler treats as though


they were a single statement. A statement is the instructions given to the computer to do
any kind of action.
2. WHAT IS A CONDITIONAL STATEMENT? WRITE DOWN THE NAME OF CONDITIONAL
STATEMENTS PROVIDED BY C++?
 Control structures that allow code to make decisions are managed by conditional
statements. Another name for these statements is "decision-making statements."
Additionally, the if, if-else, switch, and if statements are the conditional statements that
C++ offers.
3. WHAT IS THE SIGNIFICANCE OF DEFAULT CLAUSE IN SWITCH STATEMENT?
 If the prerequisites to initiate code execution are not met, the switch case's default acts as
a stand-in.

4. DENTIFY THE ERROR(S), IF ANY, IN THE FOLLOWING PROGRAMS AND WRITE THEM
IN CORRECT FORMAT

a) #include <iostream>

using namespace std;

int main() {

int a, b, Max;

cout << "Enter 2 integers separeted by comma: ";

cin >> a >> b;

if (a > b)
{

Max = a;

cout << Max;

return 0;

b) #include <iostream>

using namespace std;

int main() {

int x, y;

cout << "Enter a number: ";

cin >> x;

for (y = 0; y < 10; y++)

if (x==y)

cout << (y + x) << " ";

else

cout << y << " ";

return 0;

5. WHAT WILL BE THE OUTPUT OF THE FOLLOWING PROGRAM SEGMENT:


A. Enter a value for x: 10
X IS 10
Enter a value for x: 11
X IS NOT 10
B. Enter age: 19
Legal age
Enter age: 56
Legal age

6. ANY INTEGER IS INPUT BY THE USER. WRITE A PROGRAM TO FIND OUT WHETHER
IT IS AN ODD OR EVEN NUMBER.

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter an integer: ";

cin >> num;

if (num % 2 == 1) {

cout << "The number " << num << " is odd." << endl;

} else {

cout << "The number " << num << " is even." << endl;

7. Prime or Not

#include <iostream>
using namespace std;

int main() {
int number;
bool prime = true;
cout << "Input integer: ";
cin >> number;

if (number <= 1) {
prime = false;
} else {
for (int i = 2; i * i <= number; i++) {
if (number % i == 0) {
prime = false;
break;
}
}
} if (prime) {
cout << "The number " << number << " is a prime number." << endl;
} else {
cout << "The number " << number << " is not a prime number." << endl;
}
}

8.
#include <iostream>
using namespace std;

int main() {
float quiz, mid, fin, ave;

cout << "Enter quiz score: ";


cin >> quiz;
cout << "Enter midterm score: ";
cin >> mid;
cout << "Enter final score: ";
cin >> fin;

ave = (quiz + mid + fin) / 3.0;

char finalgrade;
if (ave >= 90.0) {
finalgrade = 'A';
} else if (ave >= 70.0) {
finalgrade = 'B';
} else if (ave >= 50.0) {
finalgrade = 'C';
} else {
finalgrade = 'F';
}

cout << "Grade: " << finalgrade << endl;


}
9.
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an Integer: ";


cin >> number;

if (number >= 56 || number <= 78) {


cout<< "YOU WIN"<<endl;
} else {
cout<< "YOU LOSE"<<endl;
}
}

10.
#include <iostream>
using namespace std;
int main() {
float num1, num2;
char op;

cout << "Enter Two Integers: ";


cin >> num1 >> num2;

cout << "Enter operation: ";


cout << "\n[A]dd \n[S]ubtract \n[M]ultiply \n[D]ivide \n";

cout << "Choice: ";


cin >> op;

if (op == 'a' || op == 'A')


{
cout <<"The solution of "<< " " << num1 << " + " <<num2<< " = " <<num1+num2;
}
else if (op == 's' || op == 'S')
{
cout <<"The solution of "<< " " << num1 << " - " <<num2<< " = " <<num1-num2;
}
else if (op == 'm' || op == 'M')
{
cout <<"The solution of "<< " " << num1 << " x " <<num2<< " = " <<num1*num2;
}
else if (op == 'd' || op == 'D')
{
cout <<"The solution of "<< " " << num1 << " ÷ " <<num2<< " = " <<num1/num2;
}
else
{
cout << "Please enter the right operation";
}
return 0;
}

Chapter 4
1. WHAT IS THE DIFFERENCE BETWEEN A WHILE STATEMENT AND A DO-WHILE
STATEMENT?
 When a certain Boolean condition is met, code can be continuously executed using a
control flow statement known as a while loop. A do-while loop, on the other hand, is a
control flow statement that terminates the loop or repeats the code block. The do while
construct is composed of a process symbol and a condition. The block's code is executed
first.

2. Is there any error in this program segment?


For (int k=2;k<=12; k++);
{
}

for (int k = 2; k <= 12; k++)


{

}
3. write a c++ program that asks the user for an integer and then prints out all its factors
in increasing order. Example input is 150, it should print:
2
3
4
5
5

#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;

if (number <= 0) {
cout << "Please enter a positive integer." << std::endl;
return 1;
}

cout << "Factors of " << number << " are: ";
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
cout << i << " ";
}
}
cout << endl;

return 0;
}

4. Create a program in c++ that asks the user to enter 10 integers and write the number of
occurrence of the biggest value

#include <iostream>
using namespace std;
int main() {
int number[9];
int count = 0;

for (int i = 0; i < 10; ++i)


{
cout << "Enter number: ";
cin >> number[i];
}
int val = number[0];
for (int i = 1; i < 10; ++i)
{
if (number[i] > val)
{
val = number[i];
}
}

for (int i = 0; i < 10; ++i)


{
if (number[i] == val)
{
++count;
}
}

cout << "The largest value is " <<


val << " and it occurs " << count << " times." << endl;

return 0;
}

5. Debug this program that aims to display the reverse the integer input. Write the exact
screen output when num is 12345
#include<iostream>
#include<stdlib.h
using namespace std;
main()
(
int num, rem;

cout<<”Enter a number: ”;
cin<<num;

do
{
rem=num%10;
num=num/100;
}
while(num)
cout<<”is the reverse:”<<endl;
system(pause);
}

Answer:
#include <iostream>
using namespace std;

int main() {
int num, re;
int rev = 0;

cout << "Enter a number: ";


cin >> num;
do {
re= num % 10;
rev = rev * 10 + re;
num = num / 10;
}
while (num != 0) ;{

cout << "The reverse is: " << rev << endl;
}

return 0;
}

6. Create a program that will display the fibonacci series.

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter the number of terms: ";

cin >> n;

int term1 = 0, term2= 1, nextTerm = 0;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {

if (i == 1) {

cout << term1 << ", ";

continue;

if (i == 2) {

cout << term2<< ", ";

continue;
}

nextTerm = term1 + term2;

term1 = term2;

term2 = nextTerm;

cout << nextTerm << ", ";

cout << "\n";

return 0;

7. Create a c++ program that will accept a positive integer and display it's factorial.

#include <iostream>
using namespace std;
int main() {
int number;

cout << "Enter a number: ";


cin >> number;

if (number <= 0) {
cout << "Please enter a positive integer." << std::endl;
return 1;
}

cout << "Factors of " << number << " are: ";
for (int i = 1; i <= number; i++) {
if (number % i == 0) {
cout << i << " ";
}
}
cout << endl;

return 0;
}

8. What will be the output if integer is 12345?


#include <iostream>

using namespace std;

int main() {

int dig, ans;

cout << "Enter an integer: ";

cin >> dig;

while(dig > 0)

ans+= dig % 10;

dig /= 10;

cout <<endl<<ans;

return 0;

Answer:

You might also like