24/11/2015

C++ Program To Find The Address Of Variable
Problem :- Write A C++ Program To Find The Address Of Variable .

Logic :- We Can find the address of any variable by using ' & ' operator we can find address of variable in memory.
You can find address of variable syntax is given Below 

Syntax :-

cout << "Address Of First Variable :\n\n";
cout << &first <<"\n";

Try Yourself  Java Program For Calculate Simple Interest

Solution :-

#include <iostream>
using namespace std;

int main ()
{
   int  first,sec;
   
   cout<<"Enter The Value Of First and Second Variable \n\n";
   cin>>first>>sec;

   cout << "Address Of First Variable :\n\n";
   cout << &first <<"\n";

   cout << "\nAddress Of Second Variable :\n\n";
   cout << &sec << endl;

   return 0;
}

Output:-

C++ Program To Find The Address Of Variable

23/11/2015

Write A C++ Program To Print  Perfact Square Of Program Using Sleep And Delay Function In Loop
1     2    3     4    5   6      7    8   9    10
36  37  38  39  40  41   42  43  44   11
35  64  65  66  67  68   69  70  45  12
34  63  84  85  86  87   88  71  46  13
33  62  83  96  97  98   89  72  47  14
32  61  82  95  100  99 90  73  48  15
31  60  81  94  93  92   91  74  49  16
30  59  80  79  78  77   76  75  50  17
29  58  57  56  55  54   53  52  51  18
28  27  26  25  24  23   22  21  20  19

#include<stdio.h>
#include<stdlib.h>
#include <dos.h>
int main()
{
    int a[10][10]={0},i,j,low=0,top=9,n=1;
    for(i=0;i<5;i++,low++,top--)
    {
        for(j=low;j<=top;j++,n++)
            a[i][j]=n;
        for(j=low+1;j<=top;j++,n++)
            a[j][top]=n;
        for(j=top-1;j>=low;j--,n++)
            a[top][j]=n;
        for(j=top-1;j>low;j--,n++)
            a[j][low]=n;
    }
    printf("\t\t\t\tPerfect Square\n");
    for(i=0;i<10;i++)
    {
        printf("\n\n\t");
        for(j=0;j<10;j++)
        {
            printf("%6d",a[i][j]);
            sleep(1);
        }
    }
    return 0;
}

Output:-

Here I Use A Sleep Function So Outputs Are Coming Every 1 Sec.











Write A C++ Program To Print Half Pyramid Alternative Using ( * ) Star And Alphabet
*
*A
*A*
*A*A
*A*A*
*A*A*A

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n;
cout<<"Enter the No Of Row\n";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0)
cout<<"A";
else
cout<<"*";
}
cout<<"\n";
}
return 0;
}

Output:-


Write A C++ Program To Print  INDIA Pattern As Given Below
I
I N
I N D
I N D I
I N D I A

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
char s[]="INDIA";
int i,j;
for(i=0;s[i];i++)
{
for(j=0;j<=i;j++)
cout<<s[j];
cout<<"\n";
}
return 0;
}

Output:-


Write A C++ Program To Print REVERSE Of Half Pyramid Using Alphabet
A B C D E F
   G H  I  J K
       L M N O
          P  Q R
              S T
                U

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n,k;
cout<<"Enter the No Of Row\n";
cin>>n;
char c='A';
    for(i=0;i<=n;i++)
    {
       for(j=0;j<=i;j++)
       {
    cout<<" ";
       }
       for(k=n-i-1;k>=0;k--)
       {
   cout<<c;
   c++;
       }
       cout<<"\n";
    }
    return 0;
}

Output:-


Write A C++ Program To Print Half Pyramid Using Alphabet
A
B C
D E F
G H I J
K L M N O

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n;
cout<<"Enter the No Of Row\n";
cin>>n;
char c='A';
   for(i=0;i<n;i++)
   {
   for(j=0;j<=i;j++)
   {
   if(c=='Z')
   break;
  cout<<c;
  c++;
   }
  cout<<"\n";
  }
  return 0;
}

Output:-


Write A C++ Program To Print Half Pyramid Using Similar  Number In Row
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n;
cout<<"Enter the No Of Row\n";
cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<i<<" ";
        }
        cout<<"\n";
    }

}

Output:-


Write A C++ Program To Print Half Pyramid Using Number
1
2 3
4 5 6
7 8 9 10

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n,k=1;
cout<<"Enter the No Of Row\n";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
cout<<k<<" ";
k++;
}
cout<<"\n";
}
return 0;
}

Output:-



Write A C++ Program To Print Reverse Half Pyramid Using *
******
*****
****
***
**
*

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n;
cout<<"Enter the No Of Row\n";
cin>>n;
 for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}

Output:-


Write A C++ Program To Print Half Pyramid Using *
*
**
***
****
*****
******

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,j,n;
cout<<"Enter the No Of Row\n";
cin>>n;
for(i=1; i<=n; i++)
{
for(j=1; j<i; j++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}

Output:-


C++ Program To  Print A Given Reverse STAR Pattern
Problem :- Write A C++ Program To  Print A Given Reverse STAR Pattern Using As-trick ( * ) .


                                      ***********
                                        *********
                                          *******
                                            *****
                                             ***
                                               *

Logic :- For Printing any pattern remember always use For loop .In mostly pattern two for loops require first loop for Row and second for Column .May Be in your pattern for loop can be use parallel or Nested according to pattern you have to use if you don't know about how to print pattern then i refer first print this pattern by Own
C++ Program To Print A Given Triangle of STARS

Solution :-

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav 
int i,n, j, k;

cout<<"Enter How Many Times You Want To Print Pattern\n";
cin>>n;

for(i=n;i>=1;i--)
{
for(j=n;j>i;j--)
{
cout<<" ";
}
for(k=1;k<(i*2);k++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}

Output:-

C++ Program To  Print A Given Reverse STAR Pattern

C++ Program To Print A Given Triangle of STARS
Problem :- C++ Program To Print A Given Triangle of STARS Using As-trick ( * )

                                *
                             * * *
                          * * * * *
                        * * * * * * *
                     * * * * * * * * *

Logic :- For Printing any pattern remember always use For loop .In mostly pattern two for loops require first loop for Row and second for Column .May Be in your pattern for loop can be use parallel or Nested according to pattern you have to use if you don't know about how to print pattern then i refer first print this pattern by Own  C++ Program To Print A Given Pattern Or Series Like 12345,5432,234,43,3

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,j,k,n;

cout<<"Enter How Many Row Print With Pattern\n";
cin>>n;

for(i=1; i<=n; i++)
{
for(j=n-1; j>=i; j--)
{
cout<<" ";
}
for(k=1; k<=(2*i-1); k++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}

Output:-

C++ Program To Print A Given Triangle of STARS

C++ Program to Copy One String into Another String Without Using strcpy()
Problem:- C++ Program to Copy One String into Another String Without Using strcpy() or Write a c++ program to copy one string to another string without library function or C Program to Copy String Without Using strcpy() or C++ Program to Copy Strings or C++ Program to Copy One String into Another String or C Program to Copy One String into Other Without Using Library Function or C++ Program to Copy One String to Another using Library Function or C++ Program to Concatenate Two Strings or strcpy in C/C++ or copy one string to another in c++.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


Logic:- For this problem, you can use two methods one method is Using Library Function and secondly is Without Using Library Function. Here we are going through second method copy one string to in another without using inbuilt functions for that we are taking an input from the user after that with the help of "For Loop" copying character by character fro one string to another. Below are both methods(using library function and without using library function) you can try both.


Using Library Function

strcpy:- You can use strcpy() for copy one string to another string syntax is given below

Syntax:-

strcpy (destination, source)

it will copy a string from source and paste into a string in the destination.


Without Using Library Function

Here you can use a loop(For Loop And While Loop recommended ) and copy character by character from one string to another string.

Syntax:-

for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}


Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page.


Try Yourself C++ Program To Print Reverse Sentence

Solution:-



#include<iostream>
using namespace std;

int main()
{
/*Visit - www.programmingwithbasics.com*/
   
    cout<<"=====================================";
    cout<<"\nVisit - www.programmingwithbasics.com";
    cout<<"\n=====================================";
    
    char s1[100], s2[100], i;
  
    cout<<"\n\nEnter The String S1: ";
    cin>>s1;
  
    for(i=0; s1[i]!='\0'; ++i)
    {
      s2[i]=s1[i];
    }
   
  s2[i]='\0';
   
    cout<<"\n\nCopied String S2 is : "<<s2;
return 0;
}

Output:-


C++ Program to Copy One String into Another String Without Using strcpy()



17/11/2015

C Program To Print Ascii Value Of Character
ASCII :- ASCII was developed from telegraph code. Its first commercial use was as a seven-bit teleprinter code promoted by Bell data services. Work on the ASCII standard began on October 6, 1960, with the first meeting of the American Standards Association's (ASA) (now the American National Standards Institute or ANSI) X 3.2 subcommittee. The first edition of the standard was published in 1963.
Full Form of ASCII Is ( American Standard Code for Information Interchange). Source Wikipedia

For ASCII Table Click Here  

See Also :- C++ Program To Print ASCII Value Of Character

Solution :-

#include<stdio.h>
void main()
{
 /*Program By Ghanendra Yadav
   Visit http://www.programmingwithbasics.com/
  */
char a;
int b;
printf("Enter The Character ");
scanf("%c",&a);
b=a;
printf("\nOutput Is =%d ",b);
}

Output:-



C++ Program To Print The Series 1 -4  7 -10 . . . . . -40
Problem :- Write A C++ Program To Print The  Given Series 1 -4  7 -10 . . . . . -40

Logic :- See the Series and according to series try to implement your logic .I just want to say only one thing if you practice more and more so you can solve and understood easily ,don't depend to copy paste answer .

Try Yourself  Write A C++ Program To Find Sum Of Series 1+2+4+8+16+32+...........n

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,a=-1,b,n;
cout<<"Enter The Number of Terms\n";
cin>>n;
for(i=1;i<=n;i+=3)
{
      a*=-1;
      b=i;
      b*=a;
      cout<<b<<" ";
}
return 0;
}

Output:-

C++ Program To Print The Series 1 -4  7 -10 .................-40

16/11/2015

C++ Program To Print And Find Sum Of Series 1+2+4+8+16+32+. . . N
Problem :- Write A C++ Program To Find Sum Of Given Series 1+2+4+8+16+32+.....n

Logic :- Logic is very simple you need to use one for loop and initialize with 1 and run into numbers of terms condition and increase by multiply of condition like 
i*=2 and print the value of i .
or sum=sum+i; this statement print the sum of the series .

Try Yourself  C++ Program To Find Sum Of Series 1^2+3^2+5^2+. . . . n^2

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,n,sum=0;
cout<<"Enter The Number Of Terms \n";
cin>>n;
cout<<"\nSeries I sGiven Below\n\n";
for(i=1;i<=n;i*=2)
{
sum+=i;
cout<<i<<" ";
}
cout<<"\n\nSum Of Above Series Is\n";
cout<<sum <<endl;;
return 0;
}

Output:-
C++ Program To Find Sum Of Series 1+2+4+8+16+32+. . . N

C++ Program To Find Sum Of Given Series 1^2+3^2+5^2+ . . . . n^2
Problem :- Write A C++ Program To Find Sum Of Series 1^2+3^2+5^2+ . . . . n^2

Logic :- In This series loop initialize with zero and increase by 2 cause we have to find the sum of the power of 2 odd  number so first print odd number then power of 2 after that sum .
 i+=2 ( increase by 2 )
i*i ( Power of 2 )
sum+=(i*i); ( sum of odd )

Try Yourself C++ Program To Find Sum Of Series 1+x^1+x^2+x^3+ . . . . . x^n

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghannedra Yadav
int n,i;
long sum=0;

cout<<"1^2+3^2+5^2+……+n^2\n\nEnter Value of N :\n";
cin>>n;

for(i=1;i<=n;i+=2)
{
sum+=(i*i);
}

cout<<"\nSum of given series is = "<<sum<<endl;
return 0;
}

Output:-

C++ Program To Find Sum Of Given Series 1^2+3^2+5^2+ . . . . n^2

C++ Program To Find Sum Of The Given Series 1+x^1+x^2+x^3+ . . . . x^n
Problem :- Write A C++ Program To Find Sum Of The Given Series 1+x^1+x^2+x^3+ . . . . x^n .

Logic :- We can clearly seen that series 
x^0+x^1+x^2+x^3 and so on so first we enter value of 'X' then number of terms after that we calculate sum of series upto n'th terms.In the last print the sum of series . 

Try Yourself C++ Program To Find Sum Of Given Series 1^2+3^2+5^2+ . . . . n^2

Solution :-

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//By-Ghanendra Yadav
long i,n,x,sum=1;

cout<<"\n1+x+x^2+……+x^n";
cout<<"\nEnter The Value Of X and n :\n";
cin>>x>>n;

for(i=1;i<=n;++i)
{
sum+=pow(x,i);
}
cout<<"\nSum = "<<sum<<endl;
return 0;
}

Output:-

C++ Program To Find Sum Of The Given Series 1+x^1+x^2+x^3+ . . . . x^n

C++ Program To Find Sum Of The Given Series 1/2+4/5+7/8+ . . . N
Problem :- Write A C++ Program To Find Sum Of The Given Series 1/2+4/5+7/8+.......N

Logic :- As we can see the the dividend of the given series is increase by 3 each time and divisor is also increasing by 3 but divisor = 
dividend+1 so according to the series we can use for loop 
Logic is Given Below .

for(i=0;i<n;++i)
{
x=a/(a+1);
sum+=x;
a+=3;
}

Try Yourself C++ Program To Find Sum Of The Given Series 1+2+3+4+5+6 . . . . n

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,n;
float sum=0,x,a=1;

cout<<"\n1/2+4/5+7/8+……'\n";
cout<<"\nEnter The Number of Terms \n";
cin>>n;

for(i=0;i<n;++i)
{
x=a/(a+1);
sum+=x;
a+=3;
}

cout<<"\n Sum = "<<sum<<endl;
return 0;
}

Output:-

C++ Program To Find Sum Of The Given Series 1/2+4/5+7/8+ . . . N

C++ Program To Find Sum Of The Given Series  x+x^2/2+x^3/3+x^4/4+ . . . . x^n/n
Problem :- Write A C++ Program To Find Sum Of The Given Series  x+x^2/2+x^3/3+x^4/4+ . . . . x^n/n

Logic :- This is very simple series just take a input a value of 'X' and numbers of terms and calculate the sum .at the end print the sum the logic is given below .

for(i=1;i<=n;++i)
{
sum+=pow(x,i)/i;
}

Try Yourself C++ Program To Find Sum Of Given Series 1^2+3^2+5^2+ . . . . n^2

Solution :-

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,n;
float x,sum=0;

cout<<"\nx+x^2/2+x^3/3+…..+x^n/n\n";
cout<<"\nEnter value of x and n :\n";
cin>>x>>n;

for(i=1;i<=n;++i)
{
sum+=pow(x,i)/i;
}
cout<<"\nSum Is = "<<sum<<endl;
return 0;
}

Output:-

C++ Program To Find Sum Of The Given Series  x+x^2/2+x^3/3+x^4/4+ . . . . x^n/n

C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n
Problem :- Write A C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n .

Logic :- This is very simple series you just need to print sum of 1 to n terms ,there are two method you can use either use for loop or use formula Running time of using formula is Constant or 
Running time of using for loop is O(n) in words ' Order of N '

Method 1:- sum of series from 1 to N.

Formula =n(n+1)/2

Method 2:- sum of series from 1 to N.

for(i=1;i<=n;++i)
{
sum+=i;
}
If you are interested then you can modified this series for large number or you can try below series .

Method 1:- Using For Loop

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,n,sum=0;

cout<<"\n1+2+3+4+5+6+……+n\n";
cout<<"\nEnter The Value Of N:\n";
cin>>n;

for(i=1;i<=n;++i)
{
sum+=i;
}
cout<<"\nSum = "<<sum<<endl;
return 0;
}

Method 2:- Using Formula

#include<iostream>
using namespace std;

int main()
{
//By-Ghanendra Yadav
int i,n,sum=0;

cout<<"\n1+2+3+4+5+6+……+n\n";
cout<<"\nEnter The Value Of N:\n";
cin>>n;

sum=(n*(n+1))/2;
cout<<"\nSum = "<<sum<<endl;
return 0;
}

Output:-

Method 1:-
C++ Program To Find Sum Of The Following Series 1+2+3+4+5+6 . . . . . n






C++ Program To Find Sum Of Series 1+1/2^2+1/3^3+…..+1/n^n
Problem :- Write A C++ Program To Find Sum Of Series 1+1/2^2+1/3^3+…..+1/n^n .

Logic :- This Below Solve this series in second ,use this statement and use it .you also can modify and see the difference 

Syntax :-

Series=1/pow(i,i);

After that statement you need to add all the statement .   

Solution :-

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//By-Ghanendra Yadav
double sum=0,a;
int n,i;

cout<<"1+1/2^2+1/3^3+…..+1/n^n";
cout<<"\nEnter The Value Of n :\n";
cin>>n;

for(i=1;i<=n;++i)
{
a=1/pow(i,i);
sum+=a;
}
cout<<"Sum = "<<sum<<endl<<endl;
return 0;
}

Output:-

C++ Program To Find Sum Of Series 1+1/2^2+1/3^3+…..+1/n^n

C++ Program To Read Infinite Number And Sort In Ascending Order Using Pointer
Problem:- Write A C++ Program To Read Infinite Number Then Arrange Ascending Order Using Pointer or write a c++ program to arrange the given numbers in ascending order using pointers or sort array of pointers c++ or program to sort an array in ascending order using pointers in c++ or sorting using pointers in c++ or bubble sort using pointers c++ or insertion sort using pointers c++ or array using pointers in c++ or c++ program to sort an array using pointers or c++ program to sort an array using pointers or write a c++ program to arrange the given numbers in ascending order using pointers or program to sort an array in ascending order using pointers in c++ or sorting using pointers c++ code or sort array of pointers c++ or sorting an array using pointers in c++

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.


Logic:- We are using Malloc and realloc (reallocated memory during runtime ) after getting -1 stop inserting numbers and then use an any sorting algorithm and print the element of the array. if you don't know any sorting algorithm then you have to check here 

Sorting

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

Solution:-

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
  /*visit - programmingwithbasis.com*/
  
  int *p,*q,i=1,j,k,temp;
  
  cout<<"Enter Infinite Numbers and (-1 To Stop Reading)\n";
  
  p=(int*)malloc(sizeof(int));
  cin>>p[0];

  while(p[i-1]!=-1)
  {
    i++;
    p=(int*)realloc(p,sizeof(int)*i);
    q=p;
    cin>>p[i-1];
  }
   
    p=q;
  
  for(j=1;j<i;++j)
  {
    for(k=0;k<i-j-1;++k)
    {
    if(p[k]>p[k+1])
    {
      temp=p[k];
      p[k]=p[k+1];
      p[k+1]=temp;
    }
    }
  }
   
    cout<<"\nAscending Order Is Given Below \n\n";

for(j=0;j<i-1;++j)
  {
    cout<<" "<<p[j];
  }
}

Output:-


c program to sort elements of array using pointers


You May Also Like


1.C Program For Remove All Vowels From A String

2.C Program To Remove Given Word From A String

3.C Program To Convert String To Integer Without Using Library Functions

4.C Program For Upper Case And Lower Case Conversion And Vice-Versa

5.C Program For Reverse A String Using Library Function

6.C Program For HEAP Sort In Ascending Order Also Show Complexity

7. C Program To Insert An Element Desired or Specific Position In An Array

8. C Program For Remove Duplicates Items In An Array

9. C Program To Delete Element From Array At Desired Or Specific Position

10. C Program For Print "I AM IDIOT" Instead Of Your Name Using Array

11.
C Program For Check String Is Palindrome Or Not Using For Loop


15/11/2015

C++ Program To Swap Two Numbers Without Using Third Variable
Problem :- Write A C++ Program To Swap Two Numbers Without Using Third Variable Using Functions .

Logic :- For This question We Have 3 Method to Swap Without Using third variable .
1. Plus/Minus
2. Multiply/Divide
3. Bitwise Operator 

All Methods are follow you also can check C++ Program To Swap Two Numbers Using Functions .

Solution :-

Method 1 :- Plus/Minus

#include<iostream>
using namespace std;
void swpa(int ,int );

int main()
{
    //By-Ghanendra Yadav
    int a,b;
    
cout<<"Enter Two Number You Want To Swap :\n";
    cin>>a>>b;
    
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
    swap(a,b);
 
    cout<<a<<"\t"<<b<<" \n";
    return 0;
}
void swap(int x,int y)
{
//without using third variable
x=x+y;
y=x-y;
x=x-y;
}

Method 2 :- Multiply/Divide

#include<iostream>
using namespace std;
void swpa(int ,int );

int main()
{
    //By-Ghanendra Yadav
    int a,b;
    
cout<<"Enter Two Number You Want To Swap :\n";
    cin>>a>>b;
    
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
    swap(a,b);
 
    cout<<a<<"\t"<<b<<" \n";
    return 0;
}
void swap(int x,int y)
{
//without using third variable
x=x*y;
y=x/y;
x=x/y;
}

