some simple cpp program example for practice

The C ++ program may seem a bit rough at first, but if you try to look at its syntax and its logic, you will find C ++ and another computer language easy to do, so all you have to do is design its algorithm and flowchart and  the datatype and where variable. And the function comes in. Then you see how many times one sentence gets repeated. You just have to make up your mind if you do not feel like traveling.

some simple cpp program

write a function max() to find out maximum of three integer parameter. the function should return maximum of the three parameters. use function prototype.





 #include<iostream.h>
 #include<conio.h>
    int max(int,int ,int);

 int main()
{
   double num1,num2,num3;

cout<<"number1=";
cin>>num1;


cout<<"number2=";
cin>>num2;


cout<<"number3=";
cin>>num3;

     int result=max(num1,num2,num3);

cout<<"max is="<<result;

getch();
return 0;
}

    int max(int num1,int num2,int num3)
   { int maximum;

if(num1>=num2&&num1>=num3)
{
maximum=num1;
}

else if(num2>=num1&&num2>=num3)
{
maximum=num2;
}

else{

maximum=num3;
}
return maximum;
}
output
number1=345
number2=5
number3=398
max is=398

2.create a classs book with the datamembers bookno,pageno,title,and author write appropriate member functions getbook()-to read all book details from user and showbook();.also create functions- create one object of this class and show usage of the member functions
#include<iostream.h>
#include<conio.h>
class book
{
int bookno,pageno;
char title[20],author[20];
public:
void getbook();
void showbook();
};

void book::getbook()

        {
        cout<<"book no=";
        cin>>bookno;


        cout<<"page no=";
        cin>>pageno;


        cout<<"Title=";
        cin>>title;


        cout<<"author=";
        cin>>author;
        }

void book::showbook()

         {
         cout<<"\n\t"<<bookno;
         cout<<"\n\t"<<pageno;
         cout<<"\n\t"<<title;
         cout<<"\n\t"<<author;
         }

        int main()
{
clrscr();
book b;
b.getbook();
b.showbook();


getch();
return 0;
}
output
book no=1
page no=23
Title=cpp
author=bjarnestroupstrup

1
23
cpp
bjarnestroupstrup
create a class student with the datamembers rollno,mark1,mark2,mark3,totalmarks and percentage.
write appropriate member function readstudent()-to read rollno,mark1,mark2 and
mark3 from user AND showstudent().also create function_calculatetotal() to
calculate totalmarks and calculatepercentage() to calculate percentage.
create one object of this class and show usage of the member function


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

class student

{
int roll_no,m1,m2,m3,total_mark;
float percentage;

public:

    void readstu();
    void showstu();
    void calculate_total();
    void calculate_percentage();
};

void student::readstu()

    {
    cout<<"roll number=";
    cin>>roll_no;

    cout<<" mark1=";
    cin>>m1;


    cout<<" mark2=";
    cin>>m2;


    cout<<" mark3=";
    cin>>m3;
    }

void student::showstu()

    {
    cout<<"\n roll number="<<roll_no;
    cout<<"\n mark1="<<m1;
    cout<<"\n mark2="<<m2;
    cout<<"\n mark3="<<m3<<endl<<endl;
    }

void student::calculate_total()

    {
    total_mark=m1+m2+m3;
    cout<<"total mark="<<total_mark<<endl;
    }

void student::calculate_percentage()

    {
    percentage=(float)total_mark/3;
    cout<<"percentage="<<percentage<<endl;
    }

int main()

    {
    clrscr();

    student s;
    s.readstu();
    s.showstu();
    s.calculate_total();
    s.calculate_percentage();

getch();
return 0;
    }
output
    roll number=12
    mark1=56
    mark2=76
    mark3=45


    roll number=12
    mark1=56
    mark2=76
    mark3=45

     total mark=177   
   percentage=59  

create a class time having data members hour, minute and second. provid readtime() and printtime()functions. the printtime() function should print time in appropriate formate 12:40:01
#include<iostream.h>
#include<conio.h>

class time

{
int hour,minute;
float second;

public:

void readtime();
void printtime();
};

void time::readtime()

    {
    hour=12;
    minute=40;
    second=1;
    }

void time::printtime()

    {
    cout<<""<<hour<<":"<<minute<<":"<<second;
    }

int main()

{
clrscr();

time t1;
t1.readtime();
t1.printtime();

getch();
return 0;
}
output
12:40:1
write a program to calculate the sum of first 12 natural number
#include<iostream.h>
#include<conio.h>
int main()
{
int i=1,sum=0;

while(i<=12)
    {
    sum+=i;
    i++;
    }
    cout<<"sum="<<sum;
getch();
return 0;
}
output
sum=78


PREVIEW                                                        HOME                                                     NEXT

Comments

Popular posts from this blog

Latest random post with thumbnail widget HTML / JAVASCRIPT

simple php program upload file in database