You are on page 1of 15

CSL-113 : Computer Programming Lab

Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 1: User Input

Write a program that takes 3 values from user. Two values of integer and one
value of float data type. Print each result on one line.

SOURCE CODE
#include<iostream>
using namespace std;

int main()
{
int x ,y;
float z;
cout<<"please enter first integer:"<<endl;
cin>>x;
cout<<"please write second integer:"<<endl;
cin>>y;
cout<<"plese enter float:"<<endl;
cin>>z;
cout<<"\n"<<x<<"\n"<<y<<"\n"<<z;
return 0;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 2 : Arithmetic Operations

Write a program that gets 2 integers input from user and store them in variables.
Do the five basic Arithmetic Operations (+ , - , *, /, %) of the two numbers. Print
the results of operations as below

SOURCE CODE
#include<iostream>
using namespace std;

int main()
{
int a,b;
int add,sub,multi;
float divi,mod;

cout<<"please enter first number :";


cin>>a;
cout<<"please enter second number :";
cin>>b;

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

add=a + b;
cout<<a<< " + "<<b<<" = "<<add<<endl;
sub=a-b;
cout<<a<< " - "<<b<<" = "<<sub<<endl;
multi=a*b;
cout<<a<< " * "<<b<<" = "<<multi<<endl;
divi=(float)a/b;
cout<<a<< " / "<<b<<" = "<<divi<<endl;
mod=a%b;
cout<<a<< " % "<<b<<" = "<<mod<<endl;

return 0;
}
SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 3 : Percentage

Write a program that prompt user to input course name, obtained marks and
total marks. Calculate the percentage using this formula marks percentage =
marks obtained / total * 100
and display the results as follows.

SOURCE CODE
#include<iostream>
using namespace std;

int main()
{
string cours;
float p,x,y;

cout<<"please enter course name :";


cin>>cours;
cout<<"please enter obtained marks :";
cin>>x;
cout<<"please enter total marks :";

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cin>>y;
p=(x/y)*100;
cout<<"in "<<cours<<"you have secured %"<<p<<endl;

return 0;
}
SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 4 : Calculating Value of X

Write a program that finds the value of X by using given formula. Take value of a
and b from user.

X= (a + b)2 – 2ab

SOURCE CODE
#include<iostream>
using namespace std;

int main()
{
//declearing variables

int a,b,x;

cout<<"please enter value of a and b"<<endl;


cin>>a>>b;

x=((a+b)*2)-2*a*b;
cout<<"your answer is :"<<x<<endl;

return 0;

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 5 : Word Game

Write a program that plays a word game with the user. The program should ask
the user to enter the following:

User’s name
Year of birth (eg. 1990)
Name of university
A favorite hobby
A pet’s name

Write a program that will produce an outcome as below:

SOURCE CODE

using namespace std ;

int main()
{
string name,uname,hobby,pname;
int a;

cout<<"Whats your name :";

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cin>>name;
cout<<"Enter year of birth :";
cin>>a;
cout<<"Name Of University :";
cin>>uname;
cout<<"A favourite hobby :";
cin>>hobby;
cout<<"A pet's name :";
cin>>pname;
cout<<endl;

cout<<"there lives a person named "<<name<<" who is currently 19 years


of age. "<<name<<" still studying at "<<uname<<"."<<"it is interesting because
"
<<name<<" like to read with "<<pname<<" and they live happily ever
after !"<<endl;

return 0;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 6

Write a program that inputs a five-digit integer, separates the integer into its
Individual digits and prints the digits vertically. For example, if the user types
32156, the program should print:

6
5
1
2
3

SOURCE CODE

SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Exercise 7

Hypotenuse refers to the side opposite


the right angle in a right-angled
triangle (as shown in the diagram below).

Hypotenuse can be calculated using the following formula:

Area of this right-angled triangle can also be calculated using the following formula: a
=½*x*y

Note that: h=
hypotenuse x=
adjacent
y= opposite

Write a C++ program that prompt user to enter value of X and Y. You have to calculate
the value of Hypotenuse (h) and Area(a).

SOURCE CODE
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int x,y,a;
float h;
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"enter value of x :";


cin>>x;
cout<<"enter value of y :";
cin>>y;
h=sqrt((x*x)+(y*y));
cout<<"hypotenuse of your triangle is :"<<h<<endl;
a=0.5*x*y;
cout<<"area of your triangle is :"<<a<<endl;

return 0;
}
SNAPSHOT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 02: Variables and Data Types

You might also like