While Loop in C++

 

🔄 While Loop in C++

The while loop executes a block of code repeatedly as long as the condition is true.

🔹 Syntax

while (condition) {
    // Code to execute
}
  • The condition is checked before each iteration.
  • The loop runs until the condition becomes false.

1️⃣ Armstrong Number using While Loop

📌 An Armstrong number is a number where the sum of its digits, each raised to the power of the number of digits, equals the number itself.
👉 Example: 153 = 1³ + 5³ + 3³ = 153

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

int main() {
    int num, originalNum, remainder, result = 0, n = 0;
    cout << "Enter a number: ";
    cin >> num;
    
    originalNum = num;
    
    while (originalNum != 0) {
        originalNum /= 10;
        n++;  // Count digits
    }
    
    originalNum = num;
    while (originalNum != 0) {
        remainder = originalNum % 10;
        result += pow(remainder, n);
        originalNum /= 10;
    }
    
    if (result == num)
        cout << num << " is an Armstrong number." << endl;
    else
        cout << num << " is not an Armstrong number." << endl;
    
    return 0;
}

🔹 Output

Enter a number: 153
153 is an Armstrong number.

2️⃣ Decimal to Binary in C++

📌 Convert a decimal number to binary using a while loop.

#include <iostream>
using namespace std;

int main() {
    int num, binary = 0, place = 1, remainder;
    cout << "Enter a decimal number: ";
    cin >> num;

    while (num > 0) {
        remainder = num % 2;
        binary += remainder * place;
        place *= 10;
        num /= 2;
    }

    cout << "Binary: " << binary << endl;
    return 0;
}

🔹 Output

Enter a decimal number: 10
Binary: 1010

3️⃣ Printing Even & Odd Numbers

📌 Print even & odd numbers separately using a while loop.

Even Numbers

#include <iostream>
using namespace std;

int main() {
    int i = 2;
    while (i <= 10) {
        cout << i << " ";
        i += 2;
    }
    return 0;
}

🔹 Output

2 4 6 8 10

Odd Numbers

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 10) {
        cout << i << " ";
        i += 2;
    }
    return 0;
}

🔹 Output

1 3 5 7 9

4️⃣ Printing Positive & Negative Numbers

📌 Separate positive and negative numbers from an array using a while loop.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {-10, 15, -3, 8, -7, 12, 5, -1};
    int size = sizeof(arr) / sizeof(arr[0]);
    int i = 0;

    cout << "Positive numbers: ";
    while (i < size) {
        if (arr[i] > 0) {
            cout << arr[i] << " ";
        }
        i++;
    }

    i = 0;
    cout << "\nNegative numbers: ";
    while (i < size) {
        if (arr[i] < 0) {
            cout << arr[i] << " ";
        }
        i++;
    }

    return 0;
}

🔹 Output

Positive numbers: 15 8 12 5
Negative numbers: -10 -3 -7 -1

5️⃣ Prime Numbers between Two Numbers

📌 Find all prime numbers in a given range using a while loop.

#include <iostream>
using namespace std;

bool isPrime(int num) {
    if (num < 2) return false;
    int i = 2;
    while (i * i <= num) {
        if (num % i == 0) return false;
        i++;
    }
    return true;
}

int main() {
    int start, end;
    cout << "Enter range (start end): ";
    cin >> start >> end;

    cout << "Prime numbers: ";
    while (start <= end) {
        if (isPrime(start)) {
            cout << start << " ";
        }
        start++;
    }

    return 0;
}

🔹 Output

Enter range (start end): 10 20
Prime numbers: 11 13 17 19

6️⃣ Find Prime or Composite

📌 Check if a number is prime or composite.

#include <iostream>
using namespace std;

int main() {
    int num, i = 2, isPrime = 1;
    cout << "Enter a number: ";
    cin >> num;

    if (num <= 1) {
        cout << num << " is neither prime nor composite.";
        return 0;
    }

    while (i * i <= num) {
        if (num % i == 0) {
            isPrime = 0;
            break;
        }
        i++;
    }

    if (isPrime)
        cout << num << " is a Prime number.";
    else
        cout << num << " is a Composite number.";

    return 0;
}

🔹 Output

Enter a number: 7
7 is a Prime number.

7️⃣ Printing Numbers using While Loop

📌 Print numbers from 1 to N.

#include <iostream>
using namespace std;

int main() {
    int n, i = 1;
    cout << "Enter a number: ";
    cin >> n;

    while (i <= n) {
        cout << i << " ";
        i++;
    }
    return 0;
}

🔹 Output

Enter a number: 5
1 2 3 4 5

8️⃣ Tables using While Loop

📌 Print the multiplication table of a number.

#include <iostream>
using namespace std;

int main() {
    int num, i = 1;
    cout << "Enter a number: ";
    cin >> num;

    while (i <= 10) {
        cout << num << " x " << i << " = " << num * i << endl;
        i++;
    }
    return 0;
}

🔹 Output (if input is 3)

3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30

9️⃣ Reverse Tables using While Loop

📌 Print the reverse multiplication table.

#include <iostream>
using namespace std;

int main() {
    int num, i = 10;
    cout << "Enter a number: ";
    cin >> num;

    while (i >= 1) {
        cout << num << " x " << i << " = " << num * i << endl;
        i--;
    }
    return 0;
}

🔹 Output (if input is 3)

3 x 10 = 30
3 x 9 = 27
...
3 x 1 = 3

📌 Summary

while loops run as long as the condition is true.
break exits the loop.
continue skips the current iteration.

Would you like more real-world examples? 🚀

Post a Comment

0 Comments