You are on page 1of 11

Lab 4:

Objectives:

The purpose of this lab is to

a. Getting familiar with switch


b. Getting familiar with jump statements
c. Understanding for loop

Tools Used:

a) PC
b) Visual Studio

Submission Date: 27th march,2020


Evaluation: Signatures of Lab Engineer:

Every program should begin with a comment that describes the purpose of the program, author, date
and time

Task 1 : Dry run following code. Write step wise solution
1. Complete the following C++ statement In this operation we use for loop to
to achieve the following sequence of sequence the values
values:
99,88,77,66,55,44,33,22,11,0. #include <iostream>
using namespace std;
int main()
for (;;)
{
For(loop=99;loop>=0;loop-=11)
Cout<< loop;
}
Return 0;
}
int x=5, y =10; When I pasted and run the code the
2. if(x<10) compiler shows error because the
{ variables x and y are uninitialized so I
if(y>10) replace it with a and b also the cout is also
{ Undefined.
Cout<<”%%%”<<endl;
}
Else
{
Cout<<”***”<<endl;
Cout<<”@@@”<<endl;
}
3. switch(n)
{
Case 1:
Cout<<”the number is one”<<endl;
continue:
Case 2:
Cout<<”the number is two”<<endl;
break:
default:
Cout<<”the number is not 1 or
2”<<endl;
break:
}

4. Differentiate between if else and switch


statement
If else statement:
The if keyword is used to execute a
statement or block, if, and only if, a
condition is fulfilled , by using the
elsekeyword to introduce an alternative
statement
Syntax:
if (condition) statement1 else
statement2

switch statement:
The switch statement has a somewhat
peculiar syntax because it uses labels
instead of blocks
Syntax:
switch(expression)
{
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional

// you can have any number of case statements.


default : //Optional
statement(s);
}

5. Find error in following code Output:


switch(n)
{
Case 1:
Cout<<”the number is one”<<endl;
Case 2:
Cout<<”the number is two”<<endl;
break:
default:
Cout<<”the number is not 1 or
2”<<endl;
break:
}

6. int variable=100; Output:

cout<< (char)a;

7. Following code should output odd integers In this we use for loop to operate the
from 20 to 1 function
for (int x=20;x>=1;?) #include <iostream>
using namespace std;
int main()
{

for (int x=20;x>=1;x--)


if(x%2==1)//using modular operator
for finding odd numbers
{
cout<<x<<"\n";
}

return 0;
}
Output :
19,17,15,13,11,9,7,5,3,1

8. #include<iostream>
#include<conio.h> #include<iostream>
using namespace std;
void main() using namespace std;
{ int main()
int x = 2, y = 3, result = 1; {
for(int i = 0; i < y; i++)
int x = 2, y = 3, result = 1;
{
result *= x; for(int i = 0; i < y; i++)
}
cout << result << endl; {
getch(); result *= x;
} }
cout << result << endl;
return 0;
}
Output :
8
9. #include<iostream>
#include<conio.h> #include<iostream>
using namespace std; using namespace std;
void main() int main()
{ {
for(int i=1;i<=2;i++) for(int i=1;i<=2;i++)
{
{
for(int j=1;j<=2;j++)
{ for(int j=1;j<=2;j++)
for(int k=1;k<=3;k++) {
cout<<"*"; for(int k=1;k<=3;k++)
cout<<endl; cout<<"*";
} cout<<endl;
cout<<endl; }
} cout<<endl;
getch(); }
}

return 0;
}
Output:

***
***
***
***

10. //This program is written just to show


you that for loop can be written we do not operate any function without
without initialization and increment initializing it and In for loop we have initialize
statement. You can see both these the value and write increment within the bracket
statements are provided manually. and this is how the for loop must be written
You will not use for loop like this.
This is just to show you that this #include<iostream>
thing also exists. #include<conio.h>
using namespace std;
#include<iostream> int main()
#include<conio.h> {
using namespace std;
void main() for (int num = 1;num<=10; num++)
{ {
cout << num << " ";
int num = 1;
for(; num <= 10;)
}
{
cout << num << " ";
return 0;
num++; }
} Output:
getch(); 1,2,3,4,5,6,7,8,9,10
}

Task 2 : Write a program for a basic calculator (Using Switch Only) Your calculator should take two
integers. Then it should display options for different operations and then ask the user for choice. Based on
user’s choice it will perform the operation. The options will be displayed as following
Press 1 for addition
Press 2 for subtraction
Press 3 for multiplication
Press 4 for division
Press 5 for log of first integer
Press 6 for power function

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{

int a, b;
int sum, diff, multiply, divide, modulas,answer;
cout << "Enter the number1=";
cin >> a;
cout << "Enter the number2=";
cin >> b;
cout << "enter the answer (1=sum,2=diff,3=multiply,4=divide,5=modulas)" << endl;
cin >>answer;
sum = a + b;
diff = a - b;
multiply = a * b;
divide = a / b;
modulas = a % b;
if (answer == 1)
{
cout << "sum of number" << sum << endl;
}
else if (answer == 2)
{
cout << "diff of number" << diff << endl;
}
else if (answer == 3)
{
cout << "multiply of number" << multiply << endl;
}
else if (answer == 4)
{
cout << "divide of number" << divide << endl;
}
else if (answer == 5)
{
cout << "modulas of number" << modulas<< endl;
}

_getch();
return 0;

}
Task 3 :

Write a C++ program that displays alphabets from a to z using for loop except for vowels (a e i o u)

#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
char alpha;
for (alpha = "a"; alpha <= "z"; alpha++)
{
if (alpha != "a" && alpha != "e" && alpha != "i" && alpha != "o" && alpha != "u")
{
cout << alpha << "";
}
}
_getch();
return 0;
}
Output :

Output:
Task 4 :
Write a program using for statment to calculate and print product of even integers from 1 to 20

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

for (int number = 1; number <= 20; number++)


{
if (number % 2 == 0)
{
int result = number * number;
cout << number << " * " << number << " = " <<
result << endl;
}

_getch();
return 0;
}
Task 5:
A person invests $1000.00 in a saving account yielding 5% interest. Assuming all interest is left deposit in
the account, calculate and print the amount of money in the accounts at the end of each year for ten years.
Formula: (a = p(1+r)^n). where
p    original amount invested
r    Annual Interest rate
n    Number of years
a    Amount on deposit at the end of nth years

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
    long int p, r, n, a;
    p=1000;
    for(n=1;n<=10;n++)
    {
        a=p*(pow(1+0.05,n));
        cout<<"\n Amount of money in the account at the end of the "<<n<<" year:"<<a;
    }
    getch();
Output:

You might also like