You are on page 1of 3

Page 1 of 3

TASK #1:
Write a program in C++ to display your name 5 times on output screen, using while loop.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
int x=1;
while(x<=5)
{
cout << "KHAWAR KHALIL\n";
++x;
}
}

Task #2:
Write a program in C++ that take the integer type values for user and the program
terminates when the user enters 0. The program should display the sum of all entered
integers. Use while loop.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int num;
int sum=0;
cout<<"enter a number: ";
cin>>num;
while(num>0)
{
sum+=num;
cout<<"enter a number: ";
cin>>num;
}
cout<<"the sum is "<<sum<<endl;
}

TASK #3:
Write a program in C++ to find the factorial of number using while loop. The number must
be user defined.
SOLUTION:
#include <iostream>
using namespace std;
Page 2 of 3

int main()
{
int num, x=1, fact=1;
cout<<"Enter a Number: ";
cin>>num;
while(x<=num)
{
fact*=x;
x++;
}
cout<<"The Factorial is: "<<fact;
}

TASK #4:
Write a Program in C++ that shows the Fibonacci series using while loop. Series should be
user defined.
Hint: Fibonacci series 01 1 2 3 5 8 1 3 ….
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int temp1=0, temp2=1, Next=0, num;
cout << "Enter a number: ";
cin >> num;
cout << "Fibonacci Series: "
<< temp1 << " " << temp2 << " ";
Next = temp1 + temp2;

while(Next <= num)


{
cout << Next << " ";
temp1 = temp2;
temp2 = Next;
Next = temp1 + temp2;
}
}
TASK #5:
Write a Program in C++ that check the user defined number is Armstrong or not.
Hint: 153 is an Armstrong number
(1)3+(5)3 + (3)3 =153
SOLUTION:
#include <iostream>
using namespace std;
int main()
Page 3 of 3

{
int arm=0,a,b,c,d,num;
cout<<"Enter any number: ";
cin>>num;
d=num;
while(num>0)
{
a=num%10;
num=num/10;
arm=arm+a*a*a;
}
if(arm==d) cout<<"Armstrong";
else cout<<"Not Armstrong";
}

TASK #6:
Write a while loop program that lets the user to enter a number. The number should be
multiple by 10. and the result stored in a variable. The loop should iterate as long as the
variable contains a value less than 100.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
float store=0, input;
while(store < 100)
{
cout<<"Enter value: ";
cin>>input;
store = input*10;
}
cout<<"LOOP TERMINATED";
}

Prepared By: Khawar Khalil

You might also like