Write the output of the following programs (if any).
If there is an error in the program, correct the
code and then write the output.
Tip: Use python tutor (https://pythontutor.com/visualize.html#mode=edit) for line by line execution of
programs for a better understanding, first try to solve by yourself.
int main() Output
{
int i;
for (i = 0, i++; i <= 2; i = 0, i += 3)
cout << i;
return 0;
}
int main()
{
int choice = 5;
switch (choice) {
default:
cout << "\nI am in Default";
case 1:
cout << "\nI am in case 1";
break;
case 2:
cout << "\nI am in case 2";
break;
case 3:
cout << "\nI am in case 3";
break;
}
return 0;
}
int main()
{
float num1 = 1.1;
double num2 = 1.1;
if (num1 == num2)
cout << "Stanford";
else
cout << "Islamabad";
return 0;
}
int main()
{
int y = 0;
switch (y) {
case 0: y = y + 5;
case 1: y = y / 2;
case 2: y = y * 3;
case 3: y = y + 10;
default: y = y % 3;
}
cout << y << endl;
return 0;
}
int main()
{
int i = 5, j = 3, k = 4;
if (i % j + i < k) {
cout << (k > i < j);
}
else {
cout << (i < j == j < k);
}
return 0;
}
int main()
{
int i = 3, j = 3, k = 3;
if (--i - 7 && j++ < ++k)
cout << ++i;
else
cout << i << j << k;
return 0;
}
int main()
{
int n = 5;
while (n >= 0)
{
cout << --n * n++ << endl;
n--;
}
while (n > 0)
cout << (n /= 2) << endl;
return 0;
}
int main()
{
int i, j, m, answer;
m = 0;
j = 3;
while (m < 3) {
for (i = 0; i < j; i++) {
answer = i * m;
cout << answer;
}
m = m + 1;
cout << endl;
}
return 0;
}
int main()
{
int i = 2, j = 2;
while (i + 1 ? --i : j++)
cout << " " << i;
}
int main()
{
int y = 10;
if (y++ > 9 && y++ != 11 && y++ > 11)
cout << y;
else
cout << y;
}
int main()
{
unsigned char i = 0;
for (; i >= 0; i++);
cout << static_cast<int>(i) <<"\n";
}
int main()
{
unsigned int i = 1;
while (i-- >= 0)
cout << i;
}
int main()
{
int i = 0;
while (+(+i--) != 0)
i -= i++;
cout << i;
}
int main()
{
int i = 0;
for (i = 0; i < 30; i++)
{
switch (i)
{
case 0:
i += 5;
case 1:
i += 2;
case 5:
i += 5;
default:
i += 4;
break;
}
cout << i << " ";
}
}
int main()
{
int a;
int b = 1;
int x[5] = { 1, 2, 3, 4, 5 };
a = 5 * 4 + x[--b] - (9 / b);
cout << a;
}
int main()
{
int n = 6, x = 2, i = 0;
while (i <= n)
{
if (i % 2 == 1)
x = x + pow(2, i) * i;
i++;
cout << x << "-";
}
return 0;
}
int main() {
int i = 0, x = 0;
do
{
if (i % 5 == 0)
{
cout << x;
x++;
}
++i;
} while (i < 10);
cout << x;
return 0;
}
int main() {
int K = 5;
int I = -2;
while (I <= K) {
I = I + 2; --K;
cout << (I + K) << endl;
}
return 0;
}
int main() {
char i = 0;
for (; i++; cout << int(i));
cout << int(i);
return 0;
}
int main() {
int count = 0;
for (;;) {
if (count == 10)
break;
cout << ++count;
}
return 0;
}
int main() {
int count;
for (count = 0; count < 10; ++count) {
cout << "#";
if (count > 6)
continue;
cout << count;
}
return 0;
}
int main() {
int loopvar = 5;
while (cout << "Hello " && loopvar--);
return 0;
}
int main() {
int i, j, var = 'A';
for (i = 5; i >= 1; i--) {
for (j = 0; j < i; j++)
cout << char(i + var + j);
cout << endl;
}
return 0;
}
int main() {
int r = 5, x = 0;
while (x < r) {
int y = 1;
while (y < r - x) {
cout << " ";
y++;
}
int z = 0, n = 1;
while (z <= x) {
n = z == 0 || x == 0 ? 1
: n * (x - z + 1) / z;
char ch = n == 1 ? 'X' :
n % 3 == 0 ? 'Y' : 'V';
cout << ch << ' ';
z++;
}
cout << '\n';
x++;
}
}
int main()
{
int y = 2, x = 4, temp = 0;
temp = y == 2 ? x < 1 ? x + y + 4 : x + y - 4 : x + 9;
cout << temp;
}