Sep 29, 2021

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;

cout<<endl;

for(i=1;i<=r;i++)

{

for(j=1;j<=r-i;j++)

{

    cout<<" ";

}


for(k=1;k<=2*i-1;k++)

{

    cout<<"*";

}

cout<<endl;

}

for(i=r-1;i>=1;i--)

{

for(j=1;j<=r-i;j++)

{

    cout<<" ";

}

for(k=1;k<=2*i-1;k++)

{

    cout<<"*";

}

cout<<endl;

}

}


Sep 17, 2016

GPA calculator

 #include<iostream>
 using namespace std;

  int main()
  {
    int n;

  cout<<"Enter number ";
 cin>>n;
  if(n>0&&n<=100)
  {
if(n>=80)
{
    cout<<"You get A+";
}
else if(n>=70)
{
    cout<<"You get A";
}

else if(n>=60)
{
    cout<<"You get A-";
}

else if(n>=50)
{
    cout<<"You get B";
}
else if(n>=40)
{
    cout<<"You get c";
}


else{
    cout<<"Fail";
}



  }
else
{
    cout<<"Enter number is invalid";
}
return 0;
}

Dec 11, 2014

Recersive Function in C++

Create a recursive c++ function to fill up an array and make sum of all digit. you have to create different recursive function for fill up and to make sum. 


#include <iostream>
using namespace std;

int Array(int arr[],int n)
{
    if(n>0)
    {
        Array(arr,n-1);
        {
            cin>>arr[n-1];
        }
    }
    return arr[n-1];
}

int sum(int a[],int n)
{
int sum=0;
for (int i=0;i<n;i++)
{    sum+=a[i];
}
return sum;
}

int main()
{
    int n;
    cout<<"Enter size of Array : ";
    cin>>n;
    int x[n];
    cout<<"Enter elements: ";
    Array(x,n);
    cout<<"The array fill up by this number: ";
    for(int i=0;i<n;i++)
    {
        cout<<x[i]<<" ";
    }
    cout<<"\n"<<"The sum of all digit in array is : "<<sum(x,n);
}

Recersive Function in C++

Generate a Fibonacci series between 0 and 1000. The first two initial value is 0 and 1


#include<iostream>
using namespace std;

class fibonacci
{
public:
void setnum(int a, int b)
{
    n1=a;
    n2=b;
 
}
protected:
    int n1;
    int n2;
    int n3;
};

 class series:public fibonacci
 {
     public:
void get_series()
 {
    cout << "The Fibonacci Series  is:  \n";
    while (n1+n2<1000)
    {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        cout << n3 << "  ";
    }

 }
 };

 int main()
 {
     series s;
     s.setnum(0,1);
     s.get_series();
 }
     

Constructor in c++ : Sum of all multipliers

write a c++ program which contains a constructor to make sum of all multipliers of 7 or 9 between 1 and 100.



#include <iostream>
using namespace std;
class multipliers
{
public:
int sum=0,number;
multipliers();
~multipliers();
void getsum();
};
multipliers::multipliers()
{
}
multipliers::~multipliers()
{
}
void multipliers::getsum()
{
cout<<"All  multipliers  of 7 or 9 between 1 and 100 is : ";
for(number=1;number<=100;number++)
{
if ((number%7==0)||(number%9==0))
{
cout<<number<<"  ";
sum+=number;
}
}
cout<<"\n\n The sum of all multipliers of 7 or 9 between 1 and 100 is : "<<sum<<endl;
}
int main()
{
multipliers m;
m.getsum();

}

Dec 2, 2014

Banking System

Define a class to represent a bank account which includes following members:
Data members –   i) Name
ii) Account number
iii) Type of account
iv) Balance Amount
Member functions –
a. To assign initial value
b. To deposit an account
c. To withdraw from an account
                                    d. To display name, account number & balance
                                    e.  To display All Account
                                    f. Delete An account



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

    class bank
    {
    public:
    char name[100];
    int ac_no;
    int deposit;
    char type ;

        void body()
        {
        system("cls");
        cout<<"\n\n\t\t ==================== Bank Management ====================\n\n";
        cout<<"\n\n\t\t 1. Create an account";
        cout<<"\n\n\t\t 2. Search account";
        cout<<"\n\n\t\t 3. Show all account";
        cout<<"\n\n\t\t 4. Deposit amount";
        cout<<"\n\n\t\t 5. withdraw amount";
        cout<<"\n\n\t\t 6. Delete an account";
        cout<<"\n\n\t\t 7. Exit";
        cout<<"\n\n\tSelect Your Option (1 ==>> 7)  ";

        }
    void getdata()
    {
        system("cls");
        cout<<"\nEnter The account No.              : ";
        cin>>ac_no;
        cout<<"\nEnter The Name of account Holder   :  ";
        cin>>name;
        cout<<"\nEnter Type of The account (S/C)    :  ";
        cin>>type;
        cout<<"\nEnter The Initial amount           :  ";
        cin>>deposit;
        cout<<"\n\n\n You have successfully  created  account..................";

    }

        void showdata()
        {
        cout<<"\nAccount No.          : "<<ac_no;
        cout<<"\nAccount Holder Name  : "<<name;
        cout<<"\nType of Account      : "<<type;
        cout<<"\nBalance amount       : "<<deposit;
        cout<<"\n\n ==================================================";
        }


void dep(int dw)
    {
        deposit+=dw;
    }

    void draw(int dw)
    {
        deposit-=dw;
    }

    int retacno()
    {
        return ac_no;
    }

    int retdeposit()
    {
        return deposit;
    }
    };

int main()
{

bank ac;
int n,dw ;
char option, select;


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

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

            return 0;

    }

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

        switch(select)
        {
        //Create account...................
        case '1':
        fseek(fp,0,SEEK_END);
        option ='Y';
        while(option == 'Y' || option == 'y')
        {
        ac.getdata();
        fwrite(&ac,sizeof(ac),1,fp);
        cout << "\n\n Create Another Account (Y/N) ";
        fflush(stdin);
        option = getchar();
        }
        break;

    //Search account.......................
            case '2' :
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n\n Enter the account no  : ";
            cin >> n;
            rewind(fp);
            while (fread(&ac,sizeof(ac),1,fp)==1)
            {
            if (ac.retacno()== n)
            {
            ac.showdata();
            break;
            }
            }
            cout << "\n Find  Another Account (Y/N) ";
            fflush(stdin);
            option = getchar();
            }
            break;

        //Display all account......................
            case '3':
            system("cls");
            rewind(fp);
            cout << "\n\n=======================  View all Account ==================";
            cout << "\n";
            while (fread(&ac,sizeof(ac),1,fp))
            {
            ac.showdata();
            }
             cout << "\n\n";
            system("pause");
            break;

    //Deposit account...............
            case '4' :
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n Enter the account no  : ";
            cin >> n;
            rewind(fp);
            while (fread(&ac,sizeof(ac),1,fp)== 1)
            {
            if (ac.retacno()== n)
            {
            ac.showdata();
            cout<<"\n\n\t\t\t ========= deposit amount ========== ";
            cout<<"\n\n Enter The amount to be deposited :  ";
            cin>>dw;
            ac.dep(dw);
            fseek(fp, - sizeof(ac), SEEK_CUR);
            fwrite(&ac,sizeof(ac),1,fp);
            break;
            }
            }
            cout << "\n\n Find another account..... (Y/N) ";
            fflush(stdin);
            option = getchar();
            }
            break;

         //Withdraw  account...............
            case '5' :
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n Enter the account no  : ";
            cin >> n;
            rewind(fp);
            while (fread(&ac,sizeof(ac),1,fp)== 1)
            {
            if (ac.retacno()== n)
            {
            ac.showdata();
            cout<<"\n\n\t ========= Withdraw Amount ========== ";
            cout<<"\n\n Enter The amount to be Withdraw :  ";
            cin>>dw;
            int  bal=ac.retdeposit()-dw;
            if(bal<500)
            {
            cout<<"\n\n No enough balance";
            }
            else
            ac.draw(dw);
            fseek(fp, - sizeof(ac), SEEK_CUR);
            fwrite(&ac,sizeof(ac),1,fp);
            break;
            }
            }
            cout << "\n\n Find another account..... (Y/N)  ";
            fflush(stdin);
            option = getchar();
            }
            break;

     //Delete account...............
           case '6':
            system("cls");
            option = 'Y';
            while (option == 'Y'|| option == 'y')
            {
            cout << "\n Enter the account no to delete : ";
            cin >> n;
            ft = fopen("temp.dat", "wb");
            rewind(fp);
            while (fread (&ac, sizeof(ac),1,fp) == 1)
            if (ac.retacno()!= n)
            {
            fwrite(&ac,sizeof(ac),1,ft);
            }
            fclose(fp);
            fclose(ft);
            remove("users.txt");
            rename("temp.dat","users.txt");
            fp=fopen("users.txt","rb+");
            cout << "\n Delete another account... (Y/N) ";
            fflush(stdin);
            option = getchar();
            }
            break;

            case '7':
            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;
}

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...