You are on page 1of 4

Page 1 of 4

TASK #1:
Write a program in C++ that take display your name five times, using do-while loop.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int x=0;
do
{
cout << "KHAWAR KHALIL\n";
++x;
}
while(x<5);
}
Task #2:
Write a program in C++ using do-while loop that take multiple integer types values from
the user and calculate the sum after entering 0. The code should terminate at 0.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int num, sum=0;
do
{
cout<<"Enter a number: ";
cin>>num;
sum+=num;
}
while(num!=0);
cout<<"The sum is "<<sum<<endl;
}
TASK #3:
Write a program in C++ that take three scores from user and calculate the average using
do-while loop. It repeats as many times as the user wishes.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3, avg;
char c='n';
do
Page 2 of 4

{
cout<<"Enter score 1: ";
cin>>num1;
cout<<"Enter score 2: ";
cin>>num2;
cout<<"Enter score 3: ";
cin>>num3;
avg = (num1 + num2 + num3 / 3 );
cout<<"\nAverage is: "<<(avg/3);
cout<<"\n\nYou want calculate again (y/n)? : ";
cin>>c;
cout<<endl;
}
while(c!='n');
}
Page 3 of 4

TASK #4:
Convert the following while loop to do-while loop and show the output after execution.
int x=1;
while ( x > 0 )
{
cout<<”enter a number”;
cin>>x;
}
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int x=1;
do
{
cout<<"enter a number";
cin>>x;
}
while ( x > 0 );
}

TASK #5:
Convert the following do-while loop to while loop and show the output after execution.
char sure;
do
{
cout<<”Are you sure you want to quit ? ”;
cin>>sure;
} while (sure! = 'Y' && sure!= 'N' );
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
char sure;
while (sure != 'Y' && sure != 'N' )
{
cout<<"Are you sure you want to quit ? ";
cin>>sure;
}
}

TASK #6:
Convert the following while loop to for loop and show the output after execution.
Page 4 of 4

int count =0;


while (count < 50)
{
cout<<”count is “ << count<<endl;
count++;
}
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
for(int count=0; count<50; count++)
cout<<"count is " << count<<endl;
}

Prepared By: Khawar Khalil

You might also like