Lab05 (While - Do While)
Lab05 (While - Do While)
(Lab 05)
2. Write a C++ program to find all factors of a natural number using while
loop.
#include <iostream>
using namespace std;
int main() {
int num, i=1;
cout << "Enter a positive integer: ";
cin >> num;
cout << "Factors of " << num << " are: ";
while (i<=num)
{
if (num%i == 0) cout<<i<<" ";
i++;
}
return 0;
}
3. Write a C++ program to find power of a number using while loop.
#include <iostream>
using namespace std;
int main()
{
int base, exponent,result=1;
while(exponent > 0)
{
result = result * base;
exponent--;
}
cout<<"The result of "<<base<<"^"<<exponent1<<" is: " <<result;
return 0;
}
while(n1 != n2) {
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
cout << "HCF = " << n1;
return 0;
}
6. Write a C++ program to print Fibonacci series up to n terms.
#include <iostream>
using namespace std;
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
nextTerm = t1 + t2;
while(nextTerm <= n) {
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int decimal, binary = 0, remainder, product = 1;
cin >> decimal;
while (decimal != 0) {
remainder = decimal % 2;
binary = binary + (remainder * product);
decimal = decimal / 2;
product *= 10;
}
cout << "The number in the binary form is: " << binary ;
return 0;
}
Ex) Write a program that provides the user with the ability to continue
entering the correct numbers until he enters a number consisting of three
digits, and the entry process stops, and then on the output screen prints the
rate of all the numbers entered using While loop & do While loop
EX) write a code in C++. It asks the user to enter 10 characters, and then the
program prints the number of each of the entered uppercase and lowercase
letters, in addition to printing the number of the entered digits. using While
loop & do While loop