Do While (Unsolve Questions)
Do While (Unsolve Questions)
Example 1:
#include<iostream>
using namespace std;
int main()
{
int i = 0;
do
{
cout << i << " ";
i = i + 5;
}
while (i <= 20);
}
Example 1:
Example 2:
Write a C++ program to sum the numbers started by 0 and increment by 2 for ten times, use do while loop.
int main()
{
int sum=0;
while (sum <= 20)
{
sum = sum + 2;
cout<<sum<<endl;
}
return 0;
Example 3:
Write a C++ program to test whether number is divisible by 3 and 5 or not.
#include <iostream>
using namespace std;
int main()
{
int num, temp, sum;
cout << "Enter a positive integer: ";
cin >> num;
cout << endl;
temp = num;
sum = 0;
do
{
sum = sum + num % 10; //extract the last digit
//and add it to sum
num = num / 10; //remove the last digit
}
while (num > 0);
cout << "The sum of the digits = " << sum << endl;
if (sum % 9 == 0)
cout<< temp << " is divisible by 3 and 9" << endl;
else if (sum % 3 == 0)
cout<< temp << " is divisible by 3, but not 9" << endl;
else
cout<< temp << " is not divisible by 3 or 9" << endl;
return 0;
}
2
Example 4 : What is the output of the following C++ program segment?
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int temp;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << endl;
do
{
temp = 2 * (num1 + num2);
num1 = num2;
num2 = temp;
cout << temp << " ";
}
while (((temp + num1) % 3) != 0);
cout << endl;
return 0;
}