Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Number Varieties and Crunches
Concept Learning Code Sheets

Checking for Armstrong Number (3 Digit) #1539

Program takes integer input and finds if given number is an armstrong number

#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a three digit positive integer: ";
cin >> n;
int n1,rem, num=0;
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(n==num)
cout << n << " is an armstrong number.";
else
cout << n << " is not an armstrong number.";
return 0;
}

Armstrong number n digit #1573

Checking for armstrong number which can be even more than 3 digit.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long n;
int digits=0;
cout << "Enter a positive integer: ";
cin >> n;
long long n1,num=0;
int rem;
n1=n;
while (n1 != 0)
{
n1 /= 10;
++digits;
}
n1=n;
while(n1 != 0)
{
rem=n1%10;
num+=pow(rem,digits);
n1/=10;
}
if(n==num)
cout << n << " is an armstrong number.";
else
cout << n << " is not an armstrong number.";
return 0;
}

Check if number is a palindrome #1575

To check if given number is same even if reversed (Palindrome).

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer: ";
cin>>n;
int rem,nr=0;
int nc=n;
while(nc>0)
{
rem=nc%10;
nr=nr*10+rem;
nc=nc/10;
}
if(n==nr) cout<<"Palindrome";
else cout<<"Not Palindrome";
cout<<endl;
return 0;
}

Reversing an integer #1579

Printing a positive integer in reverse form. for e.g. printing 123 as 321.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer: ";
cin>>n;
int rem,nr=0;
int nc=n;
while(nc>0)
{
rem=nc%10;
nr=nr*10+rem;
nc=nc/10;
}
cout<<n
<<" can be written in reverse as "
<<nr<<endl;
return 0;
}

Check for perfect number #1582

Check if the sum of all the divisors of a number brings the number itself.

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

All factors (divisors) of a number #1584

Printing all factors or divisors of an integer number.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer: ";
cin>>n;
for(int i=1;i<=n/2;i++)
if(n%i==0) cout<<i<<" ";
cout<<n<<endl;
return 0;
}

Check for prime number (1 and n exclusion technique) #1587

Check if a number has only two factors 1 and the number itself. The 1 and n exclusion technique does not test division with these two obvious divisors and does it for all other possible divisors.

#include <iostream>
using namespace std;
int main()
{
int n,dc=0;
cout<<"Enter a positive integer: ";
cin>>n;
for(int i=2;i<=n/2;i++)
if(n%i==0) dc++;
if(dc>0 || n<2) cout<<"Not Prime";
else cout<<"Prime";
cout<<endl;
return 0;
}

Extracting and printing the first and last digit of a number #2119

Using a loop technique and successive division by 10 to extract first and last digit of a given number and then printing it.

#include<iostream>
using namespace std;
int main()
{
int n,digit;
cout<<"Give a positive integer: ";
cin>>n;
int nc=n;
int last_digit=nc%10;
while(nc>=10)
{
nc=nc/10;
}
int first_digit=nc;
cout<<"First Digit = "<<first_digit<<endl;
cout<<"Last Digit = "<<last_digit<<endl;
return 0 ;
}

Finding GCD/HCF of two integer numbers #2871

Program to find Highest Common Factor (HCF), also called the Greatest Common Divisor between the two numbers entered by the user.

#include <iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter two numbers separated by space: ";
cin>>x>>y;
while(x != y)
{
if(x>y) x=x-y;
else y=y-x;
}
cout<<"GCD/HCF = "<< x;
return 0;
}

Finding LCM/SCM of two integer numbers #2874

Program to find the Least Common Multiple (LCM), also called the smallest common multiple between the two numbers entered by the user.

#include <iostream.h>
using namespace std;
int main()
{
int x,y;
cout<<"Enter two numbers separated by space: ";
cin>>x>>y;
int a=x,b=y;
while(a != b)
{
if(a>b) a=a-b;
else b=b-a;
}
cout<<"LCM/SCM of "<<x<<" & "<<y
<<" = "<< x*y/a;
return 0;
}

Check if Perfect Square #2884

A program to check if the given integer by the user is a perfect square of some integer number.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n,f=0;
cout<<"Enter a positive integer: ";
cin>>n;
int s=round(sqrt(n));
if(n==s*s || n==(s+1)*(s+1)) f=1;
if(f) cout<<"Perfect Square"<<endl;
else cout<<"Not a Perfect Square "<<endl;
return 0;
}

Check if Perfect Square (double to int conversion method) #2888

A program to check if the given integer by the user is a perfect square of some integer number by using the square root of double to int conversion and then comparison method.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long long n;
int f=0;
cout<<"Enter a positive integer: ";
cin>>n;
cout<<"n = "<<n<<endl;
double dsr = sqrt(n);
cout<<"dsr = "<<dsr<<endl;
long long isr = dsr;
cout<<"isr = "<<isr<<endl;
if ( dsr == isr ) f=1;
if(f) cout<<"Perfect Square"<<endl;
else cout<<"Not a Perfect Square "<<endl;
return 0;
}

Check if Triangular Number (successive summation method) #2892

Find if the given number is a triangular number by successive summation of natural numbers until it matches the given number.

#include <iostream>
using namespace std;
int main()
{
int n,f=0,sum=0;
cout<<"Enter a positive integer: ";
cin>>n;
int c=1;
while(sum<n)
{
sum=0;
for(int i=1;i<=c;++i)
{
sum+=i;
}
if(sum==n) {f=1;break;}
c++;
}
if(f) cout<<"Triangle number"<<endl;
else cout<<"Not a triangle number "<<endl;
return 0;
}

Check if Triangular Number (perfect square testing – without sqrt) #2897

Find if the given number is a triangular number by finding if the 8*n+1 is a perfect square without using the sqrt function of the math library

#include <iostream>
using namespace std;
int main()
{
int n,f=0;
cout<<"Enter a positive integer: ";
cin>>n;
int t=8*n+1;
for (int i = 0; i < t / 2 + 2; i++)
if (i * i == t) f=1;
if(f) cout<<"Triangle number"<<endl;
else cout<<"Not a triangle number "<<endl;
return 0;
}

Check if Triangular Number (perfect square testing – using rounded sqrt) #2900

Find if the given number is a triangular number by finding if the 8*n+1 is a perfect square by using the rounded square root.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n,f=0;
cout<<"Enter a positive integer: ";
cin>>n;
int t=8*n+1;
int s=round(sqrt(t));
if(t==s*s || t==(s+1)*(s+1)) f=1;
if(f) cout<<"Triangle number"<<endl;
else cout<<"Not a triangle number "<<endl;
return 0;
}

Check if Triangular Number (perfect square testing – double to int) #2907

Find if the given number is a triangular number by finding if the 8*n+1 is a perfect square by using the double to int conversion.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n,f=0;
cout<<"Enter a positive integer: ";
cin>>n;
int t=8*n+1;
double dsr = sqrt(t);
long isr = dsr;
if ( dsr == isr ) f=1;
if(f) cout<<"Triangle number"<<endl;
else cout<<"Not a triangle number "<<endl;
return 0;
}

Checking for buzz number #3176

Test if a number is divisible by 7 or the last digit is 7, means whether the number is a buzz number.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a number: ";
cin>>n;
if(n%7 == 0 || n % 10 == 7)
cout << n << " is a buzz number "<<endl;
else
cout << n<< " is not a buzz number "<<endl;
return 0;
}

Find prime factors of a number #3218

Finding prime factors of a number input by the user.

#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a positive integer: ";
cin>>n;
cout<<"The prime factors are: ";
for (int i=2; i <= n; i++)
{
while(n % i == 0)
{
n /= i;
cout<<i<<" ";
}
}
return 0;
}

Solved Problems

Palindrome numbers between 1 to 1000 #1553

Print all palindrome numbers between 1 to 1000.

Armstrong numbers between 1 to 999 #1566

Print all the armstrong numbers between 1 to 999.

Test palindrome for an integer number #3082

Write a program where a function of prototype int ispalin(int) is written, wh

function to reverse an integer #3086

Write a program to reverse the given integer number.using a separately called

Check if number is a palindrome without using modulus operator #3581

Write a C++ program to collect a positive integer from the user and the check

Error finding in function and related code #4447

Find syntactical errors in the following program and rewrite the program afte


Code Sheets:18  Solved Problems:6 
Back