Computer Programming Lecture 3
Computer Programming Lecture 3
3
Bilal Ashfaq Ahmed
Task
Calculate the average age of a class of ten
students. Prompt the user to enter the age of
each student.
#include <iostream.h>
#include<conio.h>
void main ( )
{
int age1, age2, age3, age4, age5, age6, age7, age8, age9,
age10 ;
int TotalAge ;
int AverageAge ;
cout << “ Enter the age of student 1: “ ;
cin >> age1 ;
cout << “ Enter the age of student 2: “ ;
cin >> age2 ;
:
:
TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+
age8+age9 + age10 ;
AverageAge = TotalAge / 10 ;
cout<< “The average age of the class is :” << AverageAge ;
getch();
}
Quadratic Equation
In algebra
y = ax2 + bx + c
In C++
y = a*x*x + b*x + c
a*b%c +d
Rule of Evaluation
In algebra, there may be curly brackets { } and square brackets
[ ] in an expression but in C we have only parentheses ( ).
Using parentheses, we can make a complex expression easy to
read and understand and can force the order of evaluation. We
have to be very careful while using parentheses, as parentheses
at wrong place can cause an incorrect result.
No expression on the left hand side of the assignment
Integer division truncates fractional part
Liberal use of brackets/parenthesis
Interesting Problem
Write a program that accepts Four digit number from
the user decomposes it into first, second, third and
four digit .
For example if number =1234 then
first=1
Second=2
Third=3
Fourth=4
/* A program that takes a four digits integer from user and shows
the digits on the screen
separately i.e. if user enters 1234, it displays 1,2,3,4 separately. */
#include <iostream.h>
#include<conio.h>
void main ()
{
// declare variables
int num;
int fd, sd, td, frd;
clrscr();
// prompt the user for input
cout <<"\n\t Enter the Four Digit Number: ";
cin >> num;
// Seperation logic
fd = num / 1000;
sd = (num % 1000)/100;
td = (num % 100)/10;
frd= num % 10;
// Display number seperately
getch();
}
Task
Write a program that takes radius of a circle
from the user and calculates the diameter,
circumference and area of the circle and
display the result.