Method 3 :- Bitwise Operator

#include<iostream>
using namespace std;
void swpa(int ,int );

int main()
{
    //By-Ghanendra Yadav
    int a,b;
    
cout<<"Enter Two Number You Want To Swap :\n";
    cin>>a>>b;
    
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
    swap(a,b);
 
    cout<<a<<"\t"<<b<<" \n";
    return 0;
}
void swap(int x,int y)
{
//without using third variable
x=x^y;
y=x^y;
x=x^y;
}
 
Output:-
Method 1 :- Plus/Minus

C++ Program To Swap Two Numbers Without Using Third Variable


Method 2 :- Multiply/Divide
C++ Program To Swap Two Numbers Using Multiply/Divide


Method 3 :- Bitwise Operator
C++ Program To Swap Two Numbers Using Bitwise Operator

Swapping of Two Numbers in C++ Using Functions | Call by Reference & Call by Value
Write a Program for Swapping of Two Numbers in C++ Using Call By Value and Call by Reference. There are two methods to solve this problem with the help of functions, and there are two methods to do this the first one is Call By Value and Call by Reference. Now let discuss the Call by Value in details. In the function we can pass the value by 2 ways the first one is Call By Value and the second one is Call by Reference and there are 2 things we have to discuss that Actual Parameter and Formal Parameter to fully understand the passing values in the function in C++ Programming language. In this article I am going to tell you the Call By Value, Call by Reference, Actual Parameter and Formal Parameter to fully understand the programming problem.

Write a Program to Swap of Two Numbers in C++


1. Call by Value


In Call by Value Actual parameter are passed while calling the function, The operations effect on the formal parameters doesn't reflect on the Actual Parameters.

Example: Int A = 5 is an actual parameter and Int X = 5(Here we have Copied the Int A = 5 value to the X = 5), so whatever we do with the X, it does not reflect the Actual Value Int A = 5. It will always remain the same.  If we increase the value of the X by 1 then the value of the X will be 6 and the value of the A remains the same 5 as previous.

2. Call by Reference


In Call by Reference we passed the address of the actual parameter in a formal parameter, So the changes on the Formal Parameters reflect on the Actual parameters. If we take the above example for this then if we increase the value of the X will reflect on the A thus the value of the X and A will be same(X = A = 6)

Before going to understand the Call by value and Call by Reference let's first understood the Actual and Formal Parameters terminology to fully understand the code.


Actual parameters: The Actual parameters that appear in function calls.
Formal parameters: The Formal parameters that appear in function declarations.

1. Swapping of Two Numbers in C++ Using Call by Reference | Functions


#include<iostream>
using namespace std;

void swap(int *x ,int *y );
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter Two Numbers To Swap: ";
    cin>>a>>b;
    
    swap(&a,&b);
    
    cout<<"\nAfter Swapping Two Numbers: ";
    cout<<a<<" "<<b<<" \n";
    
    return 0;
}
void swap(int *x,int *y)
{
 int z;
 z=*x;
/*Copying the first variable Address to the tempriory variable*/
 *x=*y;
/*Copying the second variable Address to the first variable*/
 *y=z;
/*Copying the tempriory variable Address to the second variable*/ 
}

2. Program to Swapping of Two Numbers in C++ Using Call by Value | Functions


#include<iostream>
using namespace std;

void swap(int ,int );
/*Swapping of Two Numbers in C++ Using Functions Call by Value*/
int main()
{
   //Program by- Ghanendra Yadav
    int a,b;
    cout<<"Enter the Two Numbers to Swap in C++: ";
    cin>>a>>b;
    cout<<"\nAfter Swapping of Two Numbers:";
    swap(a,b);
    
    return 0;
}
void swap(int x,int y)
{
 int z;
/*Extra veriable for storing the value of first or second variable*/ 
 
 z=x;
/*Copying the first variable value to the tempriory variable*/
 x=y;
/*Copying the second variable value to the first variable*/
 y=z;
/*Copying the tempriory variable value to the second variable*/ 
 cout<<" "<<x<<"   "<<y;
 
}

Program Output of Swapping of Two Numbers Using Call by Reference

Program Output of Swapping of Two Numbers Using Call by Reference

Program Output of  Swapping of Two Numbers Using Call by Value


Program Output of  Swapping of Two Numbers Using Call by Value