Nov 30, 2014

Write a program using function to Sum of all Odd numbers from an Array. Use function prototype and parameter

#include <iostream>
using namespace std;

int getsum( int n)
    {
    int a[100],sum=0;
    cout<<"Enter "<<n<< " numbers:  ";
    for(int i=0;i<n; i++)
    {
    cin>>a[i];
    }
    for (int i=0;i<n;i++)
    {
    if (a[i]%2!=0)
    {
    sum+=a[i];
    }
    }
    return sum;
    }


    int  main()
    {
    int n;
    cout<<"Enter size of array: ";
    cin>>n;
    cout<<endl<<"Sum of odd numbers is : "<<getsum(n)<<endl;
    return 0;
    }

Recursive Function Progamming

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

class Factorial
{
public:
    void setnum(int n)
    {
    number = n;
    }
    protected: int number;
};
class findfact: public Factorial
{
public:
    int fact=1;
    void getfact()
    {
    for(int i = 1; i <= number; i++)
    {
    fact *= i;
    }
    cout<<"The Factorial value for the number is "<<fact;
    }
};

int main()
{
    findfact facts;
    int n;
    cout<<"Enter a number: ";
    cin>>n;
    facts.setnum(n);
    facts.getfact();
    return 0;
}

Nov 29, 2014

Check Perfect Number using Inheritence

#include<iostream>
using namespace std;

class perfect
{
public:
   void setnum(int digit)
    {
        number =digit;
    }
    protected: int number;
};

class chknumber: public perfect
{
public:
    int sum=0;
    void getperfect()
    {
     for(int i=1;i<number;i++)
    {
    if(number%i==0)
    {
    sum+=i;
    }
    }
    }

int chksum()
{
if (number==sum)
cout<<"Yes!!"<<endl<<"Enter number is perfect number"<<endl<<endl;
else
cout<<"Not!!"<<endl<<"Enter number is not a perfect number"<<endl;
}
};


int main()
{
chknumber per;
int digit;
cout<<"Enter number to check perfect or not : ";
cin>>digit;
per.setnum(digit);
per.getperfect();
per.chksum();
return 0;
}

Count the number of character

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

int character (string s)
{
int charnum=s.size();
for(char i=0; i<s.size(); i++)
{
if(s.at(i)== ' ')
{
charnum--;
}
if(s.at(i)== '.')
  {
charnum--;
}
}
cout<<endl<<"Enter string has "<<charnum <<" Characters:"<<endl;
}


int main()
{
string s;
char i;
cout<<"Please type in a string: " ;
getline(cin,s);
character(s);
return 0;
}

Sum of all even elements from an Array

    #include <iostream>
    using namespace std;

    class evensum
    {
    public:
        void setnum(int n)
        {
            number =n;
        }
        protected: int number;

    };

class findsum: public evensum
{
public:
    void getsum()
    {
    int temp,i,a[100],sum=0;
    temp=number;
    cout<<"Enter "<<temp<< " numbers:  ";
    for(i=0;i<temp; i++)
    {
    cin>>a[i];
    }
    for (i=0;i<temp;i++)
    {
    if (a[i]%2==0)
    {
    sum+=a[i];
    }
    }
    cout<<"Sum of even numbers is : "<<sum<<endl;
    }
};
    int  main()
    {
    findsum es;
    int n;
    cout<<"Enter size of array: ";
    cin>>n;
    es.setnum(n);
    es.getsum();
    return 0;
    }



Search an element in an Array of n integer values. Using inheritance

#include<iostream>
using namespace std;

class searnum
{
public:
    void setnum(int n)
    {
        number = n;
    }
    protected: int number;
};


class chknum: public searnum
{
public:
    int a[100],i,temp,item,s=0;
    void found()
    {
    temp=number;
    cout<<"Enter "<<temp<<"  number : ";
    for(i=1;i<=temp;i++)
    {
    cin>>a[i];
    }
    cout<<"Enter number what are you want to be Search : ";
    cin>>item;
    for(i=1;i<=temp;i++)
    {
    if(a[i]==item)
    {
    cout<<item<<" is Found in the array  at Location : "<<i<<endl;
    s=1;
    break;
    }
    }
    if(s==0)
    {
    cout<<item<<" is Not Found in tha array"<<endl;
    }
    }
    };

