Exercise 1.1:
Write a program that takes an year (e.g. 2018 or 1998) as input and checks if the year is a leap year.
Note : An year is a leap year if it is divisible by 4, but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400. Sample output 1:
Please enter year : 1996
Year is leap year
Sample output 2:
Please enter year : 2000
Year is not a leap year
Solution:
#include
#include
using namespace std;
int main()
{
int year;
cout
cin >> year;
if(year % 4 == 0 && year%100 != 0)
{
cout
}
else if(year%100 == 0 && year % 400 == 0)
{
cout
}
else
{
cout
}
}
Output
Exercise 1.2
Write a program that takes a six digit integer as input and prints them with a seperator ( | ). Accomplish the task without using loop.
Sample output 1:
Please enter a six digit Number : 135996
The number with seperators is : 1 | 3 | 5 | 9 | 9 | 6 Sample output 2:
Please enter a six digint number : 642389
The number with seperators is : 6 | 4 | 2 | 3 | 8 | 9 Solution:
#include
using namespace std;
int main()
{
int number, digit;
cout
cin >> number;
digit = number / 100000;
cout
number = number % 100000;
digit = number / 10000;
cout
number = number % 10000;
digit = number / 1000;
cout
number = number % 1000;
digit = number / 100;
cout
number = number % 100;
digit = number / 10;
cout
number = number % 10;
cout
return 0;
}
Output
Exercise 1.3
Write a program that takes number of seconds elapsed as input e.g. (87792) and outputs the time in hours:minutes:seconds format as hh:mm:ss. See the sample output Sample output 1:
Please enter number of seconds : 87792
The time in required format is : 24:23:12
Sample output 2:
Please enter number of seconds : 43834
The time in required format is : 12:10:34
Solution:
#include
using namespace std;
int main()
{
int secondsElapsed, hours, minutes, seconds;
const int SECONDS_PER_MIN = 60;
const int SECONDS_PER_HOUR = 60 * SECONDS_PER_MIN;
cout
cin >> secondsElapsed;
hours = secondsElapsed / SECONDS_PER_HOUR;
secondsElapsed = secondsElapsed % SECONDS_PER_HOUR;
minutes = secondsElapsed / SECONDS_PER_MIN;
seconds = secondsElapsed % SECONDS_PER_MIN;
cout
}
Output
Exercise 1.4
Write a program that takes a number as input and prints the digits of number in reverse order and with a seperator.
Hint: Use while loop to check if the number is still greater than 0 and find remaineder with required number.
Sample output 1:
Please enter a number : 156
The number with seperators is : 6 | 5 | 1
Sample output 2:
Please enter a six digit number : 42389
The number with seperators is : 9 | 8 | 3 | 2 | 4
Solution:
#include
using namespace std;
int main()
{
int num;
cout
cin >> num;
while(num > 0)
{
cout
num = num / 10;
}
}
Output