Wednesday, 26 October 2016

C++ program of reverse a number

In this C++ program we will learn how to reverse the sequence of entered numbers

//Code starts
#include<iostream>
#include<conio.h>
using namespace std; 
int main()
{
int number, reverse = 0; 
cout<<"Input a Number to Reverse and press Enter: "; 
cin>> number; // Taking Input Number in variable number 
while( number> 0 ) 
reverse = reverse * 10; 
reverse = reverse + number%10;
number = number/10; 
}
 cout<<"New Reversed Number is: "<<reverse;
getch();
}
//Code Ends

Output:


Share:

Sunday, 12 June 2016

C++ Program of Exponential using Functions

In this program we will learn how to take exponential of a variable e.g 10^2.
The code is given below.

//Code starts
#include<iostream>
#include<conio.h>

using namespace std;

int expo(int b, int p)
{
 int temp=1;        //taking a temporary variable

 for(int i=1; i<=p; i++) //for loop starts
 {
  temp=temp*b;
 }

 return temp;  //returning value of temporary variable
}


int main()   //main starts

{

 int c,d;

 cout<<"Enter Base to Find its Exponential : ";
 cin>>c;

 cout<<"Enter Power to Find its Exponential : ";
 cin>>d;

 cout<<"Answer = "<<expo(c,d); // Function calling
getch();
}
//Code ends
OUTPUT:



Share:

C++ Program of Sum, Multiplication, Average Program in Array

In this program we will learn how to calculate sum, multiply and average of numbers stored in array.
The code is given below.

//Code Starts
#include<iostream>
#include<conio.h>

using namespace std;

int main()

{
  int n,a[n],sum=0,mul=1;
  float avg;

 
  cout<<"Enter Size of Array : ";
  cin>>n;
 
  for(int i=0;i<n;i++)
  {
    cout<<"Enter Number at "<<i<<" index : ";
    cin>>a[i];
  
    sum=sum+a[i];

    mul=mul*a[i];
  }

 cout<<"Sum = "<<sum<<endl;

 cout<<"Multiplication = "<<mul<<endl;

 avg=sum/n;

 cout<<"Average = "<<avg;

 getch();

}
//Code Ends

OUTPUT:


Share:

C++ Program of Binary search in Array

In this program we will learn how to search a number from indexes of array.
The code is given below.

//Code Starts
#include <iostream>
#include<conio.h>

using namespace std;

int main()

{
int n,a[n],search,j;

cout<<"Enter Size of Array : ";
cin>>n;

for(int i=0;i<n;i++)
{
cout<<"Enter "<<i<<" element = ";
cin>>a[i];
}

for(int b=0;b<n;b++)

{
cout<<"Number at index ["<<b<<"] = ";
cout<<a[b]<<endl;
}

cout<<"Enter Number to search : ";
cin>>search;

for(int j=0;j<n;j++)

{
if(search==a[j])
{
cout<<"Number found at index : "<<j<<endl;
break;
}
}
getch();
}
//Code Ends

OUTPUT:



Share:

C++ structure program of person

In this program we will create structure of person enter data in structure and also show the entered data.
The code is given below.

//Code Starts

#include<iostream>
#include<conio.h>

using namespace std;

struct person
{
char name[15];
char age[2];
char father_name[20];
char adress[40];
char phone[11];
};

int main()
{
person p;
cout<<"Enter Name "<<endl;
cin>>p.name;
cout<<"Enter Father's Name "<<endl;
cin>>p.father_name;
cout<<"Enter Age "<<endl;
cin>>p.age;
cout<<"Enter Adress "<<endl;
cin>>p.adress;
cout<<"Enter Mobile Number "<<endl;
cin>>p.phone;
cout<<"Your Entered Data :"<<endl;
cout<<p.name<<endl;
cout<<p.age<<endl;
cout<<p.father_name<<endl;
cout<<p.adress<<endl;
cout<<p.phone<<endl;
getch();
}
//Code Ends

OUTPUT:





Share:

Monday, 30 May 2016

C++ program of bubble sorting array

In this program we will learn how  to sort elements of array in bubble sorting order.
The code is given below.

//Code starts
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;

for(i=0;i<n;++i)
{
cout<<"Enter number at index ["<<i<<"] : ";
cin>>a[i];
}

for(i=1;i<n;++i)
{
for(j=0;j<(n-i);++j)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];

getch();
}
//Code ends

OUTPUT:
Share:

Friday, 27 May 2016

6 in 1 Program in C++

In this program we will learn how to combine 6 C++ program in a single C++ program and run them as per user desire.
The code is given below


//Code Starts

#include <iostream>
#include<math.h>
#include<conio.h>

using namespace std;

int main()

{
int a,b,num,dig,sum,sub,mul;

float rem,div;

char op;

cout<<" Enter 1 to Perform Methamatical Operation "<<endl;

cout<<" Enter 2 to search a prime number in given range "<<endl;

cout<<" Enter 3 to Perform Binary Seartch in Array"<<endl;

cout<<" Enter 4 for Starric Program"<<endl;

cout<<" Enter 5 for Factorial"<<endl;

cout<<" Enter 6 to Check Even or odd Number"<<endl;

abc: cin>>num;

cout<<"------- Program Number : "<<num<<"-----------------"<<endl;

if(num==1)

{

cout<<"Enter  '+' for Sum, or '-' for Subtraction, or '/' for Division, or '*' for Multiplication, or '%'for Remainder  "<<endl;

cin>>op;


switch(op)
{


case'+':

cout<<"Enter 1st Number : " ;

cin>>a;

cout<<"Enter 2nd Number : " ;

cin>>b;

sum=a+b;
cout<<"Sum =  "<<sum;

break;

case'-':

cout<<"Enter 1st Number : " ;

cin>>a;

cout<<"Enter 2nd Number : " ;

cin>>b;

sub=a-b;
cout<<"Subtraction =  "<<sub;

break; 

case'*':

   cout<<"Enter 1st Number : " ;

cin>>a;

cout<<"Enter 2nd Number : " ;

cin>>b;

mul=a*b;

cout<<"Multiplication =  "<<mul;

break;
case'/':

cout<<"Enter 1st Number : " ;

cin>>a;

cout<<"Enter 2nd Number : " ;

cin>>b;

div=a/b;
cout<<"Divide =  "<<div;

break;

case'%':

cout<<"Enter 1st Number : " ;

cin>>a;

cout<<"Enter 2nd Number : " ;

cin>>b;

rem=a%b;
cout<<"Remainder =  "<<rem;

break;

}
}

else if(num==2)

{
int c,d;

int a=0,b=0;

cout<<"ENTER STARTING RANGE TO CHECK PRIME NUMBER"<<endl;
cin>>c;

cout<<"ENTER ENDING RANGE TO CHECK PRIME NUMBER"<<endl;

cin>>d;

for(int i=c;i<=d;i++)
{
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)

a++;
}

if(a==0&&i!=0)
{
b++;

cout<<"PRIME Number =>   "<<i<<endl;

a=0;
}
a=0;
}

cout<<"RANGE OF PRIME NUMBER IN BETWEEEN  "<<c<<" TO "<<d<<" = "<<b;
}
else if(num==3)
{
int a[100],n,i,beg,end,mid,item;

cout<<"\n------------ BINARY SEARCH ------------ \n\n";
cout<<"Enter No. of Elements= ";
cin>>n;

cout<<"\nEnter Elements in Sorted Order=\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

cout<<"\nEnter Item you want to Search= ";
cin>>item;

beg=1;
end=n;

mid=(beg+end)/2;                       // Find Mid Location of Array

while(beg<=end && a[mid]!=item)      // Compare Item and Value of Mid
{
if(a[mid]<item)
beg=mid+1;
else
end=mid-1;

mid=(beg+end)/2;
}

if(a[mid]==item)
{
cout<<"\nData is Found at Location : "<<mid;
}
else
{
cout<<"Data is Not Found";
}
}

else if(num==4)
{
int a;

cout<<"Enter anY number"<<endl;

cin>>a;

for(int i=1;i<=a;i++)

{
for(int j=1;j<=i;j++)

{
cout<<"*";

}

cout<<endl;
}

}

else if(num==5)

{
    int num,factorial=1;

    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int a=1;a<=num;a++)

    {

        factorial=factorial*a;

    }

cout<<"Factorial of Given Number is ="<<factorial<<endl;

}

else if(num==6)

{
    int min,max;
   
 {
    cout << "Enter the minimum range: "<<endl;
    cin >> min;

   cout << "Enter the maximum range: "<<endl;
    cin >> max;

    cout << "Odd numbers in given range are: "<<endl;
    for(int num=min;num <= max;num++)

         if(num % 2 !=0)
             cout << num<< " "<<endl;

    cout<<"Even numbers in given range are: "<<endl;
    for(int even=min;even <= max;even++)

         if(even % 2 ==0)
             cout << even << " "<<endl;
          
  }
}

else
{
cout<<"Invalid Entry enter again with number 1-6 only : "<<endl;
goto abc;
}

getch();

}

//Code Ends

OUTPUT:

Share:

Sunday, 15 May 2016

C++ Program to Sort Distorted Array

#include<iostream>
#include<conio.h>
using namespace std;
//Function for partitioning array
int part(int low,int high,int *a)
{
     int i,h=high,l=low,p,t;  
//p==pivot
     p=a[low];
     while(low<high)
     {
                    while(a[l]<p)
                    {
                                   l++;
                    }
                    while(a[h]>p)
                    {
                                   h--;
                    }
                    if(l<h)
                    {
                                t=a[l];
                                a[l]=a[h];
                                a[h]=t;
                    }
                    else
                    {
                        t=p;
                        p=a[l];
                        a[l]=t;
                        break;
                    }
     }
     return h;   
}

void quick(int l,int h,int *a)
{
  int index,i;
  if(l<h)
  {
          index=part(l,h,a);
          quick(l,index-1,a);
          quick(index+1,h,a);
  }
}

int main()
{
      int a[100],n,l,h,i;
      cout<<"Enter number of elements:";
      cin>>n;
      cout<<"Enter the elements (Use Space As A Separator):";
      for(i=0;i<n;i++)
      cin>>a[i];
      cout<<"\nInitial Array:\n";
      for(i=0;i<n;i++)
      {
                      cout<<a[i]<<"\t";
      }  
      h=n-1;
      l=0;
      quick(l,h,a);
      cout<<"\nAfter Sorting:\n";
      for(i=0;i<n;i++)
      {
                cout<<a[i]<<"\t";
      }
      getch();
      return 0;
}
Share:

Thursday, 12 May 2016

C++ Program of Array to Calculate Sum and Average of given Numbers

// This program calculates the average of any number of numbers.
// Using the for structure
#include <iostream>
using namespace std;
#include <iostream>
 
int main()
{
   int n, count;
   float x, sum, avg;
 
   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;
   int size= n;
   int array[size];
   for (count=1; count<=n; count++){
       cout << "Enter Number:  ";
         cin >> array[n];
        sum = sum + array[n];
   }  //end for
   cout << "The sum is " << sum << endl;
   avg = sum / n;
   cout << "The average is  " << avg << endl;
   system("pause");
   return 0;   //successful termination

 }
Share:

C++ Program of Array to Calculate Average of Numbers Using Arrays

#include <iostream>
using namespace std;
int main(){
    int n, i;
    float num[100], sum=0.0, average;
    cout << "Enter the numbers of data: ";
    cin >> n;
    while (n>100 || n<=0)
    {
        cout << "Error! number should in range of (1 to 100)." << endl;
        cout << "Enter the number again: ";
        cin >> n;
    }
   for(i=0; i<n; ++i)
   {
      cout << i+1 << ". Enter number: ";
      cin >> num[i];
      sum+=num[i];
   }
   average=sum/n;
   cout << "Average = " << average;
   return 0;
}
Share:

Monday, 9 May 2016

C++ Program to Find Even and Odd Elements in Array

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int arr[20],even[20],odd[20],i,j=0,k=0,no;
   clrscr();
   cout<<"How Size of Array: ";
   cin>>no;
   cout<<"Enter any "<<no<<" elements in Array: ";
   for(i=0; i<no;i++)
   {
   cin>>arr[i];
   }
   for(i=0; i<no;i++)
   {
   if(arr[i]%2==0)
   {
    even[j]=arr[i];
    j++;
   }
   else
   {
   odd[k]=arr[i];
   k++;
   }
   }
  cout<<"\nEven Elements: ";
  for(i=0; i<j ;i++)
   {
    cout<<even[i]<<"  ";
   }
  cout<<"\nOdd Elements: ";
  for(i=0; i<k; i++)
   {
    cout<<odd[i]<<"  ";
   }
  getch();
  }
Share: