Lab2

Home Up

 

 Lab1 ] [ Lab2 ] Lab3 ] lab4 ] lab5 ] lab6 ] Lab7 ] Lab8 ] Lab9 ] Lab10 ]

CP107 LAB 2

 

LAB OBJECTIVES:

Learn binary, hex and decimal representations

Learn how to convert a number to another numbering system

Use cin to read integer into a variable

Use simple if statements

 

LAB ASSIGNMENTS:

LAB 2A
Write a program that reads an integer and determines and prints whether it is odd or even. (Hint: Use the modulus operator (page 31 of text) An even number is a multiple of 2. Any multiple of 2 (even number) leaves a remainder of zero when divided by 2).

Submit the program as even.cpp

 

LAB 2B
Write a program that prompts the user to enter a decimal integer in the range of 8 through 15. Test the value just read in and only proceed if it falls within that range. The program will then convert and display the equivalent value in octal, hexadecimal and binary notation. Octal and hex representations will be done similar to example below. Binary conversion will be done in 4 steps of division by 2 (repetitive division by 2), saving the remainder in a variable as described in the Lecture 2 document. The 4 remainder values saved in variables will then be displayed in reverse order of construction.

Submit the program as num.cpp

#include <iostream>
using
namespace std;

int main()

{

    int x = 9;
    cout << "value of 9 in decimal is " << dec << x << endl; // print decimal value
    cout << "value of 9 in octal is " << oct << x << endl; // print octal value
    cout << "value of 9 in hex is " << hex << x << endl; // print hexadecimal value

    / / do calculation to convert 9 into binary
   
//cout << "value of 100 in binary is " << a << b << c << d << endl; // print hexadecimal value

    return 0;

}

 

 

 

Copyright (c) Yusuf Family Website 2001