C++ Assignment 1 (Deepti)
C++ Assignment 1 (Deepti)
#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;
}
#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;
}
#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;
}
#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;
}