Muhammad Mamoon Irfan - F2023266192 - OOPLAB - V-6 - LAB#1
Muhammad Mamoon Irfan - F2023266192 - OOPLAB - V-6 - LAB#1
Muhammad Mamoon Irfan - F2023266192 - OOPLAB - V-6 - LAB#1
Email: f2023266192@umt.edu.pk
Assignment No: 1
Announcement date:3/16/2024
Section: V6
Object Oriented Programming (OOP)
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)
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)
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)
swap(A, B, n);
}
Object Code:
17
Object Oriented Programming (OOP)
Task: 13
Source Code:
18
Object Oriented Programming (OOP)
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)
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