C++ Program to find pairs in an array with given sum [DEVCPP/GCC]

We intend to find pairs in an array having the sum of its elements equal to the provided number.
For instance, if an array consists of elements {0,2,6,4,8,10} and the provided number is 8, then we must find pairs having sum of its elements as 8 i.e. {0,8} and {2,6}.

PROGRAM

// Program to find pairs in an array having sum equal to a given number

#include <iostream>

using namespace std;

void findPairs(int a[], int n, int sum)
{
      int count=0;

      for(int i=0 ; i<n ; i++)
      {
             for(int j=i+1 ; j<n ; j++)
             {
                     if(a[i]+a[j]==sum)
                     {
                              count++;
                              cout<<"\nPAIR FOUND AT INDICES "<<i<<","<<j;
                     }
             }
       }

       cout<<"\n\nTOTAL PAIRS HAVING SUM AS "<<sum<<" ARE "<<count;
}

int main()
{
       int a[5],sum;

       cout<<"ENTER ARRAY OF ELEMENTS: ";

       for(int i=0 ; i<5 ; i++)
       {
                cin>>a[i];
       }

        cout<<"\nENTER SUM: ";
        cin>>sum;

        findPairs(a,5,sum);
}

OUTPUT

C++ Program to find pairs in an array with given sum

C++ Program to obtain sum of digits of a given number using recursion [DEVCPP/GCC]

PROGRAM

#include <iostream>

using namespace std;

int sumOfDigits(int n)
{
        if(n==0)
        {
              return 0;
        }
        else
        {
              return (n%10) + sumOfDigits(n/10);
        }
}

int main()
{
        int num;

        cout<<"\nENTER NUMBER: ";
        cin>>num;

        cout<<"\nSUM OF DIGITS: "<<sumOfDigits(num);

        return 0;
}

OUTPUT

C++ Program to obtain sum of digits of a given number using recursion with output

C++ Program to illustrate use of switch case without break [DEVCPP/GCC]

SWITCH

A switch statement allows an integer variable to be tested for equality against a list of values. These values are provided through cases. The basic syntax of switch is given below:
switch(expression)
{
   case constant-expression  :
      statement(s);
      break;      //Optional
  
   case constant-expression  :
      statement(s);
      break;      //Optional
  
   default :      //Optional
      statement(s);
}
PROGRAM

// Program to illustrate use of switch case without break

#include <iostream>

using namespace std;

int main()
{
switch(2017)
{
case 2017:
{
cout<<"\nMay you get: \n";
}
case 1:
{
cout<<"\n\tHappiness";
}
case 2:
{
cout<<"\n\tJoy";
}
case 3:
{
cout<<"\n\tProsperity";
}
case 4:
{
cout<<"\n\tLove";
}
case 5:
{
cout<<"\n\tCare";
}
case 6:
{
cout<<"\n\tMoney";
}
case 7:
{
cout<<"\n\tSupport";
}
case 8:
{
cout<<"\n\tRespect";
}
case 9:
{
cout<<"\n\tBlessings";
}
case 10:
{
cout<<"\n\tHealth";
}
case 11:
{
cout<<"\n\tMotivation";
}
case 12:
{
cout<<"\n\tGuidance";
}
default:
cout<<"\n\nThis year!";
cout<<"\n\nHappy New Year Guys!";
}
}

OUTPUT

C++ Program to illustrate use of switch case without break

FIND US ON FACEBOOK!