0% found this document useful (0 votes)
0 views4 pages

Practice Problem Set 3

The document is a practice problem set for a computational problem-solving course, focusing on C++ programming concepts. It includes various exercises that require students to predict outputs of code snippets, identify errors, and write programs for specific tasks without using certain statements. Additionally, it emphasizes the importance of understanding the reasoning behind outputs and encourages hands-on experimentation with code.

Uploaded by

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

Practice Problem Set 3

The document is a practice problem set for a computational problem-solving course, focusing on C++ programming concepts. It includes various exercises that require students to predict outputs of code snippets, identify errors, and write programs for specific tasks without using certain statements. Additionally, it emphasizes the importance of understanding the reasoning behind outputs and encourages hands-on experimentation with code.

Uploaded by

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

CS-100: Computational Problem Solving

Practice Problem Set 3


You don’t need to submit it!

1. What would be the output of the following statements, if they are used inside a C++
program? Assume that values of variables a, b, c and d will be printed at screen using
cout statement. Try to find it without using computer. Then verify your solutions through
computer. (It may also be possible that these statement cause errors)

char a; char a; char a;


char b; char b; char b;
char c; char c; char c;
a=’2’; a=”2”; a=’abc’;
b=3; b=’3’; b=’3’;
c=’c’; c=’c’; c=’’;

char a; char a; char a;


char b; char b; char b;
char c; int c; int c;
a=’2’; a=’2’; a=’a’;
b=’3’; b=’3’; b=’b’;
c=a+b; c=a*b; c=a*b;

char a; int a; char a;


char b; int b; char b;
float c; int c; char c;
a=’a’; a=’a’; a=’a’;
b=’b’; b=’b’; b=a;
c=a*b; c=a*b; c=a*b;

char a; char a; char a;


char b; char b; char b;
char c; char c; char c;
a=2; a=’p’; a=’p’;
b=a; b=’q’; b=’q’;
c=b; c=a/b; c=a%b;

[You are also encouraged to experience such kind of more programs yourselves, and the
reasoning of various outputs should be very clear in your mind.]

2. What would be the output of following programs? If any error, then point it out. Please
note that there might be some weird combinations which don’t give a syntax error. Do
give them a try on paper and then verify your answer using the computer.

Dr. Malik Jahan Khan June 18, 2025


CS-100: Computational Problem Solving

#include<iostream #include<iostream #include<iostream


using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int a=300,b,c; int a=500, b,c; int a=200,b,c;
while (a>=400) while (a>=400) while (a>=400)
b=300; b=300; c=200; a = 300;
c=200; b=300;
c=200;
cout<<"\n"<<b;
cout<<"\n"<<b; cout<<"\n"<<b;
cout<<"\n"<<a;
cout<<"\n"<<c;
cout<<"\n"<<c; return 0;
return 0;
return 0; }
}
}
#include<iostream #include<iostream> #include<iostream>
using namespace std; using namespace std; using namespace std;
int main() int main() int main()
{ { {
int y = 0, x = 1, string x=“CS100”; string x = "CS100";
total = 0;
string y = “”; string y = "";
while(x <= 10)
{ int i = 0;
y = x * x; while(x.length()<6) char temp;
total += y; { while(i<=x.length()-1)
++x; y = y + x[3] + x[2]; {
}
x = x + y; temp = x[i] + 1;
cout<<total;
} y = y + temp;
return 0; cout<<x<<”…”<<y;
++i;
} return 0;
} }
cout<<"Encrypted
text = "<<y;
return 0;
}

Dr. Malik Jahan Khan June 18, 2025


CS-100: Computational Problem Solving

3. Write a program which takes two strings as input and checks whether any one of them is a substring
of the other. You are not allowed to use the if statement in this program.

4. Write a program which takes a string as input and counts how many words are there in the string.
For example, the string “I am a student” contains four words.

5. Write a program which takes a string as the input, reverses its characters and stores the result in a
new string. Then it outputs the result. For example, if the input is “abcd” then the output would be
“dcba”.

6. Write a program to print first 20 odd integers using while loop.

10 Write a program to print all multiples of 3 from 100 to 20 (i.e. in the reverse order) using while loop.

11 Write a program to count the number of common factors of two input integers using while loop. For
example, if the input is 18 and 30 then the output should be 4.

12 Write a program to check whether a given input integer is a prime. Prime integers are the integers
having exactly two factors.

13 Write a program which takes two integers as the input and computes their greatest common divisor
(GCD). For example, GCD of 30 and 55 is 5.

14 Write a program which keeps on taking an integer as the input from the user until the user inputs 0.
It will add all positive integers in a variable named “positives” and add all negative integers in a
variable named “negatives”. At the end, it will display both resultant variables.

15 Write a program which keeps on taking positive integers as the input until the user enters 0 or a
negative integer. It displays the smallest and largest of all positive integers entered by the user.

16 Modify your solution of the above problem to find the second largest integer.

17 Write a program which keeps on taking integers until the user enters 0. It outputs the integers which
have been repeated consecutively (adjacent duplicates). For example, if the input is 2, 2, 9, 5, 5, 1,
3, 3 then the output should be 2, 5 and 3.

18 Write a program which takes a string as the input and prints every other letter of the string. For
example, if the input is “greatness” then the output should be “rans”.

19 Write a program which takes a string as the input and prints only upper-case letters in the string.

20 Write a program which takes a string as the input and changes case of each letter. For example, if
the input is “sTarS” then the output should be “StARs”.

21 Write a program which counts number of vowels in a string.

22 Convert the following program into an equivalent program which doesn’t use if statement. Instead

Dr. Malik Jahan Khan June 18, 2025


CS-100: Computational Problem Solving

use switch statement:

#include <iostream>
using namespace std;
int main() {
int x, y;
cout<<"Enter first number";
cin>>x;
cout<<"Enter second number";
cin>>y;
if (x > y) {
cout<<"\n First number is greater than the second number";
}
else if (x < y) {
cout<<"\n Second number is greater than the first number";
}
else {
cout<<"\n Both of them are equal";
}
return 0;
}

NOTE

When we read a string using cin statement, it reads the first word and truncates the input after the first
space. To overcome this issue:

Replace: cin>>x; with getline(cin, x); Try both of them and see the difference.

Dr. Malik Jahan Khan June 18, 2025

You might also like