Friday, November 8, 2013

Simple Programs in C++

This Program to demonstrate simple output using cout statements:
#include <iostream.h>
main()
{
cout << "Frankly, my dear...\n";
cout << "C++ is a better than C \n";
return 0;
}

This program is an example to perform ‘input’ and ‘output’ operations
#include <iostream.h>
int main()
{
int bushels;
float dollars, rate;
cout << "How many dollars did you get? quot;;
cin >> dollars;
cout << "For how many bushels? ";
cin >> bushels;
rate = dollars / bushels;
cout.precision(2);
cout << "You have received quot; << rate << " for each bushel.\n";
return 0;
}

This program calculates a purchase slip
#include <iostream.h>
float tax (float) ;
int main()
{
float purchase, tax_amt, total;
cout << "\nAmount of purchase: ";
cin >> purchase;
tax_amt = tax(purchase);
total = purchase + tax_amt;
cout.precision(2);
cout << "\nPurchase is: " << purchase;
cout << "\nTax: " << tax_amt;
cout << "\nTotal: " << total;
return 0;
}
float tax (float amount)
{
float rate = 0.065;
return(amount * rate);
}

A simple exercise on C++ program to perform a calculation
#include <iostream.h>
int main()
{
int pounds;
int total, bags;
pounds = 50;
bags = 1000;
total = bags * pounds;
cout << "There are " << total << "lbs. in 1000 bags of beans\n";
return 0;
}

This program performs to find out fractions of numerator and denominator
#include <iostream.h>
int main()
{
float num, denom; // numerator and denominator of fraction
float value; // value of fraction as decimal
cout << "Convert a fraction to a decimal\n";
cout << "Numerator: ";
cin >> num;
cout << "Denominator: ";
cin >> denom;
value = num / denom; // convert fraction to decimal
cout << '\n' << num << '/' << denom << '=' << value;
return 0;
}

This program performs to find out fractions of numerator and denominator
#include <iostream.h>
int main()
{
int num = 3, denom = 4;
float value;
value = num / denom;
cout << value;
return 0;
}

This Program demonstrates how to increment values in the C++ program
#include <iostream.h>
int main()
{
int val = 1;
cout << "\nval is " << val++ << " and then post-incremented\n";
cout << "val is now " << val << "\n";
cout << "val is pre-incremented to " << ++val << '\n';
return 0;
}

This Program demonstrates the symbols and values of different operations
#include <iostream.h>
int main()
{
cout << "1 & 1 is " << (1 & 1) << '\n';
cout << "1 | 1 is " << (1 | 1) << '\n';
cout << "1 ^ 1 is " << (1 ^ 1) << '\n';
cout << "255 << 2 is " << (255 << 2)<< '\n';
cout << "255 >> 2 is "<< (255 >> 2) << '\n';
cout << "~255 is " << ~(unsigned int)255 << '\n';
return 0;
}

No comments:

Post a Comment