0% found this document useful (0 votes)
15 views26 pages

Muhammad Mamoon Irfan - F2023266192 - OOPLAB - V-6 - LAB#1

The document discusses object oriented programming and contains 12 programming tasks. The tasks cover basic concepts like taking user input, performing calculations, arrays, functions, conditional statements, and finding minimum and maximum values. Programs are provided for each task in C++.

Uploaded by

mamoonirfan786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

Muhammad Mamoon Irfan - F2023266192 - OOPLAB - V-6 - LAB#1

The document discusses object oriented programming and contains 12 programming tasks. The tasks cover basic concepts like taking user input, performing calculations, arrays, functions, conditional statements, and finding minimum and maximum values. Programs are provided for each task in C++.

Uploaded by

mamoonirfan786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Object Oriented Programming (OOP)

Submitted To: Riaz Ahmed

SCHOOL OF SYSTEM AND


TECNOLOGY (SST) UNIVERSITY
3/22/2024 OF MANAGEMENT AND
TECNOLOGY

Department: Computer Science (SST/CS)

Email: f2023266192@umt.edu.pk

Submitted By: Muhammad Mamoon Irfan

Student ID: F2023266192 BS(CS)

Assignment No: 1

Announcement date:3/16/2024

Subject: Object Oriented Programming


(OOP)

Section: V6
Object Oriented Programming (OOP)

Object Oriented Programming (OOP)


Lab Assignment No 1
Task: 1
Important Note: My entire programs was written by me. For this assignment, I used Visual
Studio. The source code is editable, not the screenshot.

Source Code:
//Given two floats x and y (take input from user), write a C++ program that finds
their sum.
#include <iostream>
using namespace std;
int main()

float num1,num2;
cout<<"Please enter a first value:"<<endl;
cin>>num1;
cout<<"Please enter a second value:"<<endl;
cin>>num2;
cout<<"Sum of your given inputs is="<<num1+num2;
}

Object Code:

1
Object Oriented Programming (OOP)

Task: 2
Source Code:
//Write a C++ program that takes a number and find square, cube and half of the
input.
#include <iostream>
using namespace std;
int main()
{
int num;
float half;
cout << "Enter a number:";
cin >> num;
cout << "The Square of your given number will be:" << num * num << endl;
cout << "The Cube of your given number will be:" << num * num * num << endl;
half = num / 2;
cout << "The half of your given number will be:" << half;
}

Object Code:

2
Object Oriented Programming (OOP)

Task: 3
Source Code:
/*Fibonacci sequence is a sequence in which every number after the first two is
the sum of the two preceding ones. Write a C++ program that takes a number n from
user and populate a array with first n Fibonacci numbers.*/
#include <iostream>
using namespace std;
int main()
{
int b = 1, num, next = 1;
cout << "Enter a number:";
cin >> num;
cout << b << "\t";
for (int i = 1; i < num; i++)
{
cout << next << "\t";
int temp = next;
next = next + b;
b = temp;
}

3
Object Oriented Programming (OOP)

Object Code:

Task: 4
Source Code:
/*Write a C++ program that creates an array of 10. It then adds 3 to each element
of the array You have to add to the elements using pointer only.*/
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {0, 10, 2, 30, 26, 12, 5, 9, 15, 28};
for (int i = 0; i < 10; i++)
{
cout <<arr[i]+3<<"\t";
}
}

Object Code:

4
Object Oriented Programming (OOP)

Task: 5
Source Code:
//Write a function that checks whether a given number is prime or not.
#include <iostream>
using namespace std;
int main()
{
int num, j = 0;
cout << "Please enter an number:";
cin>>num;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
j++;
}

5
Object Oriented Programming (OOP)

}
if (j == 2)
{
cout << "It is a prime number.";
}
else
{
cout << "It is not a prime number.";
}
}

Object Code:

