Chapter: Simple Programs (C++)

INDEX


//A simple Hello program
#include<iostream>


using namespace std;


int main()

{
    cout<<"Hello!!! Welcome to C++";

    return 0;
}

TOP

//Accept two integers and display their average
#include<iostream>

using namespace std;

int main()
{
    int n1,n2;
    float result;

    cout<<endl<<"Enter 1st Integer: ";
    cin>>n1;
    cout<<"Enter 2nd Integer: ";
    cin>>n2;

    result=(n1+n2)/2.0;

    cout<<"average  = "<<result;

    return 0;
};

TOP

//A program to aceept temperature in degree celsius and displays its farenheit equivalent
#include<iostream>

using namespace std;

int main()
{
    float c, f;

    cout<<endl<<"Enter temperature in Celsius: ";
    cin>>c;
   
    f=(9*c)/5+32;

    cout<<c<<" degree celsius = "<<f<<" degree farenheit.";
   
    return 0;
};

TOP

/*SWAP PROGRAM (Using 3rd Variable): Accept two integers, excahnge their values and then display their new values.
example:
Enter Integer1: 5
Enter Integer2: 7

Values Before Swap/Exchange: Integer1: 5 and Integer2: 7

Values After Swap/Exchange: Integer1: 7 and Integer2: 5

*/
#include<iostream>

using namesapce std;

int main()
{
    int a,b,c;
    cout<<endl<<"Enter Integer1: ";
    cin>>a;
    cout<<endl<<"Enter Integer2: ";
    cin>>b;

    cout<<endl<<"Values Before Swap/Exchange: Integer1: "<<a <<" and Integer2: "<<b;
    c=a;
    a=b;
    b=c;

    cout<<endl<<"Values After Swap/Exchange: Integer1: "<<a <<" and Integer2: "<<b;   

    return 0;
};


TOP

/*SWAP PROGRAM (Without Using 3rd Variable): Accept two integers, excahnge their values and then display their new values.
example:
Enter Integer1: 5
Enter Integer2: 7

Values Before Swap/Exchange: Integer1: 5 and Integer2: 7

Values After Swap/Exchange: Integer1: 7 and Integer2: 5

*/
#include<iostream>

using namesapce std;

int main()
{
    int a,b,c;
    cout<<endl<<"Enter Integer1: ";
    cin>>a;
    cout<<endl<<"Enter Integer2: ";
    cin>>b;

    cout<<endl<<"Values Before Swap/Exchange: Integer1: "<<a <<" and Integer2: "<<b;
    a=a+b;
    b=a-b;
    a=a-b;

    cout<<endl<<"Values After Swap/Exchange: Integer1: "<<a <<" and Integer2: "<<b;   

    return 0;
};


TOP

/*Accept first name, middle name and lastname seperately and then display the full name
example:
Enter First Name: Sachin
Enter Middle Name: Ramesh
Enter Last Name: Tendulkar
Full Name: Sachin Ramesh Tendulkar
*/

#include<iostream>
#include<stdio.h>

using namespace std;

int main()
{
    char fname[25], mname[25], lname[25];
   
    cout<<endl<<"Enter First Name: ";
    gets(fname);
    cout<<"Enter Middle Name: ";
    gets(mname);
    cout<<"Enter Last Name: ";
    gets(lname);

    cout<<"Full Name: "<<fname<<" "<<mname<<" "<<lname;
    return 0;
};


TOP

/*Accept principal, rate of interest and time period, then display the compound interest and the amount earned.
example:
Principal Money: 1000
Rate of Interest(per annum): 10
Time period (in years): 3
Calculating for Interest Compounded Annually
Interest Earned: 331
Amount: 1331
*/

#include<iostream>
#include<math.h>

using namespace std;

int main()
{
    float p,r,t,ci,amt;

    cout<<endl<<"Principal Money: ";
    cin>>p;
    cout<<"Rate of Interest (per annum): ";
    cin>>r;
    cout<<"Time period (in years): ";
    cin>>t;
   
    cout<<"Calculating for Interest Compounded Annually ...";
    amt=p*pow((1+r/100),t);
    ci=amt-p;

    cout<<endl<<"Interest Earned: "<<ci;
    cout<<endl<<"Amount: "<<amt;

    return 0;
};

TOP

/*A program to aceept marks of a student in five subjects - English, Physics, Chemistry, Mathematics, Computers
and then display the total marks and percentage. Assuming maximum marks for each subject is 80.*/

#include<iostream>

int main()
{
    float eng, phy, chem, math, comp, total, percent;
   
    cout<<endl<<"Enter the Marks in Respective Subjects.";
    cout<<endl<<"ENGLISH (out of 80): ";
    cin>>eng;
    cout<<"PHYSICS (out of 80): ";
    cin>>phy;
    cout<<"CHEMISTRY (out of 80): ";
    cin>>chem;
    cout<<"MATHEMATICS (out of 80): ";
    cin>>math;
    cout<<"COMPUTER (out of 80): ";
    cin>>comp;

    total=eng+phy+chem+math+comp;
    percent=total*100/400;

    cout<<endl<<"Total Marks (out of 400): "<<total;
    cout<<endl<<"Percentage Secured: "<<percent<<" %";

    return 0;
};

TOP