You are on page 1of 8

Programming Fundamentals

Chapter 2,3
Introduction to C++
Lecture Outline:
• Sample programs
Programming Examples
EXAMPLE 1:

Write a program to #include <iostream>


using namespace std;
prompt the user to int main()
{
enter his name
char name_I;
int age;
initial, age and float height;
cout<<“Please enter your name initial, age and height \n”;
height and display cin>>name_I>>age>>height;
cout<<“name is”<<name_I <<“\n” <<“age is”<<age<<“ \n”;
them on the cout<<“height is”<<height;
return 0;
screen
}

4
Programming Examples
EXAMPLE 1:
#include <iostream>
Write a program to ask using namespace std;
int main()
the user for the width {
const int one_tree_space 4;
and length of a piece of int length, width, area, no_of_trees;
cout<<“Enter the length of the land \n”;
land and then tell him cin>>length;
cout<<“Enter the width of the land \n”;
how many orange trees cin>>width;
area = Length*width;
he can grow on it. no_of_trees=area/one_tree_space;
cout<<“The available number of trees = “<<no_of_trees;
Given that each orange return(0);
}
tree requires 4 m2.

5
Programming Examples
EXAMPLE 2:

Write a program to ask the #include <iostream>


using namespace std;
user to input the radius of
a circle, and then display int main()
{
its area and circumference const float PI 3.141593;
float radius, area, c;
cout<<“Enter radius of the circle \n”;
cin>>radius;
area = PI * radius * radius;
c = 2 * PI * radius;
cout<<“The area of the circle = “<<area;
cout<<“The circumference of the circle = “<<c;
return(0);
}

6
Programming Examples

EXAMPLE 3 //This program gets the sum and product of 2 integers


Write a program to #include <iostream>
create two integer using namespace std;
int main()
variables, and store {
int first,second,sum,product; //declaring variables
in them the values 3 first = 3;
and 5. Then second = 5;
sum = first + second ;
calculate their sum product = first * second ;
cout<<“The sum is “<< sum<<“ \n”;
and their product cout<<“The product is “<< product<<“ \n”;
and display the return(0);
}
result . Use
comments.

7
Thank You

You might also like