Task: 6
Source Code:
// Write a function that calculates factorial of a given number.
#include <iostream>
using namespace std;
int main()
{
int num, fact = 1;

6
Object Oriented Programming (OOP)

cout << "Please Enter a number:";


cin >> num;
if (num >= 0)
{
for (int i = 1; i <= num; i++)
{
fact = fact * i;
}
cout << "Factorial of your given number is=" << fact;
}
else
{
cout << "Factorial is not possible";
}
}

Object Code:

Task: 7
Source Code:
/*Write a C++ program, that prints grade of marks.
If marks are between 85-100 grade them A+

7
Object Oriented Programming (OOP)

If marks are between 80-85 grade them A


If marks are between 75-80 grade them A-
If marks are between 70-75 grade them B+
If marks are between 65-70 grade them B-
If marks are between 60-65 grade them B
If marks are between 55-60 grade them C+
If marks are between 50-55 grade them C
< 50 F
*/
#include <iostream>
using namespace std;
int main()
{
int marks;
cout << "Enter your marks between 0 to 100:";
cin >> marks;
if (marks > 85 && marks <= 100)
{
cout << "Your grade is A+";
}
else if (marks > 80 && marks <= 85)
{
cout << "Your grade is A";
}
else if (marks > 75 && marks <= 80)
{
cout << "Your grade is A-";
}
else if (marks > 70 && marks <= 75)
{
cout << "Your grade is B+";
}
else if (marks > 65 && marks <= 70)
{
cout << "Your grade is B-";
}
else if (marks > 60 && marks <= 65)
{
cout << "Your grade is B";
}
else if (marks > 55 && marks <= 60)
{
cout << "Your grade is C+";
}
else if (marks >= 50 && marks <= 65)

8
Object Oriented Programming (OOP)

{
cout << "Your grade is C";
}
else if (marks < 50)
{
cout << "Your grade is F";
}
else
{
cout << "Incorrect input.";
}
}

Object Code:

Task: 8
Source Code:
//Write a C++ program, to find the number is even or odd.
#include <iostream>
using namespace std;

9
Object Oriented Programming (OOP)

int main()
{
int num;
cout << "Enter a number:";
cin >> num;
if (num % 2 == 0)
{
cout << "It is an even number.";
}
else
{
cout << "It is an odd.";
}
}

Object Code:

10
Object Oriented Programming (OOP)

Task: 9
Source Code:
//Write a C++ program to check either the user is male or female. Hint: if user
enter M he’s male else she’s female.
#include <iostream>
using namespace std;
int main()
{
char ch;
cout << "Enter M if Male else Female:";
cin >> ch;
if (ch=='M')
{
cout << "You are Male.";
}
else
{
cout << "You are female.";
}
}

Object Code:

11
Object Oriented Programming (OOP)

Task: 10
Source Code:
//Write C++ program to check whether the user enters a positive number or the
negative number.
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number:";
cin >> num;
if (num>0)
{
cout << "It is positive.";
}
else
{
cout << "It is negative.";
}
}

Object Code:

12
Object Oriented Programming (OOP)

Task: 12
Source Code:
// Write C++ program to check whether the user enters a positive number or the
negative number.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4, num5;
cout << "Input five numbers:";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
if (num1 >= num2 && num1 >= num3 && num1 >= num4 && num1 >= num5)
{
cout << "The maximum number is:" << num1<<endl;
}
if (num2 >= num1 && num2 >= num3 && num2 >= num4 && num2 >= num5)
{
cout << "The maximum number is:" << num2<<endl;
}
if (num3 >= num1 && num3 >= num2 && num3 >= num4 && num3 >= num5)
{
cout << "The maximum number is:" << num3<<endl;
}
if (num4 >= num1 && num4 >= num2 && num4 >= num3 && num4 >= num5)

13
Object Oriented Programming (OOP)

