SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
C++ Basics
Exercise:
Self-test Exercises
Question:17 | ISBN:9780321531346 | Edition: 7

Question

What is the output of the following program lines (when embedded in a correct program that declares number to be of type int)?

number = (1/3) * 3;

cout << "(1/3) * 3 is equal to " << number;

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
using namespace std;

int main()
{
	//declares number to be of type int
  int number;
  //Calculate the value and store result in number
  number= (1/3) * 3;
  //Print output
cout << "(1/3) * 3 is equal to " << number;
cout<<endl;
 system("pause"); 
 return 0;
}

Output:

(1/3) * 3 is equal to 0
 

Explanation:

Declared the variable name, "number" with the data type is "int(integer)". the variable, number accepts only intger values, not accept decimal values in any case. So, the actuall output of the (1/3) * 3=0.999, but display the result only 0.

1 0

Discussions

Post the discussion to improve the above solution.