Showing posts with label CONDITIONAL [IF ELSE]. Show all posts
Showing posts with label CONDITIONAL [IF ELSE]. Show all posts

C++ Program to calculate factorial of a number using goto and Labels [DEVCPP/GCC]

STEPS

To calculate factorial of a number,

1. Initialize a variable (fact) with 1.
                     fact=1;

2. Multiply fact with given number(num).
                 fact = fact * num;

3. Decrement num by 1.
                      num--;

4. Repeat steps 2-3 until num>0.

PROGRAM

//Program to calculate factorial of a number using goto and Labels

#include <iostream>

using namespace std;

int main()
{
        int num ,n ;
        long long fact=1;

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

        n=num;

        Top:                        //Label Top

                  if(num>0)
                 {
                        fact = fact * num ;
                        num--;
                        goto Top;            // Switches control to Label Top
                 }

        cout<<"FACTORIAL OF "<<n<<" IS "<<fact;

        return 0;
}

OUTPUT

C++ Program to calculate factorial of a number using goto and Labels with output

C++ Program for Mini Calculator [DEVCPP/GCC]

PROGRAM

//A simple program for Mini Calculator using do-while loop and conditional statements.

# include <iostream>

using namespace std;

int main()
{
int op1,op2,res=0;
char ch,con;

do
{

cout<<"\nENTER TWO NUMBERS:\n";
cin>>op1>>op2;

cout<<"ENTER OPERATOR(+,-,*,/,%):\n";
cin>>ch;

switch(ch)
{
case '+':
res=op1+op2;
break;

case '-':
res=op1-op2;
break;

case '*':
res=op1*op2;
break;

case '/':
 
                           if(!op2)                // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
res=op1/op2;
 
  break;

case '%':
 
                           if(!op2)               // True when op2 is zero
cout<<"DIVIDE BY ZERO ERROR!!!\n";
else
{
                                  int q,r;
q=op1/op2;
r=op1-(op2*q);
res=r;
}

break;

default :
cout<<"WRONG OPERATOR\n";
}

cout<<"THE CALCULATED RESULT IS:"<<res<<"\n\n";

cout<<"DO YOU WISH TO CONTINUE:";
cin>>con;
 
}while(con=='Y'|| con=='y');

return 0;
}

OUTPUT

C++ Program for Mini Calculator with output

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 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 find the maximum of three numbers [DEVCPP/GCC]


//Program to find maximum among three numbers

#include <iostream>

using namespace std;

int main()
{
        float a,b,c,max;

        cout<<"ENTER FIRST NUMBER: ";
        cin>>a;
        cout<<"ENTER SECOND NUMBER: ";
        cin>>b;
        cout<<"ENTER THIRD NUMBER: ";
        cin>>c;

        if(a>b)            //Comparing first two numbers (a & b)
max=a;
else
max=b;

if(c>max)      //Comparing the max of above two numbers from the third number
max=c;


        cout<<"\nMAXIMUM AMONG "<<a<<","<<b<<","<<c<<" IS:"<<max;

        return 0;
}



C++ Program to find maximum among two numbers [DEVCPP/GCC]


//Program to find maximum among two numbers

#include <iostream>

using namespace std;

int main()
{
float a,b,max;

cout<<"ENTER FIRST NUMBER: ";
cin>>a;
cout<<"ENTER SECOND NUMBER: ";
cin>>b;

max = (a>b) ? a : b ;
// Variable =  <condition>  ?  <True>  :  <False> 

cout<<"\nMAXIMUM AMONG "<<a<<" & "<<b<<" IS "<<max;

return 0;
}


C++ Program to check whether given character is vowel or consonant [DEVCPP/GCC]

PROGRAM

//Program to check whether given character is vowel or consonant

#include <iostream>

using namespace std;

int main()
{
      char ch;

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

      // Checking for Upper Case Vowel

      if(ch>=65 && ch<=90)
      {
             if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
             {
                        cout<<ch<<" IS A VOWEL";
              }
              else
              {
                        cout<<ch<<" IS A CONSONANT";
              }
      }

      // Checking for  Lower Case Vowel
   
      else if(ch>=97 && ch<=122)
      {
              if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' )
              {
                        cout<<ch<<" IS A VOWEL";
              }
              else
              {
                        cout<<ch<<" IS A CONSONANT";
              }
      }
      else
     {
               cout<<"\nPLEASE ENTER A CHARACTER! ";
      }

      return 0;
}

OUTPUT

C++ Program to check whether given character is vowel or consonant

C++ Program to check whether given character is vowel or consonant

FIND US ON FACEBOOK!