{
cout << "The maximum number is:" << num4<<endl;
}
if (num5 >= num1 && num5 >= num2 && num5 >= num3 && num5 >= num4)
{
cout << "The maximum number is:" << num5<<endl;
}
if (num1 <= num2 && num1 <= num3 && num1 <= num4 && num1 <= num5)
{
cout << "The minimum number is:" << num1<<endl;
}
if (num2 <= num1 && num2 <= num3 && num2 <= num4 && num2 <= num5)
{
cout << "The minimum number is:" << num2<<endl;
}
if (num3 <= num1 && num3 <= num2 && num3 <= num4 && num3 <= num5)
{
cout << "The minimum number is:" << num3<<endl;
}
if (num4 <= num1 && num4 <= num2 && num4 <= num3 && num4 <= num5)
{
cout << "The minimum number is:" << num4<<endl;
}
if (num5 <= num1 && num5 <= num2 && num5 <= num3 && num5 <= num4)
{
cout << "The minimum number is:" << num5<<endl;
}
}

Object Code:

14
Object Oriented Programming (OOP)

Task: 12
Source Code:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3;
cout << "Input three values:";
cin >> num1 >> num2 >> num3;
if (num1 <= num2 && num1 <= num3 && num2 <= num3)

15
Object Oriented Programming (OOP)

{
cout << "Your given inputs have sorted:" << num1 << "\t" << num2 << "\t"
<< num3;
}
else if (num1 <= num2 && num1 <= num3 && num3 <= num2)
{
cout << "Your given inputs have sorted:" << num1 << "\t" << num3 << "\t"
<< num2;
}
else if (num2 <= num1 && num2 <= num3 && num1 <= num3)
{
cout << "Your given inputs have sorted:" << num2 << "\t" << num1 << "\t"
<< num3;
}
else if (num2 <= num1 && num2 <= num3 && num3 <= num1)
{
cout << "Your given inputs have sorted:" << num2 << "\t" << num3 << "\t"
<< num1;
}
else if (num3 <= num1 && num3 <= num2 && num1 <= num2)
{
cout << "Your given inputs have sorted:" << num3 << "\t" << num1 << "\t"
<< num2;
}
else if (num3 <= num1 && num3 <= num2 && num2 <= num1)
{
cout << "Your given inputs have sorted:" << num3 << "\t" << num2 << "\t"
<< num1;
}
else
{
cout << "Both are equal";
}
}

Alternate way:
/*Write a program in C++ and perform the following: [4 Marks]
1. Declare and initialize two integer type arrays of size 4 with name A and B
respectively.
2. Call a function which will swap the values of array A and B
3. Print swapped arrays by calling a function.
*/
#include <iostream>

16
Object Oriented Programming (OOP)

using namespace std;