int main()
{
int n;
chknum ar;
cout<<"Ente size of array: ";
cin>>n;
ar.setnum(n);
ar.found();
return 0;
}

Check whether the given number is palindrome or not. Use individual member function to take input, output and display. Use Inheritance

#include<iostream>

using namespace std;

class palindrome

{

public:

   void setnum(int n)

    {

        number=n;

    }

     protected: int number;

};

class palnum:public palindrome

    {

    public:

       int temp,rev=0,rem;

void chknum()

    {

temp=number;

while(temp!=0)

{

rem=temp%10;

temp=temp/10;

rev=rev*10+rem;

}

if(rev==number)

cout<<"Yah!! "<<endl<<number<<" is palindrome number"<<endl;

else

cout<<"No ??"<<endl<<number<<" is not palindrome number"<<endl;

}

};

int main()

{

int n;

palnum pal;

cout<<"Enter number to check palindrome or not :";

cin>>n;

pal.setnum(n);

pal.chknum();

return 0;

}

Nov 28, 2014

Find ODD or EVEN

#include<iostream>
using namespace std;

int main()
{
int min,max;
cout<<"\n\t******Find even and odd numbers in given range  : ****"<<endl<<endl;

cout<<"Enter min number   : ";

cin>>min;

cout<<"Enter max number   : ";

cin>>max;

cout<<"\n";

    cout<<"Even Numbers form "<<min<<" to " <<max <<" is  : ";
    for(int i=min;i<=max;i++)
        {
        if(i%2==0)
        cout<<i<<", ";
        }

    cout<<endl<<endl;
    cout<<"Odd Numbers form "<<min<<" to " <<max <<" is  : ";
      for(int j=min;j<=max;j++)
        {
        if(j%2!=0)
        cout<<j<<", ";
        }
        cout<<endl;
return 0;
}

Write a Object Oriented Program to Display Student Information

/*
Create a class named as Student containing the following data members:
--Roll_no
--Name
--Marks1, Marks2, Marks3
Write necessary member functions:
1. to accept details of all students
2. to display details of one student
3. to display details of all students(Use Function overloading).

*/


#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include<cstring>
using namespace std;

    class student
    {
        public:
        char first_name[30], last_name[30];
        int roll;
        float ban_mark,eng_mark,mat_mark;
  

        void body()
        {
    system("cls");
    cout << "\n\n";
    cout << "\t\t****** INFORMATION OF STUDENTS ******";
    cout << "\n\n";
    cout << "\n \t\t 1. Add    Students";
    cout << "\n \t\t 2. Search by first Name ";
    cout << "\n \t\t 3. Show  all Records";
    cout << "\n \t\t 4. Delete Records";
    cout << "\n \t\t 5. Exit";
    cout << "\n\n";
    cout <<"\t Select What you want:<1-5> ";

        }
    void getdata()
    {
        system("cls");
        cout << "First Name         : ";
        cin >> first_name;
        cout << "Last Name          : ";
        cin >> last_name;
        cout << "Roll No            : ";
        cin >>roll;
        cout << "Marks of Bangla    : ";
        cin >> ban_mark;
        cout << "Marks of English   : ";
        cin >> eng_mark;
        cout << "Marks of Mathmetic : ";
        cin >> mat_mark;

    }

        void showdata()
        {
        cout<< "\n";
        cout <<"\t Name of Students : "<< first_name << setw(8)<< last_name;
        cout << "\n";
        cout <<"\t Roll No          : " <<roll;
        cout << "\n";
        cout <<"\t Bangla           : " <<ban_mark;
        cout << "\n";
        cout << "\t English          : " <<eng_mark;
        cout << "\n";
        cout <<"\t Mathmetics       : " <<mat_mark<<endl;
        cout<<"\t" <<endl<<"================================="<<endl;
        }


    };

int main()
{

student inf;
char xfirst_name[30];
char option, select;


    FILE *fp, *ft;
    fp=fopen("users.txt","rb+");

    if (fp == NULL)
    {
        fp = fopen("users.txt","wb+");

            return 0;

    }

    while(1)
    {
    inf.body();
    select = getche();

        switch(select)
            {
            case '1':

            fseek(fp,0,SEEK_END);
            option ='Y';
            while(option == 'Y' || option == 'y')
            {
            inf.getdata();

               fwrite(&inf,sizeof(inf),1,fp);
                cout << "\n Add Another Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }
            break;

            case '2' :
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
                cout << "\n Enter the first name of the student : ";
                cin >> xfirst_name;

                rewind(fp);
                while (fread(&inf,sizeof(inf),1,fp)==1)
                {
                    if (strcmp(inf.first_name,xfirst_name)== 0)
                    {
                 inf.showdata();

                        break;
                    }
                    else
                        cout<<endl;
                        cout<<"record not found"<<endl;
                }
                cout << "\n Find  Another Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }
            break;

            case '3':
            system("cls");
            rewind(fp);
            cout << "=== View all Records in the Database ===";
            cout << "\n";
            while (fread(&inf,sizeof(inf),1,fp))
            {
                inf.showdata();
            }
             cout << "\n\n";
            system("pause");
            break;


            case '4':
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n Enter the fitst name of the student to delete : ";
            cin >> xfirst_name;

            ft = fopen("temp.dat", "wb");

                rewind(fp);
                while (fread (&inf, sizeof(inf),1,fp) == 1)

                    if (strcmp(inf.first_name,xfirst_name) != 0)
                    {
                        fwrite(&inf,sizeof(inf),1,ft);
                    }
                fclose(fp);
                fclose(ft);
                remove("users.txt");
                rename("temp.dat","users.txt");

                fp=fopen("users.txt","rb+");

                cout << "\n Delete option Record (Y/N) ";
                fflush(stdin);
                option = getchar();
            }

            break;

            case '5':
            fclose(fp);
            system("cls");
            cout << "\n\n";
            cout << "\t\t********************************************************"<<endl;
            cout << "\t\t*          THANKS BY MUJAFFOR , Batch-46, AUB          *"<<endl;
            cout << "\t\t********************************************************"<<endl;
            cout << "\n\n";
            exit(0);
        }
    }


    system("pause");
    return 0;
}

Check Whether the given number is Strange or Not.

#include<iostream>
using namespace std;

int strange(int digit)
{
int d,r;
while(digit!=0)

{
d=digit/10;
r=digit%10;
digit=d;
return r;
}
}

int main()
{
int digit,p=1,re;
cout<<"Enter number to check strange or not : ";
cin>>digit;
cout<<endl;
re=strange(digit);
for(int i=2;i<re;i++)
{
if (re%i ==0)
{
p=0;
i=re;
}
}
if (p==1)
cout<<"Yes!!"<<endl<<"Enter number is strange number"<<endl<<endl;
else
cout<<"Not!!"<<endl<<"Enter number is not a strange number"<<endl;

}

C++ program to find area of triangle, circle,and rectangle using function overloading

#include<iostream>
using namespace std;

const float pi=3.14;
float area(float n,float b,float h)
{
float ar;
ar=n*b*h;
cout<<"The area of Triangle : "<<ar<<endl<<endl;
}
float area(float r)
{
float ar;
ar=pi*r*r;
cout<<"The area of Circle : "<<ar<<endl<<endl;
}
float area(float l,float w)
{
float ar;
ar=l*w;
cout<<"The area of Rectangle :"<<ar<<endl;
}
int main()
{
float b,h,r,l,w;

cout<<"Enter the Base & Hieght of Triangle : ";
cin>>b>>h;
area(0.5,b,h);

cout<<"Enter the radius of circle :";
cin>>r;
area(r);


cout<<"Enter the Lenght & width of Rectangle: ";
cin>>l>>w;
area(l,w);
return 0;
}

Create a Diamond in C++

 #include<iostream> using namespace std; int main() { int i, j, k,r; cout<<"Enter the row of diamond   "; cin>>r...