Practice C++ output basics with beginner-friendly questions on cout, variables, arithmetic expressions, and predicting console output in simple programs.
Task: Predict the output.
int i = 2;
while (i <= 6)
{
cout << i * i << " ";
i++;
}
Task: Predict the output.
int n = 20;
while (n >= 5)
{
cout << n << " ";
n -= 5;
}
Task: Predict the output.
int i = 1;
while (i <= 4)
{
cout << i << " ";
i += 2;
}
Task: Predict the output.
int i = 1;
while (i <= 3)
{
cout << "CS" << i << " ";
i++;
}
Task: Predict the output.
int x = 2;
while (x <= 8)
{
cout << x - 1 << " ";
x += 3;
}
Task: Predict the output.
int i = 4;
while (i >= 1)
{
cout << i % 2 << " ";
i--;
}
Task: Predict the output.
int x = 1;
while (x <= 4)
{
cout << x + 3 << " ";
x++;
}
Task: Predict the output.
int n = 3;
while (n > 0)
{
cout << n * 2 << " ";
n--;
}
cout << "Done";
Task: Predict the output.
int x = 2;
int sum = 0;
while (x <= 8)
{
sum += x;
x += 2;
}
cout << sum;
Task: Predict the output.
int n = 1;
int result = 1;
while (n <= 4)
{
result += n * 3;
n++;
}
cout << result;
Task: Predict the output.
int x = 1;
while (x <= 8)
{
if (x % 3 == 0)
cout << x << " ";
x++;
}
Task: Predict the output.
int x = 10;
while (x > 0)
{
x -= 3;
cout << x << " ";
}
Task: Predict the output.
int n = 2;
int count = 0;
while (n <= 12)
{
if (n % 4 == 0)
count++;
n += 2;
}
cout << count;
Task: Predict the output.
int x = 1;
int y = 8;
while (x <= 4)
{
cout << x + y << " ";
x++;
y -= 2;
}
Task: Predict the output.
int n = 1;
while (n <= 5)
{
if (n == 3)
cout << "Three ";
else
cout << n << " ";
n++;
}
Task: Predict the output.
int x = 2;
int product = 1;
while (x <= 6)
{
product *= x;
x += 2;
}
cout << product;
Task: Predict the output.
int n = 5;
while (n >= 1)
{
if (n % 2 == 1)
cout << n * 2 << " ";
n--;
}
Task: Predict the output.
int x = 1;
int total = 0;
while (x <= 4)
{
total += x * x;
x++;
}
cout << total;
Task: Predict the output.
int x = 1;
while (x <= 5)
{
cout << x << " ";
if (x % 2 == 0)
cout << "Even ";
x++;
}
Task: Predict the output.
int n = 1;
int value = 2;
while (n <= 4)
{
value += n;
cout << value << " ";
n++;
}
Task: Predict the output.
int n = 246;
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
cout << sum;
Task: Predict the output.
int n = 1357;
while (n > 0)
{
cout << n % 10 << " ";
n /= 100;
}
Task: Predict the output.
int x = 1;
int y = 2;
while (x <= 4)
{
y += x;
x++;
if (y > 5)
cout << y << " ";
}
Task: Predict the output.
int x = 1;
int count = 0;
while (x <= 10)
{
if (x % 2 == 0 && x % 3 != 0)
count++;
x++;
}
cout << count;
Task: Predict the output.
int x = 1;
while (x <= 4)
{
int y = x;
while (y > 0)
{
cout << y << " ";
y--;
}
x++;
}
Task: Predict the output.
int x = 1;
int y = 10;
while (x < y)
{
cout << x * y << " ";
x += 2;
y -= 3;
}
Task: Predict the output.
int n = 12345;
int result = 0;
while (n > 0)
{
int digit = n % 10;
if (digit % 2 == 1)
result += digit;
n /= 10;
}
cout << result;
Task: Predict the output.
int x = 2;
int total = 0;
while (x <= 10)
{
if (x % 4 == 0)
total += x;
else
total -= 1;
x += 2;
}
cout << total;
Task: Predict the output.
int n = 5;
int result = 0;
while (n > 0)
{
result = result * 2 + n;
n -= 2;
}
cout << result;
Task: Predict the output.
int n = 9876;
int reversed = 0;
while (n > 0)
{
reversed = reversed * 10 + n % 10;
n /= 10;
}
cout << reversed;
Task: Predict the output.
int x = 1;
int result = 0;
while (x <= 6)
{
if (x % 2 == 0)
result += x * 2;
else
result += x;
x++;
}
cout << result;
Task: Predict the output.
int n = 1;
int sum = 0;
while (n <= 20)
{
if (n % 4 == 0 || n % 6 == 0)
sum += n;
n++;
}
cout << sum;
Task: Predict the output.
int n = 2;
int count = 0;
while (n <= 50)
{
if (n % 7 == 0 && n % 2 == 0)
count++;
n += 2;
}
cout << count;
do-while Loop## Question 1:
Task: Predict the output.
### Code
int i = 2;
do
{
cout << i * i << " ";
i++;
}
while (i <= 6);
## Question 2:
Task: Predict the output.
### Code
int i = 1;
do
{
cout << i << " ";
i += 2;
}
while (i <= 9);
## Question 3:
Task: Predict the output.
### Code
int i = 5;
do
{
cout << i << " ";
i--;
}
while (i >= 1);
## Question 4:
Task: Predict the output.
### Code
int i = 1;
int sum = 0;
do
{
sum += i;
i++;
}
while (i <= 4);
cout << sum;
## Question 5:
Task: Predict the output.
### Code
int i = 2;
do
{
cout << i * 3 << " ";
i += 2;
}
while (i <= 10);
## Question 6:
Task: Predict the output.
### Code
int i = 10;
do
{
cout << i << " ";
i -= 3;
}
while (i > 0);
## Question 7:
Task: Predict the output.
### Code
int i = 1;
do
{
cout << i * i * i << " ";
i++;
}
while (i <= 4);
## Question 8:
Task: Predict the output.
### Code
int i = 1;
int count = 0;
do
{
count++;
i += 2;
}
while (i <= 10);
cout << count;
## Question 9:
Task: Predict the output.
### Code
int i = 1;
int sum = 0;
do
{
if (i % 2 == 0)
{
sum += i;
}
i++;
}
while (i <= 6);
cout << sum;
## Question 10:
Task: Predict the output.
### Code
int i = 5;
do
{
if (i % 2 == 0)
{
cout << i << " ";
}
i--;
}
while (i >= 1);
## Question 11:
Task: Predict the output.
### Code
int i = 1;
int x = 2;
do
{
x += i;
cout << x << " ";
i++;
}
while (i <= 4);
## Question 12:
Task: Predict the output.
### Code
int i = 1;
int sum = 0;
do
{
sum += i;
i += 2;
}
while (i <= 7);
cout << sum;
## Question 13:
Task: Predict the output.
### Code
int i = 1;
int n = 10;
do
{
cout << n - i << " ";
i += 2;
}
while (i <= 5);
## Question 14:
Task: Predict the output.
### Code
int n = 246;
int count = 0;
do
{
count++;
n /= 10;
}
while (n > 0);
cout << count;
## Question 15:
Task: Predict the output.
### Code
int i = 1;
int result = 0;
do
{
result = result * 10 + i;
i++;
}
while (i <= 4);
cout << result;
## Question 16:
Task: Predict the output.
### Code
int i = 1;
int sum = 0;
do
{
if (i % 3 == 0)
{
sum += i;
}
i++;
}
while (i <= 10);
cout << sum;
Task: Predict the output.
int x = 1;
while (x <= 3)
{
int y = 3;
while (y >= 1)
{
cout << x * y << " ";
y--;
}
x++;
}
Task: Predict the output.
int n = 1;
int sum = 0;
while (n <= 3)
{
int j = 1;
while (j <= 3)
{
sum += n * j;
j++;
}
n++;
}
cout << sum;
Task: Predict the output.
int x = 1;
int count = 0;
while (x <= 5)
{
int y = x;
while (y <= 5)
{
count++;
y += 2;
}
x++;
}
cout << count;
Task: Predict the output.
int x = 1;
int y = 1;
while (x <= 4)
{
y = 1;
while (y <= x)
{
cout << x + y << " ";
y++;
}
cout << endl;
x++;
}
Task: Predict the output.
int x = 1;
int count = 0;
while (x <= 4)
{
int y = 1;
while (y <= 4)
{
if (x + y > 4)
count++;
y++;
}
x++;
}
cout << count;
Task: Predict the output.
int x = 1;
int result = 0;
while (x <= 4)
{
int y = 1;
while (y <= x)
{
result += x - y;
y++;
}
x++;
}
cout << result;
Task: Predict the output.
int x = 1;
int y = 5;
while (x <= 3)
{
int z = y;
while (z >= x)
{
cout << z << " ";
z--;
}
cout << endl;
x++;
y--;
}
Practice tip: Trace each loop iteration in a small table with columns for the variables, condition, and output. Pay special attention to when variables change and when the condition is checked again.