void swap(int *arr1, int *arr2, int n)
{
int temp;
for (int i = 0; i < n; i++)
{
temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
for (int i = 0; i < n; i++)
{
cout << arr1[i] << "\t";
}
for (int i = 0; i < n; i++)
{
cout << arr2[i] << "\t";
}
}
int main()
{
int n = 5;
int A[n], B[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
for (int i = 0; i < n; i++)
{
cin >> B[i];
}

swap(A, B, n);
}

Object Code:

17
Object Oriented Programming (OOP)

Task: 13
Source Code:

18
Object Oriented Programming (OOP)

/*Write a program in C++ and perform the following: [4 Marks]


1. Declare and initialize two integer type arrays of size 4 with name A and B
respectively.
2. Call a function which will swap the values of array A and B
3. Print swapped arrays by calling a function.
*/
#include <iostream>
using namespace std;
int main()
{
int arr1[5], arr2[5];
for (int i = 0; i < 5; i++)
{
cin >> arr1[i];
}
for (int j = 0; j < 5; j++)
{
cin >> arr2[j];
}
for (int k = 0; k < 5; k++)
{
int temp = arr1[k];
arr1[k] = arr2[k];
arr2[k] = temp;
}
for (int i = 0; i < 5; i++)
{
cout << arr1[i] << "\t";
}
cout<<endl;
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << "\t";
}
}

Object Code:

19
Object Oriented Programming (OOP)

Task: 14
Source Code:
// Write a C++ Program in which you need to implement Following Functions. Take
Int type 1D Array Size 10.
#include <iostream>
using namespace std;
void sort1D(int *arr, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[i])
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < n; i++)
{

20
Object Oriented Programming (OOP)

cout << arr[i] << "\t";


}
cout << endl;
}
void SearchArray(int *arr, int n)
{
int num, loc = -1, index;
cout << "Please enter a number for search:";
cin >> num;
for (int i = 0; i < n; i++)
{
if (arr[i] == num)
{
loc = i;
index = loc;
}
}
if (loc == -1)
{
cout << "Not found";
}
else
{
cout << "sucessfully found." << endl;
cout << "Your entered number is on this index:" << index;
}
}
void FindmaximumNumber(int *arr, int n)
{
int max, min;
max = n - 1;
min = n - n;
cout << "The maximum value is=" << arr[max] << endl;
cout << "The minimum value is=" << arr[min] << endl;
}
void CalculateSumArray(int *arr, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + arr[i];
}
cout << "Sum of Array Elements=" << sum << endl;
}
int main()

21
Object Oriented Programming (OOP)

{
int n = 10;
int arr[n];
cout<<"Please Input 10 values:";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort1D(arr, n);
FindmaximumNumber(arr, n);
CalculateSumArray(arr, n);
SearchArray(arr, n);
}

Object Code:

22
Object Oriented Programming (OOP)

Task: 15
Source Code:
//Write a program in C++ and perform the following: Find out the greatest and the
smallest among three numbers using pointers. You can use function if you want.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3, *ptr1, *ptr2, *ptr3;
cout << "Enter three numbers:";
cin >> num1 >> num2 >> num3;
ptr1 = &num1;
ptr2 = &num2;
ptr3 = &num3;
if (*ptr1 > *ptr2 && *ptr1 > *ptr3 && *ptr2 > *ptr3)
{
cout << "The gratest number is=" << *ptr1 << endl;
cout << "The smallest number is=" << *ptr3 << endl;
}
else if (*ptr1 > *ptr2 && *ptr1 > *ptr3 && *ptr2 < *ptr3)
{
cout << "The gratest number is=" << *ptr1 << endl;
cout << "The smallest number is=" << *ptr2 << endl;
}
else if (*ptr2 > *ptr1 && *ptr2 > *ptr3 && *ptr1 > *ptr3)
{
cout << "The gratest number is=" << *ptr2 << endl;
cout << "The smallest number is=" << *ptr3 << endl;
}
else if (*ptr2 > *ptr1 && *ptr2 > *ptr3 && *ptr1 < *ptr3)
{
cout << "The gratest number is=" << *ptr2 << endl;
cout << "The smallest number is=" << *ptr1 << endl;
}
else if (*ptr3 > *ptr1 && *ptr3 > *ptr2 && *ptr1 > *ptr2)
{
cout << "The gratest number is=" << *ptr3 << endl;
cout << "The smallest number is=" << *ptr2 << endl;
}
else if (*ptr3 > *ptr1 && *ptr3 > *ptr2 && *ptr1 < *ptr2)
{
cout << "The gratest number is=" << *ptr3 << endl;
cout << "The smallest number is=" << *ptr1 << endl;

23
Object Oriented Programming (OOP)

}
}

Object Code:

Task: 16
Source Code:
/*Print the Following Pattern Using Nested Loop.
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
*/
#include <iostream>
using namespace std;

24
Object Oriented Programming (OOP)

int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <=i ; j++)
{
cout<<"*";
}
cout<<endl;

}
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <=i ; j++)
{
cout<<"*";
}
cout<<endl;

Object Code:

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy