Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

LAB 11 TASKS

1. Using while loop, write a program to generate ODD integers only from 1 to 20.
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
while (i <= 20)
{
if (i % 2 != 0)
{
cout << i << endl;
}
i++;
}
return 0;
}
Output:

2. Write a C++ program that takes an integer value and decide if it is even or odd. Continue
asking for numbers till the user enters a negative value. The program then displays the
total number of even and odd integers given. Use While statement.
Code:
#include <iostream>
using namespace std;
int main()
{
int a = 0;
while (a >= 0)
{
cout << "Enter the number: ";
cin >> a;
if (a % 2 == 0)
{
cout << "Even" << endl;
}
else
{
cout << "Odd" << endl;
}
}
return 0;
}
Output:

3. Write a program using while statement, which takes a number from the user, calculates
and displays its factorial.
Code:
#include <iostream>
using namespace std;
int main()
{
int a,b=1,c=1;
cout << "Enter a Number: ";
cin >> a;
while (c<=a)
{
b = b * c;
c++;
}
cout << "The Factorial is " << b << endl;
}
Output:

You might also like