prowessapps.in Loop Programs in C++

#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

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

#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.

#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

#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 

#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

#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.

#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.

#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

#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-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved