Saturday, 18 February 2017

C++ program of entering employee data through functions and structure

In this program we will learn how to enter employee data through functions using structure.
The code is given below.

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

using namespace std;

struct emp
{
int id,salary;
char name[20];
};

void input (emp &emp1);

void show (emp emp1);

int main()
{
emp emp1;

input (emp1);

show (emp1);

     getch();

}

void input (emp &emp1)
{
cout<<"Enter Employee ID : "<<endl;
cin>>emp1.id;

cout<<"Employee Name : "<<endl;
cin>>emp1.name;

cout<<"Enter Salary :"<<endl;
cin>>emp1.salary;
}

void show (emp emp1)
{

cout<<"ID: "<<emp1.id<<endl;

cout<<"Name: "<<emp1.name<<endl;

cout<<"Salary: "<<emp1.salary;
}
//code ends


Share:

C++ program of calling a structure in a function

In this program we will learn, how to call a structure inside a function and insert its values outside the main()..
The code is given below with screenshot.

//Code starts

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

using namespace std;

struct student{
int id;
char name[15];
};

void show (student s1);

int main()

{
student s1;

show (s1);

getch();

}

void show (student s1)
{
cout<<"Enter Student name"<<endl;
cin>>s1.name;

cout<<"Enter your Roll Number"<<endl;
cin>>s1.id;

cout<<"Name: "<<s1.name << endl;
cout<<"ID: "<<s1.id;
}
//Code ends



Share:

C+ program, Functions of exponential, increment and addition

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

using namespace std;

int expo(int a, int b);

int inc(int x);

int add(int a, int b);

int main()

{
int num,x;
cout<<"Enter Number for exponential"<<endl;
cin>>num;
cout<<"Enter Power of exponential"<<endl;
cin>>x;
cout<< "Exponential of " <<num<<" is "<<expo(num,x)<<endl;
cout<<"Increament of "<<x<<" is "<<inc(x)<<endl;
cout << "Addition of "<<num<<" and "<<x<<" is "<< add(num,x);
getch();
}

int add(int a, int b)

{
int ans=0;
ans=a+b;
return ans;
}


int inc(int x)

{
x=x+1;
return x;
}



int expo(int a, int b)
{
int ans=1;
for(int i=1;i<=b;i++)
{
ans=ans*a;
}
return ans;
}



Share:

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: