Sie sind auf Seite 1von 9

Lab 3

Introduction to Computer Programming Lab

Lab Journal - 3
Name: __Muhammad Furqan Khan__________

Enrollment #: ___01-235181-033______________________________

Date: ___07/03/2018_____________________________

Objective:
1) Understanding of relational and equality operators
2) Understanding Control Structures
3) Using if, if/else, multiple ifs/else-if, switch statements and ternary operator

Tools Required:

a) PC with Windows 7 Professional or onwards


b) Visual Studio 2013 onwards

Attempt the following tasks:


Task 1 : Give output of the following piece of code:
1. int a=5, b =10; Output:
cout << (a > b) << endl;
cout << (a < b) << endl;
cout << (a == b) << endl;
cout << (a != b) << endl;

Print the output of following expressions Output:


2. (each line of code) as 1 and 0 (1 for “True”
and 0 for “False”).
(5+ 4 < 3)

('a' != 'b' - 1)

(7 == 7)

(3%2 == 1)

('a' == 97)
3. int a=5, b=10, c=15; ; Output:

Introduction to Computer Programming Lab


Page 1
Lab 3

cout << ((a > b) && (c > b)) <<


endl;
cout << ((a > b) || (c > b))<<
endl;
cout << (a || b) << endl;
cout << (a && b) << endl;
cout << !(a > b) << endl;

4. Differentiate between = and ==. = Assignment operator


== Equal sign
5. int x = 25; Output:
if (x / 2 == 12)
{
cout << "I ”;
cout << "like";
}
else
{
cout << "I ";
cout << "don’t like";
}
cout << "apples";

6. double Max =12.7; Output:


if(Max>=12)
cout<<”French”;
else
cout<<”Fries”;

7. int x = 12; Output:


if(x!=12)
cout << “Yes”;
else
cout << “No”;

8. int x = 13; Output:


if(x>12)
{
if(x<15)
cout<<”Blue”;
}
Else
{
cout<<”Green”;
}

Introduction to Computer Programming Lab


Page 2
Lab 3

cout<<”Jeans”;
9. int x = 20;
if(x>10)
{
if(x<25)
cout<<”Happy ”;
}
Else
{
cout<<”Not Happy ”;
}
cout<<” Day!”;
Output:
10. Write C++ statement for following pseudocode:
if number is divisible by 2
print “even”
else
print “odd”
Using if/else statement:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int x,number;
cout << "enter a number" ;
cin >> x;
if (x % 2 == 0)

cout << "even";


}

else
{

cout << "odd";


}

_getch();
return 0;

Introduction to Computer Programming Lab


Page 3
Lab 3

11. Cout<<”Hi there!\n”;


Goto Label;
Cout<<”How are you?\n”;
Cout<<”Are you feeling
better?\n”;
Label:
Cout<<”Hope you are good!”;
12. cout << "I'm at line 1\n";
goto Point1;
cout << "I'm at line 2\n";
cout << "I'm at line 3\n";
Point1:
cout << "I'm at line 4\n";

13. int x = 10;


cout<<x<<endl;
x++;
cout<<x<<endl;
++x;
cout<<x<<endl;
x--;
cout<<x<<endl;
--x;
cout<<x<<endl;

14 int x = 10;
cout<<x<<endl;
cout<<x++<<endl;
cout<<++x<<endl;
cout<<x--<<endl;
cout<<--x<<endl;
cout<<x<<endl;

Task 2 : Write a C++ program that reads an integer and determines and prints whether it is odd or
even using if-else statements.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{

int x, number;
cout << "enter a number\t=";
cin >> x;
if (x % 2 == 0)

Introduction to Computer Programming Lab


Page 4
Lab 3

cout << "even";


}

else
{

cout << "odd";


}
q cout << " Number";

_getch();
return 0;

Task 3 : Write a program that reads three non zero integers and determines and prints whether
they could be the sides of a right angle triangle. (Hint* : Pythagorus Theorem a2 + b2 = c2 )
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{

int g, h, k;
cout << "Enter the num1\t";
cin >> g;
cout << "Enter the num2\t";
cin >> h;
cout << "Enter the num3\t";
cin >> k;

if ((k*k) == (g*g) + (h*h))


{
cout << "yes the side of angle";
}
else
{
cout << " No the side of angle";
}

_getch();
return 0;

Introduction to Computer Programming Lab


Page 5
Lab 3

So your program should take three variables a, b and c and check that the sum of square of a and
square of b should be equal to the square of c.

Task 4: Write a program for a basic calculator. 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 finding the remainder
#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)

Introduction to Computer Programming Lab


Page 6
Lab 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;

Introduction to Computer Programming Lab


Page 7
Lab 3

Task 5 : Write a C++ program (use if-else statement) to compute the telephone bill for the city
consumers. The bill computed according to the number of calls.

 If numbers of calls are less than and equal to 100, then the rate per call is rs.0.80 and the
meter charges is Rs. 250.
 If numbers of calls are greater than 100, then the rate per call is computed is Rs. 1.00 and
the meter charges are minimum Rs.350.
 Formula for bill calculation is:

Introduction to Computer Programming Lab
Page 8
Lab 3

Phone Bill = meter charges + (number of calls x rate per call)


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

int call, bill;


cout << " Enter the call=";
cin >> call;
if (call <= 100)
{

bill = 250 + (0.80*call);


}
else
{
bill = 350 + (1 * call);
}
cout << "rupees bill=" <<bill;

_getch();
return 0;
}

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

Introduction to Computer Programming Lab


Page 9

Das könnte Ihnen auch gefallen