05 DSA Lab Exercise For Chapter Two (WU)
05 DSA Lab Exercise For Chapter Two (WU)
Example 1
#include<iostream.h> Output
void main()
{
cout<<“Welcome”;
cout<<“ to C++!”;
}
Example 2
Output
#include<iostream.h>
void main()
{
cout<<“Welcome”;
cout<<“\nto C++!”;
}
Example 3
#include<iostream.h>
int main()
{ Output
cout<<“Name\t Age”;
cout<<“\nBoni\t18”;
return 0;
}
Exercise
Write a C++ Program to print:
A. B.
* * *
* * **
**** ***
* * ****
* *
This example demonstrates use of variable
#include<iostream.h> Output
int main()
{
int number;
number = 10;
cout<<“The value in number is: ”<<number;
return 0;
}
Example 2
#include<iostream.h>
int main() Output
{
int number;
number = 10;
cout<<“The value in number is: ”<<“number”;
return 0;
}
Example 3 Output
#include<iostream.h>
void main()
{
char check=‘M’;
cout<<check;
}
Example 4
Output
#include<iostream.h>
void main()
{ char check;
check =‘9’;
cout<<“Value of check is: ”<<check;
}
Example 5
#include<iostream.h>
void main()
Output
{
int firstNumber;
float secondNumber;
firstNumber = 5;
secondNumber = 25.36;
cout<<“Value of fist number is: ”<<firstNumber<<“\nValue of second number is:
”<<secondNumber; }
Identify errors in the following program
#include<iostream.h Corrected
Void Main{ }
int value = 7;
cout<< “Value is: <<value<<“Result is: <<result;
int result=12;
Arithmetic Operation
Example1
#include<iostream.h>
void main()
{ Output
int n=50, m =10;
n+=m;
cout<<“n = ”<<n;
}
Example 2
#include<iostream.h>
void main()
{
int x = 39, y = 6; Output
cout<<“x = ”<<x<<“ and y = ”<<y<<endl;
cout<<“x+y = ”<<x+y<<endl;
cout<<“x-y = ”<<x-y<<endl;
cout<<“x*y = ”<<x*y<<endl;
cout<<“x/y = ”<<x/y<<endl; //x/y = 6, not 6.5
cout<<“x%y = ”<<x%y<<endl;
}
Example 3
#include<iostream.h> Output
#include<stdlib.h>
void main()
{
int neg = -7;
cout<<“Absolute value of -7 is: ”<<abs(neg);
}
Logical operators
Example
#include<iostream.h>
void main()
{
Output
int w, x, y,z;
w = 5<5;
x= !(5==5);
y = (5 < 6) && (6 < =6);
z = 5 < 6 || 6 < 5;
cout<<“w = ”<<w;
cout<<“x = ”<<x<<endl;
cout<<“y = ”<<y<<endl;
cout<<“z = <<z<<endl;
}
Increment/Decrement operation
Example 1
#include<iostream.h> Output
void main()
{ int x = 10;
Example 2
#include<iostream.h> Output
void main()
{
int x = 10, y;
y = ++x + 5; //what if y = x++ + 5;
cout<<“Value of y is: ”<<y;
}
Example 3
#include<iostream.h> Output
void main()
{
int x = 10, y;
y = x-- + 5; //what if y = --x + y;
cout<<“Value of y is: ”<<y;
}
#include <iostream>
int main()
{
Output
int m=6,n=2,p=4,r=3,s=8,k = 4,a,b,c,d ;
a =++n + ++m;
b = s++ * s++;
c = p– –*p– – ;
d = (– –k* – – k)*++r ;
cout<<“a = “<<a<<“,\tn = “<<n<<“,\tm = “<<m<<endl;
cout<<“b = “<<b <<“,\ts = “ <<s<<“\n”;
cout<<“c = “<<c<<“,\tp = “<<p<<endl;
cout<<“d = “<<d <<“,\tk = “<<k<<“,\tr = “<<r<<endl;
return 0;
}
Example 4
#include<isotream.h>
#include<math.h> Output
void main()
{
int radius, area;
cout<<“Enter radius of the cirlce: ”<<endl;
cin>>radius;
area = 2 * M_PI *radius * radius;
cout<<“Area of a circle with radius:”<<radius<<“ is: ”<<area;
}