Programs of printing for beginners

Exercise 1: Write a C++ program to print the following lines:

You are 10 years old. 
You are too young to play the game.
Solution:
#include <conio.h> 
#include <iostream.h> 
void main()
{ 
    int age; 
    age=10; 
    cout<<" You are "<<age<<" years old.\n"; 
    cout<<" You are too young to play the game.\n"; 
    getch();
}

Exercise 2: Write five C++ statements to print the asterisk pattern as shown below.


***** 
***** 
***** 
***** 
***** 

Solution:
#include <conio.h> 
#include <iostream.h> 
  
void main()
{ 
    cout<<"*****\n"; 
    cout<<"*****\n"; 
    cout<<"*****\n"; 
    cout<<"*****\n"; 
    cout<<"*****\n"; 
    getch(); 
}

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 <conio.h> 
#include <iostream.h> 
  
void main() 
  
{ 
    int x; 
    int y; 
    float z; 
    x=10; 
    y=15; 
    z=12.6; 
    cout<<"x="<<x <<"\t"<<"y= "<<y<<"\t"<<"z="<<z; 
    cout<<"\n"; 
     getch();
} 

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 <conio.h> 
#include <iostream.h> 
 
void main()
{ 
    char name[20]; 
    cout<<"Enter your name:"; 
    cin>>name; 
    cout<<"Hello "<<name<<"! \n"; 
        
    getch();
} 

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 <conio.h> 
#include <iostream.h> 
    
void main()
{ 
    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"; 
    getch();
} 


0 comments: