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

Loop Programming Exercises

(Lab 05)

1. Write a C++ program to convert a number from binary to decimal.


#include<iostream>
using namespace std;
int main ()
{
int num, rem, temp, result = 0, b = 1;
cout << "Enter the binary number : ";
cin >> num;
temp = num;
while (temp > 0)
{
rem = temp % 10;
result = result + rem * b;
b *= 2;
temp /= 10;
}
cout << "The decimal equivalent of " << result << " is " << result;
return 0;
}

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;

cout<<"Enter base: ";


cin>>base;
cout<<"Enter exponent: ";
cin>>exponent;
int exponent1=exponent;

while(exponent > 0)
{
result = result * base;
exponent--;
}
cout<<"The result of "<<base<<"^"<<exponent1<<" is: " <<result;

return 0;
}

4. Write a C++ program to enter a number and print it in words.


#include <iostream>
using namespace std;
int main()
{
int n, num = 0;

cout<<"Enter any number to print in words: ";


cin>>n;

while(n != 0)// ‫لقلب الرقم‬


{
num = (num * 10) + (n % 10);
n /= 10;
}
while(num != 0)
{
switch(num % 10)
{
case 0: cout<<"zero ";
break;
case 1: cout<<"one ";
break;
case 2: cout<<"two ";
break;
case 3: cout<<"three ";
break;
case 4: cout<<"four ";
break;
case 5: cout<<"five ";
break;
case 6: cout<<"six ";
break;
case 7: cout<<"seven ";
break;
case 8: cout<<"eight ";
break;
case 9: cout<<"nine ";
break;
}
num = num / 10;
}
return 0;
}
5. Write a C++ program to find HCF (GCD) of two numbers.
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << "Enter two numbers:\n ";
cin >> n1 >> n2;

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;

cout << "Enter a positive number: ";


cin >> n;

// displays the first two terms which is always 0 and 1


cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";

nextTerm = t1 + t2;

while(nextTerm <= n) {
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

7. Write a C++ program to convert a number from decimal to binary.

#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

You might also like