0% found this document useful (0 votes)
7 views

C Lastassignment

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

C Lastassignment

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

Activity 3

1. Write a C++ program to calculate the area of a rectangular room:

#include <iostream>

using name space std;

int main() {

double length, breadth, area;

std::cout << "Enter the length of the room: ";

std::cin >> length;

std::cout << "Enter the breadth of the room: ";

std::cin >> breadth;

// Calculate the area

area = length * breadth;

// Display the result

std::cout << "The area of the room is: " << area << std::endl;

return 0;

2. Write a C++ program to display the remainder and quotient when one integer number is divided by
another:

#include <iostream>

Using namespace std;

int main() {

int dividend, divisor, quotient, remainder;

std::cout << "Enter the dividend: ";

std::cin >> dividend;

std::cout << "Enter the divisor: ";

std::cin >> divisor;


// Calculate the quotient and remainder

quotient = dividend / divisor;

remainder = dividend % divisor;

// Display the result

std::cout << "Quotient: " << quotient << std::endl;

std::cout << "Remainder: " << remainder << std::endl;

return 0;

Activity 4

1 write a C++ program to convert a binary number to a decimal number:

#include <iostream>

#include <cmath>

Using namespace std;

int main() {

long long binaryNumber;

int decimalNumber = 0, i = 0, remainder;

std::cout << "Enter a binary number: ";

std::cin >> binaryNumber;

// Convert binary to decimal

while (binaryNumber != 0) {

remainder = binaryNumber % 10;

binaryNumber /= 10;
decimalNumber += remainder * pow(2, i);

++i;

std::cout << "Decimal number: " << decimalNumber << std::endl;

return 0;

Activity 5

Write a C++ program for a guessing game. Let your program randomly generate a secret number
between 1 and 100. Let the program allow the user to guess the number and display YOU ARE CORRECT
if the correct number is guessed , TOO HIGH if the guessed number is higher than the secret number,
TOO LOW if the guessed umber is lower than the secret number, SORRY!! TRY AGAIN NEXT TIME if the
guess exceed three times.

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main() {

srand(time(0)); // Seed the random number generator

int secretNumber = rand() % 100 + 1; // Generate a random number between 1 and 100

int guess, attempts = 0;

std::cout << "Welcome to the guessing game!" << std::endl;

std::cout << "I have picked a secret number between 1 and 100." << std::endl;
do {

std::cout << "Guess the number: ";

std::cin >> guess;

attempts++;

if (guess == secretNumber) {

std::cout << "YOU ARE CORRECT! Congratulations!" << std::endl;

break;

} else if (guess > secretNumber) {

std::cout << "TOO HIGH! Try again." << std::endl;

} else {

std::cout << "TOO LOW! Try again." << std::endl;

if (attempts >= 3) {

std::cout << "SORRY!! TRY AGAIN NEXT TIME. The secret number was: " << secretNumber <<
std::endl;

break;

} while (true);

return 0;

Activity 6

Write C++ program to calculate an area of 3 rectangles of the following lengths and breaths 12,22,44
and 10,6,15 respectively
#include <iostream>

using namespace std;

int calculateArea(int length, int breadth) {

return length * breadth;

int main() {

int lengths[] = {12, 22, 44};

int breadths[] = {10, 6, 15};

for (int i = 0; i < 3; ++i) {

int area = calculateArea(lengths[i], breadths[i]);

std::cout << "The area of rectangle " << (i + 1) << " is: " << area << std::endl;

return 0;

}.

Activity 7

Write a C++ program to display a duplicate element in an array

#include <iostream>

#include <unordered_set>

using namespace std;

void findDuplicate(int arr[], int size) {

std::unordered_set<int> seen;

for (int i = 0; i < size; ++i) {


if (seen.find(arr[i]) != seen.end()) {

std::cout << "Duplicate element found: " << arr[i] << std::endl;

return;

seen.insert(arr[i]);

std::cout << "No duplicate element found." << std::endl;

int main() {

int arr[] = {2, 4, 6, 8, 4, 10, 12};

int size = sizeof(arr) / sizeof(arr[0]);

findDuplicate(arr, size);

return 0;

Activity 8

Objective: using two dimensional array

Write a C++ program to find the determinant of a 2/2 matrix

#include <iostream>

Using namespace std;

int main() {

int matrix[2][2];

// Input the elements of the matrix

std::cout << "Enter the elements of the 2x2 matrix:" << std::endl;
for (int i = 0; i < 2; ++i) {

for (int j = 0; j < 2; ++j) {

std::cout << "Enter element at position (" << i << ", " << j << "): ";

std::cin >> matrix[i][j];

// Calculate the determinant

int determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);

// Display the determinant

std::cout << "The determinant of the matrix is: " << determinant << std::endl;

return 0;

Activity 9

Objective: understanding C++ function

Write a C++ program to calculate the sum of the series below using function

#include <iostream>

#include <cmath>

using namespace std;

double calculateSeriesSum(double x, int n) {

double sum = 1.0; // Initialize the sum with the first term of the series

for (int i = 1; i <= n; ++i) {

double term = std::pow(x, i) / std::tgamma(i + 1); // Calculate each term of the series

sum += term; // Add the term to the sum

}
return sum;

int main() {

double x;

int n;

std::cout << "Enter the value of X: ";

std::cin >> x;

std::cout << "Enter the value of n: ";

std::cin >> n;

double seriesSum = calculateSeriesSum(x, n);

std::cout << "The sum of the series (1+X)^n is: " << seriesSum << std::endl;

return 0;

Activity 10

Objective: more on C++ function

Write a C++ program to check if a number is palindrome or not using function

#include <iostream>

using namespace std;

bool isPalindrome(int number) {

int originalNumber = number;


int reversedNumber = 0;

while (number > 0) {

int digit = number % 10;

reversedNumber = reversedNumber * 10 + digit;

number /= 10;

return (originalNumber == reversedNumber);

int main() {

int number;

std::cout << "Enter a number: ";

std::cin >> number;

if (isPalindrome(number)) {

std::cout << "The number is a palindrome." << std::endl;

} else {

std::cout << "The number is not a palindrome." << std::endl;

return 0;

You might also like