C++ Lec 3
C++ Lec 3
C++ programming
3 Computer fundamentals and programming
cmath
4 Computer fundamentals and programming
𝑡𝑎𝑛−1
53 → 𝑝𝑜𝑤(5, 3)
(ln 𝑎)
5 Computer fundamentals and programming
#include<iostream>
#include<cmath>
using namespace std;
Main(){
double Y1, Y2, Y3, Y4, Y5,Y6,Y7,Y8,Y9,Y10;
Y1=sin (10);
Y2=cos(3);
Y3=tan(5);
Y4=atan(3);
Y5=cosh(2);
Y6=sqrt(4);
Y7=pow(2,3);
Y8=exp(3);
Y9=log(7);
Y10=log10(9);
cout<<cout<<"Y1="<<Y1<<"\nY2="<<Y2<<"\nY3="<<Y3<<"\nY4="<<Y4<<"\nY5="<<Y5<<"\nY6="<<Y6<<"\nY7=
"<<Y7<<"\nY8="<<Y8<<"\nY9="<<Y9<<"\nY10="<<Y10<<endl;}
6 Computer fundamentals and programming
C++ programming
5 Computer fundamentals and programming
Ex2:
write a c++ program to implement the following function
Y=sin(7)+ln(5)-𝟒𝟖
#include<iostream>
#include<cmath>
using namespace std;
main(){
double Y;
Y=sin (7)+log(5)-pow(4,8);
cout<<“Y=“<<Y<<endl;
}
8 Computer fundamentals and programming
// Calculating powers with the standard function pow()
#include <iostream> // Declaration of cout
#include <cmath> // Prototype of pow(), thus: Screen output
// double pow( double, double);
2.5 raised to the power 3 yields:
using namespace std;
15.625
int main()
2 + (5 raised to the power 2.5) yields:
{
57.9017
double x = 2.5, y;
// By means of a prototype, the compiler generates the correct call or an error
message! Computes x raised to the power 3:
y = pow("x", 3.0); // Error! String is not a number
y = pow(x + 3.0); // Error! Just one argument
y = pow(x, 3.0); // ok!
y = pow(x, 3); // ok! The compiler converts the
// int value 3 to double.
cout << "2.5 raised to 3 yields: "
<< y << endl;
// Calculating with pow() is possible:
cout << "2 + (5 raised to the power 2.5) yields: "
<< 2.0 + pow(5.0, x) << endl;
return 0;}
9 #include<iostream>
Computer fundamentals and programming #include<iomanip> c
using namespace std;
#include<iomanip> int main () 1 2 3 ….. 50
The iomanip is a parameterized input output manipulator. In order {
to access manipulators that take parameters the header file int i=10; Different
<iomanip> is included in the program. Some of the manipulators double a=78.121113; a(50) initial value
are:- char c[50];
• setbase(int n) – It sets the output representation of the number char c1='p';
cout << "Enter your name " << endl;
to octal, decimal or hexadecimal corresponding to the cin >> c;
argument n which is 8 in case of octal, 10 for decimal and 16 cout << "Hexedecimal number : " << setbase(16) << i << endl;
for hexadecimal. Any other value will not change the base. cout << "Octal number : " << setbase(8) << i<< endl;
• setw(int n) – It sets the width of the field of the next output cout << setw(10) << c << endl;
to n characters. If the length of the output stream is less than cout << setw(15) << c << endl;
n then spaces are padded. The no of spaces padded is the cout << setfill(c1) << setw(12) << c << endl;
difference between n and length of the output stream. If the cout << setfill(c1) << setw(2) << c << endl;
length of the output stream is less than n there will be no cout << setprecision(5) << a << endl;
cout << setprecision(7) << a << endl;
effect on output stream. Here is a program which illustrates
return(0);}
the working of input output manipulators. Enter your name
• setfill (char c) – It sets the fill character to be the value of Ahmed pppppppahmed
character c. The fill character is used in output stream for Hexedecimal number : a ahmed
padding. Octal number : 12
• setprecision(int n) – It sets the precision of floating point ahmed 78.121
number to n digits. ahmed 78.12111
10 Computer fundamentals and programming
O/P
Enter two floating-point values: 4.75 12.3456
The average of the two numbers is: 8.5478
11 Computer fundamentals and programming
➢ Divisions performed with integral operands will produce integral # include <iostream.h>
results; for example, 7/2 computes to 3. If at least one of the main(){
operands is a floating-point number, the result will also be a cout<<“7/2” <<7/2”\n”
floating-point number; e.g., the division 7.0/2 produces an exact cout<<“7%2”<<7%2”\n”}
result of 3.5. }
➢ Remainder division is only applicable to integral operands and
returns the remainder of an integral division. For example, 7%2
computes to 1.
11 Computer fundamentals and programming
Sign Operators
The sign operator – returns the value of the operand but inverts the
sign.
Example: int n = –5; cout << -n; // Output: 5
The sign operator + performs no useful operation, simply returning the
value of its operand.
Increment / Decrement Operators
The increment operator ++ modifies the operand by adding 1 to its value
and cannot be used with constants for this reason. Given that i is a variable,
both i++ (postfix notation) and ++i (prefix notation) raise the value of i
by 1. In both cases the operation i = i + 1 is performed. However, prefix
++ and postfix ++ are two different operators. The difference becomes
apparent when you look at the value of the expression; ++i means that the
value of i has already been incremented by 1, whereas the expression i++
retains the original value of i. This is an important difference if ++i or i++
forms part of a more complex expression: ++i i is incremented first and
the new value of i is then applied, i++ the original value of i is applied
before i is incremented. The decrement operator -- modifies the operand
by reducing the value of the operand by 1. As the sample program opposite
shows, prefix or postfix notation can be used with --.