0% found this document useful (0 votes)
21 views16 pages

C++ Assignment 1 (Deepti)

Uploaded by

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

C++ Assignment 1 (Deepti)

Uploaded by

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

C++ Assignment

B.Sc. (H) Computer


Science
Sem-2

Submitted by: DEEPTI


(230421)
Section-A
1. WAP to print the sum and product of last 2 digits of
an integer.
#include <iostream>
using namespace std;
int main() {
cout << "Enter an integer: ";
int number;
cin >> number;
int lastDigit = number % 10;
int secondLastDigit = (number / 10) % 10;
int sum = lastDigit + secondLastDigit;
int product = lastDigit * secondLastDigit;
cout << "Sum of last two digits: " << sum <<
endl;
cout << "Product of last two digits: " <<
product << endl;
return 0;
}
2. WAP to reverse a 3-digit number.
#include <iostream>
using namespace std;
int main() {
cout << "Enter a 3-digit number: ";
int number;
cin >> number;
int firstDigit = number / 100;
int secondDigit = (number / 10) % 10;
int thirdDigit = number % 10;
int reversedNumber = thirdDigit * 100 +
secondDigit * 10 + firstDigit;
cout << "Reversed number: " << reversedNumber
<< endl;
return 0;
}
3. WAP using functions to reverse a number and check if
it is a palindrome.

#include <iostream>
using namespace std;
int reverseNumber(int num) {
int reversedNum = 0;
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
return reversedNum;
}
bool isPalindrome(int num) {
return num == reverseNumber(num);
}
int main() {
cout << "Enter a number: ";
int number;
cin >> number;
if (isPalindrome(number)) {
cout << number << " is a palindrome." << endl;
} else {
cout << number << " is not a palindrome." <<
endl;
}
return 0;
}

9. Write a program to check whether a number is even


or odd.

#include <iostream>
using namespace std;
void checkEvenOdd(int num) {
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
}
int main() {
cout << "Enter a number: ";
int number;
cin >> number;
checkEvenOdd(number);
return 0;
}
10. Write a program to check whether a year is a leap
year.
#include <iostream>
using namespace std;
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year %
400 == 0)) {
return true;
} else {
return false;
}
}
int main() {
cout << "Enter a year: ";
int year;
cin >> year;
if (isLeapYear(year)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}

11. Write a program to check whether a character is an


alphabet.

#include <iostream>
using namespace std;
bool isAlphabet(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
<= 'Z');
}
int main() {
cout << "Enter a character: ";
char character;
cin >> character;
if (isAlphabet(character)) {
cout << character << " is an alphabet." << endl;
} else {
cout << character << " is not an alphabet." <<
endl;
}
return 0;
}
12. Write a program to input any alphabet and check
whether it is a vowel or consonant.

#include <iostream>
using namespace std;
bool isVowel(char ch) {
ch = tolower(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||
ch == 'u';
}
int main() {
cout << "Enter an alphabet: ";
char character;
cin >> character;
if (isVowel(character)) {
cout << character << " is a vowel." << endl;
} else {
cout << character << " is a consonant." << endl;
}
return 0;
}

13. Write a program to input any character and check


whether it is an alphabet, a digit or a special
character.

#include <iostream>
using namespace std;
bool isAlphabet(char ch) {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
<= 'Z');
}
bool isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
int main() {
cout << "Enter a character: ";
char character;
cin >> character;
if (isAlphabet(character)) {
cout << character << " is an alphabet." << endl;
} else if (isDigit(character)) {
cout << character << " is a digit." << endl;
} else {
cout << character << " is a special character." <<
endl;
}
return 0;
}
14. Write a program to check whether a character is
an uppercase or lowercase alphabet.

#include <iostream>
using namespace std;
bool isUppercase(char ch) {
return ch >= 'A' && ch <= 'Z';
}
bool isLowercase(char ch) {
return ch >= 'a' && ch <= 'z';
}
int main() {
cout << "Enter a character: ";
char character;
cin >> character;
if (isUppercase(character)) {
cout << character << " is an uppercase alphabet."
<< endl;
} else if (isLowercase(character)) {
cout << character << " is a lowercase alphabet."
<< endl;
} else {
cout << character << " is not an alphabet." <<
endl;
}
return 0;
}
16. Write a program to input the month number and
print the number of days in that month.

#include <iostream>
using namespace std;
int getDaysInMonth(int month) {
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case
12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return 28;
default:
return -1;
}
}
int main() {
cout << "Enter the month number (1-12): ";
int month;
cin >> month;
int daysInMonth = getDaysInMonth(month);
if (daysInMonth != -1) {
cout << "Number of days in month " << month
<< ": " << daysInMonth << endl;
} else {
cout << "Invalid month number." << endl;
}
return 0;
}

You might also like