Professional Documents
Culture Documents
PROBLEM SOLVING
(CSC415)
by
AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM SHAH ALAM
1
Component Of
Programming Language
Chapter 2
2
Contents
Basic Operations
Program Elements
Storage
Input and Output
Arithmetic Operations
constant
Storage variable
Example:
int number;
Examples
int number;
int score;
long population;
float temperature;
double accountBalance;
char gender;
December 2, 2023 CSC425 : INTRODUCTION TO COMPUTER 13
PROGRAMMING
Example :
#include <iostream.h>
//program to calculate area of a circle
PI 3.1416
main()
{ radius ?
//variable and constant allocation
float const PI = 3.1416;
area ?
float radius, area;
//input section
cout << “Enter a radius : ”;
cin >> radius;
//output section
cout << “Area of the circle is ” << area <<
endl;
return 0;
}//end main() 14
Examples
Enter a radius : __
main()
{
radius 8
//variable and constant allocation
float const PI = 3.1416; area 201.0624
float radius, area;
//input section
cout << “Enter a radius : ”;
cin >> radius;
//output section
cout << “Area of the circle is ” << area << endl;
return 0;
}//end main()
17
Examples
Enter a radius : 8
Area of the circle is 201.0624
• Example
Read a number
int a = 5, b = 2;
float c;
c = a / b; //the new value of c is 2
int a = 5, b = 2,
float c;
c = float(a) / b; //the new value of c is 2.5
c = a / float(b); //the new value of c is 2.5
Invalid Statement!
static_cast<data_type>(expression)
- example :
int n = static_cast<int>(x + 0.5)
(see Exercise 12)
Try to run :
- Exercise 3
- Exercise 2
- Exercise 7
- Exercise 8
Exercise 9 :
Write a program that does the following :
1. Prompts the user to input two decimal numbers
2. Find the sum, subtract and average of the two integers
Assignment #1
Try to run :
- Exercise 4
- Exercise 5
- Exercise 8
Read totalScore
Read count
Display average
End