Exercise 1: Print the lines:
You are 18 years old.
You are too young to play the game.
Solution:
#include <cstdlib> #include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int age;
age=10;
cout<<" You are "<<age<<" years old.\n";
cout<<" You are too young to play the game.\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 2: Write five C++ statements to print the asterisk pattern as shown below.
*****
*****
*****
*****
*****
Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout<<"*****\n";
cout<<"*****\n";
cout<<"*****\n";
cout<<"*****\n";
cout<<"*****\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 3: Write a C++ program to declare two integer , one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen.
Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x;
int y;
float z;
x=10;
y=15;
z=12.6;
cout<<"x="<<x<<"\t"<<"y="<<y<<"\t"<<"z="<<z;
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 4: Write a C++ program to prompt the user to input her/his name and print this name on the screen, as shown below. The text from keyboard can be read by using cin>> and to display the text on the screen you can use cout<<.
Hello Sok! .
Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char name[20];
cout<<"Enter your name:";
cin>>name;
cout<<"Hello "<<name<<"! \n";
system("PAUSE");
return EXIT_SUCCESS;
}
Exercise 5: Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below.
Please enter your 3 numbers: 12 45 78
Your numbers forward:
12
45
78
Your numbers reversed:
78
45
12
Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int val1;
int val2;
int val3;
cout<<"Please enter your 3 numbers:";
cin>>val1>>val2>>val3;
cout<<"\nYour numbers forward:\n";
cout<<val1<<"\n"<<val2<<"\n"<<val3<<"\n";
cout<<"Your numbers reversed:\n";
cout<<val3<<"\n"<<val2<<"\n"<<val1<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
No comments
Post a Comment