Assignment PLC and Oop 2023
Assignment PLC and Oop 2023
cout<<height;
d) String:
string stdname="hafsa";
cout<<stdname;
e) Character:
char grade='A';
cout<<grade;
f) Boolean
bool isexam=true;
cout<<isexam;
10. int a = 7, b = 2;
int result= (++b) -5;
what is the value of result?
Ans: result = -2
true || true
OR
1 || 1
Output is true or 1.
16. What will be value of temp after execution of given statements:
double temp=0;
int x=20.78;
int y=3;
temp=x+y;
cout<<temp;
Ans: 23.78
17. A person deposited one million in the bank. The bank pays an interest of 10%. Write a program
that compute his amount after three year.
Program,
#include <iostream>
#include <cmath>
int main()
{
double principal = 1000000.0;
double rate = 0.10;
int time = 3;
cout << "After " << time << " years, you will have $" << amount << endl;
return 0;
}
18. find the sum of following sequence of numbers using for loop:
10,20,30,40-------------1000
Program,
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for (int i = 10; i <= 1000; i += 10) {
sum += i;
}
cout << "The sum of the sequence is: " << sum << endl;
return 0;
}
Output: The sum of the sequence is: 50500
19. Write a program that compute the sum of given sequence using for loop:
5,10,15------------5000
Hint : n=((an-a1)/d +1) Sn=n(a1+an)/2
Program,
#include <iostream>
using namespace std;
int main()
{
int a1 = 5;
int an = 5000;
int d = 5;
int n = ((an - a1) / d) + 1;
int sum = n * (a1 + an) / 2;
cout << "The sum of the sequence is: " << sum << endl;
return 0;
}
Output: The sum of the sequence is: 2502500
20. Write a program to separate the integral and fractional parts of a given real number and print the
result on screen.
Program,
#include <iostream>
using namespace std;
int main() {
double num;
int integral, fractional;
cout << "Enter a real number: ";
cin >> num;
integral = static_cast<int>(num);
fractional = static_cast<int>((num - integral) * 100);
cout << "Integral part: " << integral << endl;
cout << "Fractional part: " << fractional << endl;
return 0;
}
output:
Enter a real number: 56.34
Integral part: 56
Fractional part: 34
21. Write a program to input an integer value and find out whether integer is divisible by 3 or not
using conditional operator.
Program,
#include <iostream>
using namespace std;
int main() {
int num;
(num % 3 == 0) ? cout << num << " is divisible by 3" : cout << num << " is not divisible by 3";
return 0;
}
Output:
Enter an integer value: 24
24 is divisible by 3
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (isalpha(ch)) {
cout << ch << " is a letter." << endl;
} else if (isdigit(ch)) {
cout << ch << " is a digit." << endl;
} else {
cout << ch << " is not a letter or digit." << endl;
}
return 0;
}
Output: Enter a character: y
y is a letter.
23. Write a program to input an integer number and display the corresponding name of the month by
using switch statement.
For example, if an integer value 3 is entered then print “March” and so on. If value is not between
1 and 12 then print message “Invalid Month”.
Program,
#include <iostream>
using namespace std;
int main() {
int month;
cout << "Enter a number between 1 and 12: ";
cin >> month;
switch(month) {
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
default:
cout << "Invalid Month";
}
return 0;
}
Output:
Enter a number between 1 and 12: 7
July
24. What’s reason to overload the operators? and why passing an object reference in operator
overloading in C++, see in below code.
Ans: Operator overloading allows operators to be used with user-defined types just like they are used
with primitive types. This makes code more readable and expressive.
Passing an object reference in operator overloading in C++ is necessary because operator overloading
is typically done as a member function of a class, which operates on an object of that class. By
passing the object reference as a parameter, the operator function can modify the object's state as
necessary. Additionally, passing by reference is more efficient than passing by value since it avoids
unnecessary copying of the object.
25. What is the importance of encapsulation in object oriented programming and how it is achieved
or implemented?
Ans: Encapsulation is an important concept in object-oriented programming that refers to the
bundling of data and methods within a single unit, i.e., a class, and restricting access to the data from
outside the class. Encapsulation allows for data hiding and protects the data from being modified by
unauthorized users. This helps in maintaining the integrity and reliability of the program.
Encapsulation is achieved in C++ through the use of access specifiers, such as public, private, and
protected, which restrict the access to the members of the class. By default, all members of a class are
private, and can only be accessed within the class itself. However, by making certain members public
or protected, they can be accessed from outside the class as well.
26. Write down names and notations of four types of relationship between objects and also write
down two examples of each relationship.
27. There are many types of classes in object oriented programming. Write down the five names of
classes.
Ans: five names of classes in object-oriented programming:
1. Abstract class
2. Concrete class
3. Final class
4. Static class
5. Inner class
Ex : Input : N=107067
Output: “107067”
Program,
#include <iostream>
#include <string>
class mystring {
public:
static string intToString(int N) {
string str=" ";
bool is_negative=false;
if(N<0){
is_negative=true;
N=-N;
}
while(N>0){
int digit=N%10;
str=char(digit)+str;
N/=10;
}
if(is_negative){
str='-'+str;
}
return str;
}
};
int main() {
int num = 107067;
string str=mystring::intToString(num);
cout <<"the string representation of"<<num<<"is"<<str<<endl;
return 0;
}
Output:
the string representation of107067is107067
0 1 2 3 4 5 6 7 8 9
48 49 50 51 52 53 54 55 56 57
______________________________________________________________________________