0% found this document useful (0 votes)
10 views7 pages

Assignment 04

The document contains multiple C++ programming tasks, each implementing different functionalities such as calculating day numbers, printing squares, computing powers, summing even and odd numbers, checking for perfect numbers, reversing digits, and converting uppercase letters to lowercase. Each task includes a main function that prompts user input and calls specific functions to perform the desired operations. Overall, the document serves as a collection of programming exercises demonstrating various programming concepts.

Uploaded by

f240784
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)
10 views7 pages

Assignment 04

The document contains multiple C++ programming tasks, each implementing different functionalities such as calculating day numbers, printing squares, computing powers, summing even and odd numbers, checking for perfect numbers, reversing digits, and converting uppercase letters to lowercase. Each task includes a main function that prompts user input and calls specific functions to perform the desired operations. Overall, the document serves as a collection of programming exercises demonstrating various programming concepts.

Uploaded by

f240784
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/ 7

Assignment 04

Task 01
#include <iostream>
using namespace std;

bool isLeapYear(int year)


{
if (year % 4 != 0)
return false;
else if (year % 100 != 0)
return true;
else if (year % 400 == 0)
return true;
else
return false;
}

void printDayNumber(int month, int day, int year)


{
int daysInMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (isLeapYear(year))
{
daysInMonths[1] = 29;
}

if (month < 1 || month > 12 || day < 1 || day > daysInMonths[month - 1]) {
cout << "Invalid date." << endl;
return;
}

int dayNumber = day;


for (int i = 0; i < month - 1; ++i)
{
dayNumber += daysInMonths[i];
}

cout << "Day number of the year: " << dayNumber << endl;
}

int main()
{
int month, day, year;
cout << "Enter date (MM-DD-YYYY): ";
cin >> month >> day >> year;

printDayNumber(month, day, year);

return 0;
}
Task 02
#include <iostream>
using namespace std;

void printSquare(int side) {


for (int i = 0; i < side; ++i) {
for (int j = 0; j < side; ++j) {
cout << "*";
}
cout << endl;
}
}

int main() {
int side;
cout << "Enter the side length of the square: ";
cin >> side;

printSquare(side);

return 0;
}

Task 03
#include<iostream>
using namespace std;

void power_cal(int a, int b)


{
int power = 1;
for (int i = 0;i < b;i++)
{
power = power * a;
cout << power << endl;
}
}
int main()
{
int n, n1;
cout << "Enter the number and power: ";
cin >> n;
cin >> n1;
power_cal(n, n1);

system("pause");
return 0;
}

Task 04
#include<iostream>
using namespace std;
int sumofevenodd(int upper_bound)
{
int sumofEven = 0;
int sumofOdd=0;
for (int i = 1;i <= upper_bound;i++)
{
if (i % 2 == 0)
sumofEven += i;
else
sumofOdd += i;
}
cout << "The sum of Even: " << sumofEven;
cout << "The sum of Odd: " << sumofOdd;
int abs_diff = sumofEven - sumofOdd;
cout << "The absolute difference is : ";
if (abs_diff < 0)
{
abs_diff = abs_diff *(-1);
}
return abs_diff;
}
int main()
{
int n;
cout << "Enter the upper bound number: ";
cin >> n;
cout << sumofevenodd(n);
system("pause");
return 0;
}
Task 05
#include <iostream>
#include <cmath>
using namespace std;

bool karatosDestruction(double base, double exponent, double result) {


return pow(base, exponent) == result;
}

int main() {
double num1, num2, num3;

while (true) {
cout << "Enter three numbers (base exponent result): ";
cin >> num1 >> num2 >> num3;

if (karatosDestruction(num1, num2, num3)) {


cout << "Condition met. Program terminated." << endl;
break;
}
else {
cout << "Incorrect. Try again." << endl;
}
}

return 0;
}

Task 06
#include <iostream>
using namespace std;

bool isPerfect(int number) {


int sum = 0;
for (int i = 1; i < number; ++i) {
if (number % i == 0) {
sum += i;
}
}
return sum == number;
}

void printDivisors(int number) {


cout << "Divisors: ";
for (int i = 1; i < number; ++i) {
if (number % i == 0) {
cout << i << " ";
}
}
cout << endl;
}

int main() {
cout << "Perfect numbers between 1 and 1000:\n" << endl;
for (int i = 1; i <= 1000; ++i) {
if (isPerfect(i)) {
cout << i << " is a perfect number." << endl;
printDivisors(i);
cout << endl;
}
}

return 0;
}

Task a
#include <iostream>
using namespace std;

int reverseDigits(int number) {


int reversed = 0;
while (number != 0) {
int digit = number % 10;
reversed = reversed * 10 + digit;
number /= 10;
}
return reversed;
}

int main() {
int num;
cout << "Enter an integer: ";
cin >> num;

int reversedNum = reverseDigits(num);


cout << "Reversed number: " << reversedNum << endl;

return 0;
}

Task b
#include <iostream>
using namespace std;

bool multiple(int a, int b) {


return b % a == 0;
}

int main() {
int num1, num2;
char choice;

do {
cout << "Enter two integers: ";
cin >> num1 >> num2;

if (num1 == 0) {
cout << "Division by zero is not allowed." << endl;
}
else if (multiple(num1, num2)) {
cout << num2 << " is a multiple of " << num1 << "." << endl;
}
else {
cout << num2 << " is NOT a multiple of " << num1 << "." << endl;
}

cout << "Do you want to try another pair y, n: ";


cin >> choice;

} while (choice == 'y' || choice == 'Y');

return 0;
}

Task c
#include <iostream>
using namespace std;

void findPreceding(char c1, char c2, char c3, char& p1, char& p2, char& p3) {
p1 = (c1 == 'a') ? 'z' : c1 - 1;
p2 = (c2 == 'a') ? 'z' : c2 - 1;
p3 = (c3 == 'a') ? 'z' : c3 - 1;
}

int main() {
char a, b, c;
char pa, pb, pc;

cout << "Enter three lowercase letters: ";


cin >> a >> b >> c;

findPreceding(a, b, c, pa, pb, pc);

cout << "The preceding letters are: " << pa << " " << pb << " " << pc << endl;

return 0;
}

Task d
#include <iostream>
#include <cctype> // For the tolower function
using namespace std;

void Upper_to_lower(char& c1, char& c2, char& c3, char& c4) {


c1 = tolower(c1);
c2 = tolower(c2);
c3 = tolower(c3);
c4 = tolower(c4);
}

int main() {
char a, b, c, d;

cout << "Enter four uppercase letters: ";


cin >> a >> b >> c >> d;

Upper_to_lower(a, b, c, d);

cout << "The lowercase letters are: " << a << " " << b << " " << c << " " << d << endl;

return 0;
}

You might also like