0% found this document useful (0 votes)
12 views3 pages

Do While (Unsolve Questions)

Uploaded by

maryam osama
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)
12 views3 pages

Do While (Unsolve Questions)

Uploaded by

maryam osama
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/ 3

Do While loop

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.

Using while loop:


1
#include<iostream>
using namespace std;

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;
}

a. What is the output if the input is 7 11?


b. What is the output if the input is -4 6?
c. What is the output if the input is 6 9?
d. What is the output if the input is 22 27?

You might also like