You Are Reading

0

C++ Lecture: 15

Unknown Monday 30 July 2012 ,
Lecture 15
Overloading Operator
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
    int a=4;
    cout << "It is a condition check \n";
    cout <<"It is a true : "<< (a>1 || a==4) << endl;
    cout <<"It is a false : "<< (a>1 || a!=4) << endl;
  getch();
  return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
      Run program Ctrl+F10
Q) A program that takes a four digits integer from user and shows the digits on the screen separately i.e. if user enters 7531, it displays 7,5,3,1 separately.
Ans:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
    // declare variables
    int number, digit;
    // prompt the user for input
    cout << "Please enter 4-digit number:";
    cin >> number;
    // get the first digit and display it on screen
    digit = number % 10;
    cout << "The digits are: ";
    cout << digit << ", ";
    // get the remaining two digits number
    number = number / 10;
    // get the next digit and display it
    digit = number % 10;
    cout << digit << ", ";
    // get the remaining two digits number
    number = number / 10;
   
    // get the next digit and display it
    digit = number % 10;
    cout << digit << ", ";
    // get the remaining two digits number
    number = number / 10;
   
        // get the next digit and display it
    digit = number % 10;
    cout << digit << ", ";
    // get the remaining two digits number
    number = number / 10;
  getch();
  return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
      Run program Ctrl+F10
Q) Following program takes the radius of a circle from the user and calculates the
diameter, circumference and area of the circle and displays the result.
Ans:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
    // declare variables
   float radius, diameter, circumference, area;
   // prompt the user for radius of a circle
   cout << "Please enter the radius of the circle " ;
   cin >> radius ;
   // calculate the diameter, circumference and area of the circle
   // implementing formula i.e. diameter = 2 r circumference = 2 ç r and area = ç r2
   diameter = radius * 2 ;
   circumference = 2 * 3.14 * radius ; // 3.14 is the value of ç (Pi)
   area = 3.14 * radius * radius ;
   // display the results
   cout << "The diameter of the circle is : " << diameter ;
   cout << "\n  circumference of the circle is : " << circumference ;
   cout << "\n  area of the circle is : " << area ;
  getch();
  return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
      Run program Ctrl+F10

0 comments:

Post a Comment

 
Copyright 2010 Learn Dev C++