Computer Programming Lab 2
Computer Programming Lab 2:
Lab 02: Variables and Data Types
Exercises
Task 1:
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 a, b;
float c;
cout<<"a= ";
cin>>a;
cout<<"b= ";
cin>>b;
cout<<"c= ";
cin>>c;
cout<<"Integer a=" <<a
<<" integer b=" <<b <<" float="
<<c;
return 0;
}
Task 2:
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>
#include <cmath>
using namespace std;
int main()
{
float a, b;
string sum, multiply, divide, subtraction, percentage;
cout<<"entering two
integers"<<endl;
cin>>a;
cin>>b;
sum= a+b;
multiply= a*b;
divide= a/b;
subtraction= a-b;
percentage= (a/100)*b;
cout<<"sum=" <<a+b <<
"\n" <<"multiply=" <<a*b <<
"\n" <<"divide=" <<a/b << "\n"
<<"subtraction=" <<a-b << "\n"
<<"percentage=" <<(a/100)*b;
return 0;
}
Snapshot:
Task 3:
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()
{
float marks_obtained, total_marks,
marks_percentage;
string course_name;
cout<<"course_name"<<endl;
cin>>course_name;
cout<<"marks_obtained"<<endl;
cin>>marks_obtained;
cout<<"total_marks"<<endl;
cin>>total_marks;
marks_percentage =
(marks_obtained/total_marks)*100;
cout<<"In "
<<course_name <<" course. You have secured "
<<marks_percentage <<"%";
return 0;
}
Snapshot:
Task 4:
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()
{
int
a, b, X;
cout<<"a
=";
cin>>a;
cout<<"b
=";
cin>>b;
X= (a+b)*2-2*(a*b);
cout<<"X="
<< (a+b)*2-2*(a*b);
return
0;
}
Snapshot:
Task 5:
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:
#include <iostream>
using namespace std;
int main()
{
string
user_name, name_of_university, A_favorite_hobby, pet_name;
int
year_of_birth,birth_year;
cout<<"What
is your name?"<<endl;
cin>>user_name;
cout<<"Year of birth"<<endl;
cin>>year_of_birth;
birth_year=
2017-year_of_birth;
cout<<"Name of University"<<endl;
cin>>name_of_university;
cout<<"A favorite hobby"<<endl;
cin>>A_favorite_hobby;
cout<<"pet name"<<endl;
cin>>pet_name;
cout<<"there's lives a person named " <<user_name
<<" whose is currently age of " <<
birth_year<<". " << user_name <<" is studying
at " <<name_of_university <<". it is interesting because
" <<user_name <<" likes to read with "
<<pet_name <<" and they lived happily after!";
return 0;
}
SnapShot:
Task 6:
(Assigment)
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 Program:
using namespace std;
int main()
{
int
a,x,x1,x2,x3,x4;
cout<<"input
a five digit integer:";
cin>>a;
x= a%10;
a=a/10;
x1=a%10;
a=a/10;
x2=a%10;
a=a/10;
x3=a%10;
a=a/10;
x4=a%10;
cout<<x
<<endl <<x1 <<endl <<x2 <<endl <<x3
<<endl <<x4;
return 0;
}
Snapshot:
Task 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
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()
{
float a, h, x, y;
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
h=sqrt(x*x+y*y);
a=0.5*x*y;
cout<<"Hypotenuse= " <<h <<"\n"
<<"Area= " <<a ;
return 0;
}
Comments
Post a Comment