Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ I/O and Arithmetic Operations, Exams of C programming

The concept of streams in c++ for performing input and output operations. It covers cin and cout, i/o manipulators, and arithmetic operators. Code examples and explanations of how to use these features in c++.

Typology: Exams

2021/2022

Uploaded on 09/12/2022

ekaraj
ekaraj ๐Ÿ‡บ๐Ÿ‡ธ

4.6

(29)

264 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Cin and Cout: Streams
โ— C++ uses ๎˜streams
๎˜
to perform input and output operations.
โ— A stream is an entity where a program can either insert or extract characters to/from.
โ— Streams are a source/destination of characters
โ— These characters are provided/accepted sequentially
stream
description
cin
standard input stream
cout
standard output stream
๎˜ Standard output (cout)
โ— Cout is used together with the ๎˜insertion operator
๎˜
, <<.
#include ๎˜<iostream>
using namespace ๎˜std๎˜;
int ๎˜main()
{
cout ๎˜<< ๎˜"Hello World! "๎˜;
}
โ— The << operator inserts the data that follows it into the stream that precedes it.
Multiple insertion operations (<<) may be chained in a single statement:
Standard input (cin)
โ— Cin is used together with the extraction operator, which is written as >>
โ— The extraction operation on cin uses the type of the variable to determine how it
interprets the characters read from the input.
#include ๎˜<iostream>
using namespace ๎˜std๎˜;
int ๎˜main()
{
๎˜float ๎˜x,y;
cout ๎˜<< ๎˜"Please enter a value for x: "๎˜;
cin ๎˜>> ๎˜x;
cout ๎˜<< ๎˜"Please enter a value for y: "๎˜;
cin ๎˜>> ๎˜y;
cout ๎˜<< ๎˜"Values of x and y are:๎˜\n๎˜"๎˜;
cout ๎˜<< ๎˜"๎˜\t๎˜x = " ๎˜<< ๎˜x ๎˜<< ๎˜" ๎˜\t๎˜y = " ๎˜<< ๎˜y ๎˜<< ๎˜'๎˜\n๎˜'๎˜;
pf3
pf4

Partial preview of the text

Download C++ I/O and Arithmetic Operations and more Exams C programming in PDF only on Docsity!

1. Cin and Cout: Streams

โ— C++ uses streams to perform input and output operations.

โ— A stream is an entity where a program can either insert or extract characters to/from.

โ— Streams are a source/destination of characters

โ— These characters are provided/accepted sequentially

stream description

cin standard input stream

cout standard output stream

Standard output (cout)

โ— Cout is used together with the insertion operator , <<.

#include using namespace std;

int main() {

cout << "Hello World! " ;

}

โ— The << operator inserts the data that follows it into the stream that precedes it.

Multiple insertion operations (<<) may be chained in a single statement:

Standard input (cin)

โ— Cin is used together with the extraction operator, which is written as >>

โ— The extraction operation on cin uses the type of the variable to determine how it

interprets the characters read from the input.

#include using namespace std;

int main() { float x,y;

cout << "Please enter a value for x: " ; cin >> x; cout << "Please enter a value for y: " ; cin >> y; cout << "Values of x and y are:\n" ; cout << "\tx = " << x << " \ty = " << y << '\n' ;

2. I/O Manipulators

โ— Setprecision:

โ—‹ Sets the decimal precision to be used to format floating-point values on output

operations.

โ— Fixed:

โ—‹ Floating-point values are written using fixed-point notation

โ—‹ The value is represented with exactly as many digits in the decimal part as

specified by the precision field

โ— Showpoint:

โ—‹ The decimal point is always written for floating point values inserted into the

stream

โ—‹ Even for those whose decimal part is zero

โ— Scientific:

โ—‹ Floating-point values are written using scientific notation

โ—‹ The value is represented always with only one digit before the decimal point,

โ—‹ Decimal point is followed by as many decimal digits as the precision field

#include // std::cout, std::fixed #include // std::setprecision

int main () { double f =3.14159; double d = 2; std::cout << std::setprecision(5) << f << '\n'; std::cout << std::setprecision(9) << f << '\n'; std::cout << std::setprecision(9) << d << '\n'; //std::cout << std::showpoint; std::cout << std::showpoint << std::setprecision(5) << f << '\n'; std::cout << std::showpoint << std::setprecision(9) << f << '\n'; std::cout << std::showpoint << std::setprecision(9) << d << '\n'; //std::cout << std::fixed; std::cout << std::fixed << std::setprecision(5) << f << '\n'; std::cout << std::fixed << std::setprecision(9) << f << '\n'; std::cout << std::fixed << std::setprecision(9) << d << '\n'; std::cout << std::scientific << f << '\n'; std::cout << std::scientific << f << '\n'; std::cout << std::scientific << d << '\n'; return 0; }

Output:

2

product*=y; quotient = x; quotient/=y; rem = x; rem%=y; cout << "The sum is " << sum << endl; cout << "The diff is " << diff << endl; cout << "The product is " << product << endl; cout << "The quotient is " << quotient << endl; cout << "The rem is " << rem << endl; }

5. Recitation Quiz Question:

Read in 2 integers. Print the difference between the two.

6. Recitation Quiz Question:

Read in 2 integers. Print the quotient when the first number is divided by the

second.