CIT 113 Practicals
CIT 113 Practicals
Step 1 :
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in
double quotes
cout << "This is C++
Programming";
return 0;
}
Output
This is C++ Programming
How does this program work?
We first include the iostream header file
the std namespace. To use
the stdnamespace, we used the using
namespace std; statement.
Every C++ program starts with
#include <iostream>
int main() {
// prints the string enclosed in
double quotes
std::cout << "This is C++
Programming";
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
cout << num1 << endl; // print
integer
cout << num2 << endl; // print
double
cout << "character: " << ch <<
endl; // print char
return 0;
}
Output
70
256.783
character: A
Notes:
The endl manipulator is used to insert a
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num; // Taking input
cout << "The number is: " << num;
return 0;
}
Output
Enter an integer: 70
The number is: 70
In the program, we used
#include <iostream>
using namespace std;
int main() {
char a;
int num;
cout << "Enter a character and an
integer: ";
cin >> a >> num;
return 0;
}
Output
Enter a character and an integer: F
23
Character: F
Number: 23
Practical No 6
C Arithmetic Operators
An arithmetic operator performs mathematical
operations such as addition, subtraction,
multiplication, division etc on numerical values
(constants and variables).
* multiplication
/ division
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes.
These two operators can also be used as postfixes
like a++ and a--. Visit this page to learn more
about how increment and decrement operators
work when used as postfix.
C Assignment Operators
An assignment operator is used for assigning a
value to a variable. The most common assignment
operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
A relational operator checks the relationship
between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision
making and loops.
Meaning of
Operator Example
Operator
5 == 3 is evaluated
== Equal to
to 0
5 > 3 is evaluated
> Greater than
to 1
5 < 3 is evaluated
< Less than
to 0
to 1
printf("%d == %d is %d \n", a, b, a
== b);
printf("%d == %d is %d \n", a, c, a
== c);
printf("%d > %d is %d \n", a, b, a >
b);
printf("%d > %d is %d \n", a, c, a >
c);
printf("%d < %d is %d \n", a, b, a <
b);
printf("%d < %d is %d \n", a, c, a <
c);
printf("%d != %d is %d \n", a, b, a !
= b);
printf("%d != %d is %d \n", a, c, a !
= c);
printf("%d >= %d is %d \n", a, b, a
>= b);
printf("%d >= %d is %d \n", a, c, a
>= c);
printf("%d <= %d is %d \n", a, b, a
<= b);
printf("%d <= %d is %d \n", a, c, a
<= c);
return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns
either 0 or 1 depending upon whether expression
results true or false. Logical operators are
commonly used in decision making in C
programming.
Operator Meaning Example
If c = 5 and d = 2 then,
Logical AND.
expression ((c==5)
&& True only if all
&& (d>5)) equals to
operands are true
0.
1.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
(a == b) && (c > 5) evaluates to 1 because
C Bitwise Operators
During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc
are converted to bit-level which makes processing
faster and saves power.
Bitwise operators are used in C programming to
perform bit-level operations.
| Bitwise OR
Operators Meaning of operators
^ Bitwise exclusive OR
~ Bitwise complement
Other Operators
Comma Operator
Comma operators are used to link related
expressions together. For example:
int a, c = 5, d;
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\
n",sizeof(a));
printf("Size of float=%lu bytes\
n",sizeof(b));
printf("Size of double=%lu bytes\
n",sizeof(c));
printf("Size of char=%lu byte\
n",sizeof(d));
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte