Bangladesh University of Business and Technology
(BUBT)
Assignment
Assignment No: 01
Course Code: CSE 112 (Old code: CSE 122)
Course Title: Object Oriented Programming Language Lab
Submitted By: Submitted To:
Name: Md. Ismail Hossain Iffat Ara Sanzida
ID: 20211203019 (Lecturer)
Intake: 37 Department of Computer Science
and Engineering
Section: 01 Faculty of Engineering & Applied
Program: B.Sc. in CSE (evening) Sciences
1. Sum of series: 1+2+3+4+...............+n using C++.
Code:
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Please enter the number: ";
cin>>n;
int sum = 0;
for(int i =1; i<=n; i++){
sum = sum+i;
}
cout<<"The sum is: "<<sum<<endl;
return 0;
}
Output:
2. Find a year leap year or not using C++.
Code:
#include<iostream>
using namespace std;
int main()
{
int year;
cout<<"Enter a year: ";
cin>>year;
if((year%4 ==0 && year%100 != 0) || year%400 == 0){
cout <<"Year is leap year";
}
else{
cout << "Year is not leap year";
}
return 0;
}
Output:
3. Find Vowel/consonant in C++.
Code:
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Please enter a letter: ";
cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
cout << "This letter is a VOWEL." << endl;
} else {
cout << "This letter is a CONSONANT." << endl;
}
return 0;
}
Output:
4. Define a class “Car”, set attributes like brand, model, year and show the attribute details using a
method “displayinfo”. Create an object of Car class and show details.
Code:
#include <iostream>
using namespace std;
class Car {
public:
string brand;
string model;
int year;
void displayInfo() {
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
}
};
int main() {
Car car1;
car1.brand = "BMW";
car1.model = "BM-1";
car1.year = 2025;
car1.displayInfo();
return 0;
}
Output:
5. Find the factorial of a number using recursion.
Code:
#include<iostream>
using namespace std;
int factorial (int n)
{
if (n==1)
return 1;
else if (n==0)
return 1 ;
else
return(n*factorial (n-1));
}
int main ()
{
int a,b ;
cout<<"Please Enter Your Number : " ;
cin>>a;
b=factorial(a);
cout<<"The factorial of this number is : "<<b<<endl ;
return 0 ;
Output:
6. Find the factorial of a number using iterative method.
Code:
#include <iostream>
using namespace std;
int main(){
int n;
cout<<"Please enter a number: ";
cin>> n;
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i;
}
cout<<"The factorial of "<<n<<" is "<<fact<<endl;
return 0;
}
Output:
7. Create a class Student with attributes: name, roll_no, and grade. Create a method show_details()
that prints the student information. Create an object and show the details.
Code:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int roll_no;
char grade;
void show_details() {
cout << "Name: " << name << endl;
cout << "Roll No: " << roll_no << endl;
cout << "Grade: " << grade << endl;
}
};
int main() {
Student s1;
s1.name = "Md. Ismail Hossain";
s1.roll_no = 19;
s1.grade = 'A';
s1.show_details();
return 0;
}
Output: