Showing posts with label C PLUS PLUS PROGRAMS. Show all posts
Showing posts with label C PLUS PLUS PROGRAMS. Show all posts

C++ PROGRAM TO MERGE TWO SORTED ARRAYS


/*C++ PROGRAM TO MERGE TWO SORTED ARRAYS*/


#include<iostream>
using namespace std;

// Merge arr1[0..n1-1] and arr2[0..n2-1] into
// arr3[0..n1+n2-1]
void mergeArrays(int arr1[], int arr2[], int n1,  int n2, int arr3[])
{
    int i = 0, j = 0, k = 0;
  // Traverse both array
    while (i<n1 && j <n2)
    {
                if (arr1[i] < arr2[j])
            arr3[k++] = arr1[i++];
        else
            arr3[k++] = arr2[j++];
    }
     // Store remaining elements of first array
    while (i < n1)
        arr3[k++] = arr1[i++];
  // Store remaining elements of second array
    while (j < n2)
        arr3[k++] = arr2[j++];
}
  // Driver code
int main()
{
    int arr1[] = {1, 3, 5, 7};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);

  int arr2[] = {2, 4, 6, 8};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);

  int arr3[n1+n2];
    mergeArrays(arr1, arr2, n1, n2, arr3);

 cout << "Array after merging" <<endl;

 for (int i=0; i < n1+n2; i++)
        cout << arr3[i] << " ";

      return 0;
} 



PROGRAM TO SWAP TWO NUMBERS USING C++


/*PROGRAM TO SWAP TWO NUMBERS*/



#include<iostream.h>
#include<conio.h>
#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}
void main()
{
clrscr();
intx,y;
cout<<“Enter two numbers:”;
cin>>x>>y;

cout<<“x=”<<x<<” y=”<<y;
SWAP(x,y);
cout<<“nx=”<<x<<” y=”<<y;
getch();
}


Output :-




C++ PROGRAM TO DISPLAY THE FACTORS OF THE ENTERED NUMBER


/*C++ PROGRAM TO DISPLAY THE FACTORS OF THE ENTERED NUMBER */


#include<iostream.h>
using namespace std;

intmain()
{
int number, temp =1;

cout<<"Enter the number to determine its factors : "<<endl;
cin>> number;
cout<<"The factors of "<< number <<" are : "<<endl;
while(temp <= number)
{
if(not(number % temp))
cout<< temp <<" ";

        temp++;
}
cout<<endl;
}

Output :-


Subscribe

Milan Panda
Admin
About Me | Contact
Copyright 2023-2024 © Programming1011 . 🎀 Developed and Design By- Milan Panda. Happy Holi All Of You 🎀