0% found this document useful (0 votes)
3 views6 pages

C++ Program Using While Lopp

The document contains a series of C++ programs that demonstrate basic programming concepts such as printing numbers, calculating sums, finding factorials, checking for prime numbers, and more. Each program uses a while loop to perform its specific task, including operations like reversing numbers, counting digits, and generating Fibonacci series. Additionally, there are examples of pattern printing and calculating GCD, Armstrong numbers, and multiplication tables.

Uploaded by

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

C++ Program Using While Lopp

The document contains a series of C++ programs that demonstrate basic programming concepts such as printing numbers, calculating sums, finding factorials, checking for prime numbers, and more. Each program uses a while loop to perform its specific task, including operations like reversing numbers, counting digits, and generating Fibonacci series. Additionally, there are examples of pattern printing and calculating GCD, Armstrong numbers, and multiplication tables.

Uploaded by

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

1.

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;
}

2. Sum of N Natural Numbers


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

cout << "Enter a number: ";


cin >> n;

while (i <= n) {
sum += i;
i++;
}

cout << "Sum = " << sum;

return 0;
}

3. Factorial of a Number
#include <iostream>
using namespace std;
int main() {
int n, i = 1, fact = 1;
cout << "Enter a number: ";
cin >> n;
while (i <= n) {
fact *= i;
i++;
}

cout << "Factorial = " << fact;


return 0;
}

4. Reverse Number Printing


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
while (n >= 1) {
cout << n << " ";
n--;
}
return 0;
}

5. Check Prime Number


#include <iostream>
using namespace std;
int main() {
int num, i = 2, isPrime = 1;
cout << "Enter a number: ";
cin >> num;
while (i <= num / 2) {
if (num % i == 0) {
isPrime = 0;
break;
}
i++;
}
if (num <= 1)
cout << "Not a prime number.";
else if (isPrime)
cout << num << " is a prime number.";
else
cout << num << " is not a prime number.";

return 0;
}

6. Sum of Digits
#include <iostream>
using namespace std;
int main() {
int num, sum = 0;

cout << "Enter a number: ";


cin >> num;
while (num > 0) {
sum += num % 10;
num /= 10;
}

cout << "Sum of digits = " << sum;

return 0;
}

7. Reverse a Number
#include <iostream>
using namespace std;
int main() {
int num, reverse = 0;
cout << "Enter a number: ";
cin >> num;
while (num > 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}
cout << "Reversed number = " << reverse;
return 0;
}

8. Print Star Pattern (Triangle)


#include <iostream>
using namespace std;
int main() {
int rows, i = 1;
cout << "Enter number of rows: ";
cin >> rows;
while (i <= rows) {
int j = 1;
while (j <= i) {
cout << "* ";
j++;
}
cout << endl;
i++;
}
return 0;
}

9. Count Number of Digits


#include <iostream>
using namespace std;
int main() {
int num, count = 0;
cout << "Enter a number: ";
cin >> num;
while (num != 0) {
num /= 10;
count++;
}
cout << "Number of digits: " << count;
return 0;
}

10. Find GCD of Two Numbers


#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
cout << "GCD is " << a;
return 0;
}
Fibonacci Series
C++#include <iostream>
using namespace std;

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

✅ 7. Count Digits
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
while (n != 0) {
count++;
n /= 10;
}
cout << "Digits: " << count;
return 0;
}

✅ 8. 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;
}

✅ 9. Armstrong Number (3-digit)


#include <iostream>
using namespace std;

int main() {
int n, temp, sum = 0;
cin >> n;
temp = n;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
cout << (sum == n ? "Armstrong" : "Not Armstrong");
return 0;
}

✅ 10. Pattern Printing (Triangle)


#include <iostream>
using namespace std;

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

You might also like