Professional Documents
Culture Documents
Semester – I I
#include <iostream.h>
/* Use of arithmetic operator */
main()
{
float result;
result = 1.0 + 2.0 * 3.0 / 4.0;
cout << '\n' << result;
result = 1.0 / 2.0 + 3.0;
cout << '\n' << result;
#include <iostream.h>
/* Use of incremental operator */
int main()
{
int val = 1;
cout << "\n val is " << val++ << " and then
post- incremented \n";
cout << "val is now " << val << "\n";
cout << "val is pre-incremented to " <<
++val << '\n';
Copyright@ Amity University
Example of Incremental / Decrement Operators ...
cout << "\n val is " << val++ << " and then
post- decremented \n";
cout << "val is now " << val << "\n";
cout << "val is pre-decremented to " <<
++val << '\n';
return 0;
}
#include <iostream.h>
/* Use of Relational Operators */
int main()
{
int first, second;
cout << "\nInput two numbers ";
cin >> first >> second;
cout << "First > second has the value "<<
(first > second) << '\n';
cout << "first < second has the value " <<
(first < second) << '\n';
Copyright@ Amity University
Example of Relational Operators ...
cout<<'\n'<< Section<<RollNo<<Marks;
return 0;
}
Copyright@ Amity University
One use for typecasting for is when you want to use
the ASCII characters. For example, what if you want to
create your own chart of all 256 ASCII characters. To
do this, you will need to use to typecast to allow you to
print out the integer as its character equivalent.
int x = 3; int y = 4;
if (x > y) {
cout << "x is bigger than y" << endl;
}
else {
cout << "x is smaller than y" << endl;
}
return 0;
}
Copyright@ Amity University
Example of If condition...
#include <iostream.h>
int main()
{
int your_number;
cout << "Enter a whole number: ";
cin >> your_number;
if (your_number % 2 == 0)
cout << "\nYour number is even\n";
return 0;
}
for(j=1;j<=i;j++)
{
cout<<"*";
}
}
getch();
}
Output :
*
**
***
****
*****
******
*******
********
*********
int main()
{
int number = 1, total = 0;
while (number < 11)
{
total += number;
number++;
}
return 0;
}
if(ch!=‘\n’)
continue;
line++;
}
return 0;
}
float marks[50]
int a[10];
a[5] = 47;
Notice that in the function definition, the braces replace the semicolon.
Since braces surround a statement or group of statements, you don’t
need a semicolon. Notice also that the arguments in the function
definition must have names if you want to use the arguments in the
function body (since they are never used here, they are optional).
Call By Value
Call By Reference
method is that you cannot alter the variable that are used to call
#include <iostream>
void main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
for(int i = 0; i < 10; i++)
{
cout << "a[" << i << "] = " << a[i] << endl;
}
}
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float marks[50];
(for(int i=0;i<50;i++)
{
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[2][5]={
{‘A’,’M’,’I’,’T’},
{‘A’,’M’,’R’,’I’,’T’}
};
#include<iostream.h>
int add(int,int);
void main()
{
int a=10;
int b=20;
int total=add(a,b);
cout<<total;
}
Copyright@ Amity University
Example of Function Declaration & Definition ...
int add(x,y)
{
int c=x+y;
return c;
}
#include <iostream.h>
int main()
{
int orig=10;
cout<<“The original value is”<<orig<<“/n”;
change(orig);
cout<<“The value after function change() is
over” <<orig<<“/n”;
Copyright@ Amity University
Example Call By Value ...
return 0;
}
void change(int orig)
{
orig=20; cout<< Value of orig in function
change is”<<orig;
return;
}
#include <iostream.h>
int main()
{
int orig=10;
cout<<“The original value is”<<orig<<“/n”;
change(orig);
cout<<“The value after function change() is
over” <<orig<<“/n”;
return 0;
}
Copyright@ Amity University
Example Call By Reference ...