0% found this document useful (0 votes)
18 views7 pages

While Loop Programming Exercises

The document contains a series of C++ programming tasks that utilize while loops to perform various operations such as printing natural numbers, summing digits, reversing numbers, counting digits, generating multiplication tables, calculating factorials, checking for palindromes, counting even and odd numbers, computing powers, and implementing games. Additionally, it includes advanced tasks like finding the GCD using the Euclidean algorithm, checking Armstrong numbers, generating Fibonacci sequences, checking for prime numbers, and calculating digital roots. Each task is presented with its corresponding C++ code implementation.

Uploaded by

mamoonaibrahim8b
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

While Loop Programming Exercises

The document contains a series of C++ programming tasks that utilize while loops to perform various operations such as printing natural numbers, summing digits, reversing numbers, counting digits, generating multiplication tables, calculating factorials, checking for palindromes, counting even and odd numbers, computing powers, and implementing games. Additionally, it includes advanced tasks like finding the GCD using the Euclidean algorithm, checking Armstrong numbers, generating Fibonacci sequences, checking for prime numbers, and calculating digital roots. Each task is presented with its corresponding C++ code implementation.

Uploaded by

mamoonaibrahim8b
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

While Loop Practice Questions

Task 1: Print Natural Numbers


#include <iostream>
using namespace std;

int main() {
int n, i = 1;
cin >> n;
while (i <= n) {
cout << i << " ";
i++;
}
return 0;
}

Task 2: Sum of Digits


#include <iostream>
using namespace std;

int main() {
int n, sum = 0;
cin >> n;
while (n > 0) {
sum += n % 10;
n /= 10;
}
cout << sum;
return 0;
}

Task 3: Reverse a Number


#include <iostream>
using namespace std;
int main() {
int n, rev = 0;
cin >> n;
while (n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
cout << rev;
return 0;
}

Task 4: Count Digits


#include <iostream>
using namespace std;

int main() {
int n, count = 0;
cin >> n;
while (n > 0) {
count++;
n /= 10;
}
cout << count;
return 0;
}

Task 5: Multiplication Table


#include <iostream>
using namespace std;

int main() {
int n, i = 1;
cin >> n;
while (i <= 10) {
cout << n << " x " << i << " = " << n * i << endl;
i++;
}
return 0;
}
Task 6: Factorial of a Number
#include <iostream>
using namespace std;

int main() {
int n, fact = 1;
cin >> n;
while (n > 0) {
fact *= n;
n--;
}
cout << fact;
return 0;
}

Task 7: Palindrome Number


#include <iostream>
using namespace std;

int main() {
int n, original, rev = 0;
cin >> n;
original = n;
while (n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
cout << (rev == original);
return 0;
}

Task 8: Even and Odd Counter


#include <iostream>
using namespace std;

int main() {
int n, i = 1, even = 0, odd = 0;
cin >> n;
while (i <= n) {
if (i % 2 == 0) even++;
else odd++;
i++;
}
cout << "Even: " << even << " Odd: " << odd;
return 0;
}

Task 9: Power of a Number


#include <iostream>
using namespace std;

int main() {
int a, b, result = 1;
cin >> a >> b;
while (b > 0) {
result *= a;
b--;
}
cout << result;
return 0;
}

Task 10: Guess the Number Game


#include <iostream>
using namespace std;

int main() {
int guess, secret = 42, attempts = 0;
cout << "Guess the secret number: ";
cin >> guess;
while (guess != secret) {
attempts++;
cout << "Wrong! Try again: ";
cin >> guess;
}
attempts++;
cout << "Correct! Attempts: " << attempts;
return 0;
}

🔹 Advanced While-Loop Tasks


Task 11: GCD (Euclidean Algorithm)
#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
cout << "GCD = " << a;
return 0;
}

Task 12: Armstrong Number Checker


#include <iostream>
#include <cmath>
using namespace std;

int main() {
int n, original, sum = 0, digits = 0;
cin >> n;
original = n;

int temp = n;
while (temp > 0) {
digits++;
temp /= 10;
}

temp = n;
while (temp > 0) {
sum += pow(temp % 10, digits);
temp /= 10;
}

cout << (sum == original);


return 0;
}

Task 13: Fibonacci Sequence until N


#include <iostream>
using namespace std;

int main() {
int n, a = 0, b = 1;
cin >> n;
cout << a << " " << b << " ";
while (a + b <= n) {
int c = a + b;
cout << c << " ";
a = b;
b = c;
}
return 0;
}

Task 14: Prime Number Check


#include <iostream>
using namespace std;

int main() {
int n, i = 2, isPrime = 1;
cin >> n;

if (n <= 1) isPrime = 0;
else {
while (i * i <= n) {
if (n % i == 0) {
isPrime = 0;
break;
}
i++;
}
}
cout << isPrime;
return 0;
}

Task 15: Digital Root


#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;

while (n >= 10) {


int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
n = sum;
}

cout << "Digital Root = " << n;


return 0;
}

You might also like