In: Computer Science
5. Write a C++ statement or statements that will:
Print the first two digits and the last two digits of any 4 digit number stored in an integer variable n.
For example, given int n = 5623, print
56 23.
6. Write C++ statements that will align the following three lines as printed in two 20 character columns.
Name Years President
Abraham Lincoln 1860-1865
Thomas Jefferson 1801-1809
7. Write a C++ statement or statements that will Output if a string has a length greater than 10, equal to 10 or less than 10.
Examples :
string str1 = “Four Score and Seven Years Ago” would output “String Length > 10”
string str2 = “Good Day” would output “String Length < 10”
string str3 = 0123456789” would output “String Length = 10”
5.
Source Code:
#include <iostream>
using namespace std;
int main()
{
int n=5623;
cout<<(n/100)<<"
"<<(n%100)<<endl;
// modulo 100 returns last 2 digits and
// division with 100 gives first 2 digits
}
6.
Source Code:
#include <iostream>
using namespace std;
int main()
{
cout<<"Name"<<" "<<"Years
President"<<endl;
cout<<"Abraham Lincoln"<<"
"<<"1860-1865"<<endl;
cout<<"Thomas Jefferson"<<"
"<<"1801-1809"<<endl;
}
7.
Source Code:
#include <iostream>
using namespace std;
void sayStringLength(string str)
{
if(str.length()>10)
cout<<"String Length > 10"<<endl;
else if(str.length()<10)
cout<<"String Length < 10"<<endl;
else
cout<<"String Length = 10"<<endl;
}
int main()
{
string str1 = "Four Score and Seven Years Ago";
sayStringLength(str1);
string str2 = "Good Day";
sayStringLength(str2);
string str3 = "0123456789";
sayStringLength(str3);
}