Task #1: What Are Increment & Decrement Operator? Explain With Proper Example.

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Task #1:

What are Increment & decrement operator? Explain with proper example.
INCREMENT OPERATOR:
Increment operators are used to increase the value of the variable by one.

Syntax: Increment operator: ++var_name; (or) var_name++;


Here  ++i returns the value after it is incremented, while i++ return the value before it is
incremented. At the end, in both cases the i will have its value incremented.

DECREMENT OPERATOR:
Decrement operators are used todecrease the value of the variable by one in C programs.

Syntax: Decrement operator: – -var_name; (or) var_name – -;

Here i-- passes i to the function then decrements i value. i-1 passes i-1 to the function and does


not change i value.

Task #2:

What are Assignment Statements/Operators? Explain with proper example.

ASSIGNMENT OPERATORs:
Assignment operators are used to assigning value to a variable. The left side operand of the
assignment operator is a variable and right side operand of the assignment operator is a value.
The value on the right side must be of the same data-type of the variable on the left side
otherwise the compiler will raise an error.Here are some assignment operators/assignment.

= Simple assignment operator. Assigns values from right sideoperands to left side operand

+= Add AND assignment operator. It adds the right operand to the leftoperand and assign the
result to the left operand.

-= Subtract AND assignment operator. It subtracts the right operandfrom the left operand and
assigns the result to the left operand.
/= Divide AND assignment operator. It divides the left operand with theright operand and
assigns the result to the left operand.

%= Modulus AND assignment operator. It takes modulus using twooperands and assigns the
result to the left operand.

<<= Left shift AND assignment operator.

>>= Right shift AND assignment operator

&= Bitwise AND assignment operator

^= Bitwise exclusive OR and assignment operator.

|= Bitwise inclusive OR and assignment operator.

CODE:
#include <iostream>
using namespace std;
int main()
{
int a = 21;
int c ;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c = a;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c += a;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c -= a;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c *= a;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c /= a;
cout<<" Value of c = "<<c<<"\n";
c = 200;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c %= a;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c <<= 2;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c >>= 2;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c &= 2;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c ^= 2;
cout<<" Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c |= 2;
cout<<"( c |= 2) Value of c = "<<c<<"\n";
return 0;
}
Task #3:
Create the source file and observe the output of the following program?

CODE:
# include<iostream>
# include<iomanip>
Using namespace std;
main ()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<" City "<<"\t No. of Schools";
cout<<"\n Durban \t"<<Durban;
cout<<"\n Moscow \t"<<Moscow;
cout<<"\n Sydney \t"<<Sydney;
cout<<"\n Paris \t"<<Paris;
}

Task# 4:
Create the source file and observe the output of the following program?

CODE:
# include<iostream>
# include<iomanip>
Using namespace std;
main ()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<setw(10)<<" City "<<setw(30)<<" No. of Schoolss \n";
cout<<setw(10)<<" Durban"<<setw(20)<<Durban<<"\n";
cout<<setw(10)<<" Moscow"<<setw(20)<<Moscow<<"\n";
cout<<setw(10)<<" Sydney"<<setw(20)<<Sydney<<"\n";
cout<<setw(10)<<" Paris"<<setw(20)<<Paris<<"\n";
}

Task #5:
Create the source file and observe the output of the following program?

CODE:
#include <iostream>
Using namespace std;
main ()
{
int a = 21;
int c ;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c = a;
cout<<" (c = a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c += a;
cout<<"(c += a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c -= a;
cout<<"(c -= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c *= a;
cout<<"(c *= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c /= a;
cout<<"(c /= a) Value of c = "<<c<<"\n";
c = 200;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c %= a;
cout<<"(c %= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c <<= 2;
cout<<"(c <<= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c >>= 2;
cout<<"(c >>= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c &= 2;
cout<<"(c &= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c ^= 2;
cout<<"(c ^= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c |= 2;
cout<<"( c |= 2) Value of c = "<<c<<"\n";
}

Task #6:
Write a program to solve the following algebraic expression that takes the
values of variables from user.
(Take power of variable from power function)
3x.x+2 / 4a+1
CODE:

You might also like