Loop Programs in C++

#### 1. Write a C++ program to calculate the sum of numbers from 1 to 10

```#include< iostream>
using namespace std;
int main() {
int sum =0;
for(int i =0;i<=10;i++) {
sum = sum+i;
}
cout<<"Sum = "<< sum;
return 0;
}
```
OUTPUT
`Sum = 55`

#### 2. Write a C++ program to find the factorial of a given number

```include< iostream>
us#ing namespace std;
int main() {
int n, res = 1;
cout<<"Enter a number ";
cin>>n;
for(int i=n; i>=2;i--) {
res = res*i;
}
cout<<"Result = "<< res<< endl;
return 0;
}

```
OUTPUT
```Enter a number 5
Result = 120
```

#### 3. Write a C++ program to check the given number is prime or not

```#include< iostream>
#include< math.h>
using namespace std;
int main() {
int n;
int flag = 0;
cout<<"Enter Value for N: ";
cin>>n;
for(int i =2; i<=sqrt(n);i++) {
if(n%i==0) {
flag = 1;
break;
}
}
if(flag==0) {
cout<< n<<" is a prime no. \n";
}
else {
cout<< n<<" is not a prime no. \n";
}
return 0;
}

```
OUTPUT
```Enter Value for N: 5
5 is a prime no.```

#### 4. Write a C++ program to count the digits in a given number

```#include< iostream>
using namespace std;
int main() {
int n;
cout<<"Enter the Value for N: ";
cin>>n;
int count = 0;
while(n>0) {
n = n/10;
count++;
}
cout<<"Total Digits = "<< count;

return 0;
}
```
OUTPUT
```Enter the Value for N: 12345
Total Digits = 5```

#### 5. Write a C++ program to check the given number is PALINDROME or NOT

```#include< iostream>
using namespace std;
int main() {
int n,orig,rev = 0;
cout<<"Enter Value for N: ";
cin>>n;
orig = n;
while(n>0) {
int temp = n%10;
rev = 10*rev+temp;
n = n/10;
}
if(orig==rev) {
cout<< orig<<" is Palindrome\n";
}
else {
cout<< orig<<" is not Palindrome\n";
}
return 0;
}
```
OUTPUT
```Enter Value for N: 12345
12345 is not Palindrome ```

#### 6. Write a C++ program to find x to the power y(xy ) using loop

```#include< iostream>
using namespace std;
int main() {
int n,p,pow = 1;
cout<<"Enter Base: ";
cin>>n;
cout<<"Enter Power: ";
cin>>p;
for(int i=1;i<=p;i++) {
pow = pow * n;
}
cout<<"Result = "<< pow<< endl;
return 0;
}
```
OUTPUT
```Enter Base: 4
Enter Power: 3
Result = 64
```

#### 7. Write a C++ program to check a given number is armstrong or not

```#include< iostream>
#include< math.h>
using namespace std;
int main() {
int n, temp, rem, p;
int sum =0, digits =0;
cout<<"Enter a Number : ";
cin>>n;
temp = n;
while(temp !=0) {
digits++;
temp = temp/10;
}
temp = n;
while(temp!=0) {
rem = temp%10;
p = pow(rem,digits);
sum = sum+p;
temp = temp/10;
}
if(n==sum)
cout<< n<<" is armstrong no.";
else
cout<< n<<" is not armstrong no.";
return 0;
}
```
OUTPUT
```Enter a Number : 123
123 is not armstrong no.```

#### 8. Write a C++ program to check a given number is perfect or not

```#include< iostream>
using namespace std;
int main() {
int n, i, sum=0;
cout<<"Enter a number : ";
cin>>n;
for(i = 1; i <= n/2; i++) {
if(n%i ==0) {
sum = sum+i;
}
}
if(sum==n)
cout<< n<<" is a perfect no.";
else
cout<< n<<" is not a perfect no.";
return 0;
}

```
OUTPUT
```Enter a number : 6
6 is a perfect no.
```

#### 9. Write a C++ program to calculate hcf and lcm of two numbers

```#include< iostream>
using namespace std;
int main() {
int a, b, gr, lcm, low, temp;
cout<<"Enter two Number : ";
cin>>a>>b;
gr =(a >= b)?a:b;
low =(a < b)?a:b;
while(low!=0) {
temp = gr % low;
gr = low;
low = temp;
}
cout<<"HCF = "<< gr<< endl;
lcm = (a*b)/gr;
cout<<"LCM = "<< lcm<< endl;
return 0;
}

```
OUTPUT
```Enter two Number : 6 10
HCF = 2
LCM = 30
```

#### 10. Write a C++ program to convert decimal to binary number

```#include< iostream>
using namespace std;
int main() {
int n, k, c;
cout<<"Enter a Number : ";
cin>>n;
for(c = 15; c>=0; c--) {
k = n>>c;
if(k & 1)
cout<<"1";
else
cout<<"0";
}
cout<< endl;
return 0;
}
```
OUTPUT
```Enter a Number : 5
0000000000000101
```

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824