Saturday, 18 February 2017

C++ program of knowing date of birth using structure

In this program we will how to enter date using structure and knows the date of birth of someone


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

using namespace std;

class date{

 private:
  int day,month,year;
 
  public:
   void setday();
   void setmonth();
   void setyear();
  
   int getday();
   int getmonth();
   int getyear();
  
   void display();
  
};

void date::setday()
{
 cout<<"Enter day ";
 cin>>day;
}

void date::setmonth()
{
 cout<<"Enter Month ";
 cin>>month;
}

void date::setyear()
{
 cout<<"Enter Year ";
 cin>>year;
}

int date::getday()
{
 return day;
}

int date::getmonth()
{
 return month;
}

int date::getyear()
{
 return year;
}


void date::display()
{

 cout<<day<<"-"<<month<<"-"<<year<<endl;

}

int main()
{
 date d1;

 d1.setday();

 d1.setmonth();

 d1.setyear();

 d1.display();


  if(d1.getday()==25&&d1.getmonth()==12)
 {
  int age;
 
  age=d1.getyear()-1876;
  cout<<"Today is Quaid_E_Azam's "<<age <<"th Birthday";
 }
 
 getch();
}

//code ends





Share:

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: