C++ Program to check whether a number is Armstrong number or not [DEVCPP/GCC]


ARMSTRONG NUMBER

A number of m digits is said to be an Armstrong number, if the sum of its each digit raise to the power m results in the original number.

For example,
5^1=5
(1^3) + (5^3) + (3^3) = 153
(8^4) + (2^4) + (0^4) + (8^4) = 8208

PROGRAM

// Program to determine whether given number is Armstrong Number or not

#include <iostream>

using namespace std;

int main()
{
int num ,n, sum=0, temp,count=0, power;

cout<<"ENTER NUMBER: ";
cin>>n;

num = n;

//To count number of digits

while(num>0)
{
count++;
num=num /10;
}

         //Reassigning n to num
 
num = n;


while(num>0)
{
temp = num % 10;    //Obtain the last digit of num

// Calculate power of obtained digit(temp)

power=1;
for(int i=0;i<count;i++)
{
power=power*temp;
}

sum = sum + power; //Rebuilding number
num = num /10; //To remove the last digit
}

if(sum==n)
{
cout<<n<<" IS AN ARMSTRONG NUMBER";
}
else
{
cout<<n<<" IS NOT AN ARMSTRONG NUMBER";
}

return 0;
}

OUTPUT

C++ Program to check whether a number is Armstrong number or not

C++ Program to display the size of different Datatypes [DEVCPP/GCC]


SIZEOF OPERATOR 

  • This compile time operator is used to determine the size of variable/data type in bytes. 
  • Sizeof operator can also be used to determine size of user defined variables i.e. class, structures etc.
  • Nesting of sizeof operator is possible .  
PROGRAM

//Program to display the size of different Datatypes

#include <iostream>

using namespace std;

int main()
{

cout<<"\n SIZE OF DIFFERENT DATATYPES \n";

cout<<"\n CHAR DATATYPE:        "<< sizeof(char);
cout<<"\n BOOL DATATYPE:        "<< sizeof(bool);
cout<<"\n WIDE CHARACTER:       "<< sizeof(wchar_t);

cout<<"\n SHORT DATATYPE:       "<< sizeof(short);
cout<<"\n INT DATATYPE:         "<< sizeof(int);
cout<<"\n LONG DATATYPE:        "<< sizeof(long);

cout<<"\n FLOAT DATATYPE:       "<< sizeof(float);
cout<<"\n DOUBLE DATATYPE:      "<< sizeof(double);
cout<<"\n LONG DOUBLE DATATYPE: "<< sizeof(long double);

return 0;
}

OUTPUT


C++ Program to demonstrate Union type [DEVCPP/GCC]


UNION
  • Union is a user defined variable. 
  • It consists of more than one non-static data members.
  • Union uses a single memory location to hold value of only one of its data members at a time. 
  • All data members refer to the same location in memory, thus modification of one of the members will affect the value of all of them.
Union can be considered as a container which can contain only one value at a time. The size of union is the size of its greatest data member. For example, if a union consists of integer, double and float data members then the size of union will be the size of double (being the greatest among all members of structure).

PROGRAM

//Program to demonstrate Union type

#include <iostream>

using namespace std;

int main() 
{
union weight //Definition
{
               long   long_weight;
double   double_weight;
};
union weight w; //Declaration  
cout<<"SIZE OF UNION WEIGHT: "<<sizeof(w)<<endl;
/* Size of union weight will be the size of double datatype as its size is greater than long datatype */
w.long_weight=40000;   //w=500.55 Overwritten
cout<<"VALUE IN CONTAINER W: "<<w.long_weight<<endl;

w.double_weight=500.55;   //w=100 Overwritten
cout<<"VALUE IN CONTAINER W: "<<w.double_weight;
return 0;
}

OUTPUT

C++ Program to demonstrate Union type with output

C++ Program to change Foreground Colour of Console Screen(Windows) [DEVCPP/GCC]

PROGRAM

//Program to change Foreground Colour of Console Screen (Windows)

#include <iostream>

using namespace std;

int main()
{
int choice;

cout<<"ENTER NUMBER(0-8): ";
cin>>choice;

switch(choice)
{
case 1: //Blue
system("COLOR 1");
break;

case 2: //Green
system("COLOR 2");
break;

case 3: //Aqua
system("COLOR 3");
break;

case 4: //Red
system("COLOR 4");
break;

case 5: //Purple
system("COLOR 5");
break;

case 6: //Yellow
system("COLOR 6");
break;

default:
cout<<"WRONG CHOICE";

}

cout<<"\n\t\t\tHAPPY HOLI";
return 0;
}

OUTPUT


C++ Program to demonstrate String datatype [DEVCPP/GCC]


STRING DATATYPE

In C++, strings are not a built-in data type, but rather a Standard Library facility. The standard C++ library provides a string class type that supports all the operations possible through character array and additional functionality.

PROGRAM

//Program to initialize string and display it

#include <iostream>
#include <string>

using namespace std;

int main()
{
string  s1("TECH"); //Through Constructor

string  s2;
cout<<"ENTER STRING: ";
cin>>s2; //Through Input From User

string  s3(5,'+'); //Through Parameterized Constructor

cout<< "\n STRING 1: "<<s1;
cout<< "\n STRING 2: "<<s2;
cout<< "\n STRING 3: "<<s3;

return 0;
}

OUTPUT

C++ Program to demonstrate String datatype with output

C++ Program to demonstrate use of continue statement [DEVCPP/GCC]


CONTINUE STATEMENT

Whenever a continue statement is encountered inside a loop, the rest of statements inside loop’s body for the current iteration are skipped and the control is transferred to the beginning of the loop for next iteration.

PROGRAM

//Program to demonstrate working of continue statement by displaying integers from 1 to 10 except 7   and 9

#include<iostream>

using namespace std;

int main()
{
for( int i=1 ; i<=10 ; i++ )
{
if(i==7 || i==9)
{
continue;
}
cout<<i<<" ";
}
return 0;
}

OUTPUT

C++ Program to demonstrate use of continue statement


C++ Program to display Hexadecimal and Octal Equivalent of a given number [DEVCPP/GCC]

PROGRAM

//Program to display Hexadecimal and Octal Equivalent of a given number 

#include <iostream>

using namespace std;

int main()
{

int num;
cout << "ENTER NUMBER: ";
cin >> num;

cout<<"\n GIVEN NUMBER: "<< num;

cout << "\n HEXADECIMAL EQUIVALENT: "<< hex << num ;

cout << "\n OCTAL EQUIVALENT: "<< oct << num ;

return 0;
}

OUTPUT

C++ Program to display Hexadecimal and Octal Equivalent of a given number

C++ Program to perform Left Shift Operation [DEVCPP/GCC]


LEFT SHIFT OPERATOR  (<<)

The left shift operator shifts the data in left direction by a specified number of bits. New bits coming in from the right are zeros.
Shifting left by n places is the same as multiplying by 2^n. Moreover, Shifting is faster than multiplication.

For instance, i = j << 2 i.e. Multiply j by 4 (2^2)

PROGRAM

//Program to perform Left Shift Operation

#include <iostream>

using namespace std;

int main()
{
int x,res;

cout<<"ENTER X: ";
cin>>x;

res = x<<1; //res=x*(2^1)
cout<<x<<" *  2= "<<res<<endl ;

res = x<<2; //res=x*(2^2)
cout<<x<<" *  4= "<<res<<endl ;

res = x<<3; //res=x*(2^3)
cout<<x<<" *  8= "<<res<<endl ;

res = x<<4; //res=x*(2^4)
cout<<x<<" * 16= "<<res<<endl ;

return 0;
}

OUTPUT

C++ Program to perform Left Shift Operation

C++ Program to find Greatest common Factor (GCF) and Least Common Multiple (LCM) of a number [DEVCPP/GCC]


GREATEST COMMON FACTOR (GCF)

It is also known as Greatest Common Divisor and Highest Common Factor.
The GCF of numbers X and Y is the biggest number that will divide both X and Y,
GCF is the product of all the factors that X and Y have in common.
For example,

X=20= 2*2*5
Y=30=2*3*5

GCF=2*5=10

LEAST COMMON MULTIPLE (LCM)

The LCM of X and Y is the smallest number (non zero) that is a multiple of both X and Y.

For example,

X=20=2*2*5
Y=30=2*3*5

LCM=60

PROGRAM
  
//Program to find GCF and LCM of a number

#include<iostream>

using namespace std;

int main()
{
        int a, b, x, y ,t, gcd, lcm;

        cout<<"ENTER TWO INTEGERS:\n";
        cin>>x>>y;

        a=x;
        b=y;

        while (b != 0)
        {
                 t = b;
                 b = a % b;
                 a = t;
        }

        gcd = a;
        lcm = (x*y)/gcd;

        cout<<"GREATEST COMMON FACTOR OF "<<x<<" AND "<<y<<" IS:"<<gcd<<endl;
        cout<<"LEAST COMMON MULTIPLE OF "<<x<<" AND "<<y<<" IS:"<<lcm;

        return 0;
}

OUTPUT

C++ Program to find GCF and LCM of a number

C++ Program to determine the type of character and its ASCII value [DEVCPP/GCC]

ASCII VALUES

The ASCII values of Digits and Alphabets are in a particular sequence i.e.

Digits(0-9)                                =  48 to 57
Uppercase Alphabets(A-Z)      =  65 to 90
Lowercase Alphabets(a-z)       =  97 to 122

PROGRAM

//Program to determine the type of character and its ASCII value

#include <iostream>

using namespace std;

int main()
{
int n;
char ch;

cout<<"ENTER CHARACTER: ";
cin>>ch;

n=(int)ch;               //Type Casting into Integer

if( n>=48 && n<=57 )
{
cout<<ch<<" IS A DIGIT\n";
}
else if( n>=65 && n<=90 )
{
cout<<ch<<" IS A UPPERCASE ALPHABET\n";
}
else if( n>=97 && n<=122 )
{
cout<<ch<<" IS A LOWERCASE ALPHABET\n";
}

cout<<"ASCII VALUE: "<<n;
return 0;
}

OUTPUT


C++ Program to print a string multiple times using goto and labels [DEVCPP/GCC]


GOTO STATEMENT

The goto statement is used to transfer program control to some other part of the program without specifying any condition.

PROGRAM

//Program to print a string multiple times using goto and labels

#include <iostream>

using namespace std;

int main()
{
int a=0;

print:

if(a==10)
{
goto end;        //Transfers control to label end
}
else
{
a++;
cout<<"TECHCPP \n";
}

goto print;              //Transfers control to label print

end:
return 100;
}

OUTPUT

C++ Program to print a string multiple times using goto and labels

C++ Program to check whether the number is even or odd using bitwise AND (&) operator [DEVCPP/GCC]


BITWISE AND (&) OPERATOR

Bitwise operators work on binary digits. The bitwise AND operator performs logical AND operation on pair of bits. Consider two numbers 2 and 1, then 2 & 1 will result in 0.
       
        2:     010
        1:     001
              --------
                000

Explanation:
 
              0 & 1 = 0
              1 & 0 = 0
              0 & 0 = 0

Now, we can apply this for determining whether a given number is even or odd. The bitwise AND operation of any even number with 1 will always result in 0.

 // Program to check whether the number is even or odd using Bitwise AND (&) operator

#include<iostream>

using namespace std;

int main()
{

int num;

cout<<"ENTER THE NUMBER TO BE CHECKED:\n";
cin>>num;

if((num & 1)== 0 )             // Using Bitwise AND (&) operator
{
cout<<"NUMBER IS EVEN";
}
  else
{
cout<<"NUMBER IS ODD";
       }
 
return 0;
}


C++ Program to check whether the number is even or odd using division operator [DEVCPP/GCC]


The division of odd numbers with 2 will always result in a fractional value. For example, 25/2=12.50. The fractional part of the result is ignored by the compiler in case of integer datatype. Hence on multiplying the result with 2, we would not get the original number.

For instance,
55/2 = 27.5 = 27
27*2 = 54

Here, we get 54 after calculation which signifies 55 is an odd number.

// Program to check whether the number is even or odd using division operator


#include<iostream>

using namespace std;

int main()
{

int num;

cout<<"ENTER THE NUMBER TO BE CHECKED:\n";
cin>>num;

if( (num/2) * 2  == num)       // Using division operator
{
cout<<"NUMBER IS EVEN\n";
}
  else
{
cout<<"NUMBER IS ODD";
     }
 
return 0;
}



C++ Program to demonstrate use of Binary, Octal and Hexadecimal numbers as Integer literals [DEVCPP/GCC]


In C++, we can use Binary numbers, Octal numbers and Hexadecimal numbers as Integer literal.

For declaring an Binary Integer literal, the Binary digits are placed after 0b or 0B.
eg. <0b>101 i.e.  Binary (101)= Decimal (5)

For declaring an Octal Integer literal, the Octal digits are placed after 0.
eg. <0>112 i.e.  Octal(112)= Decimal (156)

For declaring a Hexadecimal Integer literal, the Hexadecimal digits are placed after 0x or 0X.
eg. <0x>12 i.e.  Hexadecimal(12)= Decimal (18)


//Program to demonstrate use of Binary, Octal and Hexadecimal numbers as Integer literals 

#include <iostream>

using namespace std;

int main()
{
int a = 0106;                  // Octal(106)= Decimal(70)
cout<< a << endl;

int b = 0x46;                 // Hexadecimal(46)=Decimal(70)
cout<< b << endl;

int c = 0B1000110;      // Binary(1000110) = Decimal(70)
cout<< c;

return 0;
}


FIND US ON FACEBOOK!